Webhooks
In this guide, we will look at how to register and consume webhooks to integrate your app with InvoiceAhoy. With webhooks, your app can know when something happens in InvoiceAhoy, such as someone creating or updating an invoice.
Registering webhooks
To register a new webhook, you need to have a URL in your app that InvoiceAhoy can call. You can configure a new webhook from the InvoiceAhoy dashboard under API settings. Give your webhook a name, pick the events you want to listen for, and add your URL.
Now, whenever something of interest happens in your app, a webhook is fired off by InvoiceAhoy. In the next section, we'll look at how to consume webhooks.
Consuming webhooks
When your app receives a webhook request from InvoiceAhoy, check the type
attribute to see what event caused it. The first part of the event type will tell you the payload type, e.g., a conversation, message, etc.
Example webhook payload
{
"id": "a056V7R7NmNRjl70",
"type": "invoice.updated",
"payload": {
"id": "WAz8eIbvDR60rouK"
// ...
}
}
In the example above, an invoice was updated
, and the payload type is a invoice
.
Event types
- Name
invoice.created
- Description
A new invoice was created.
- Name
invoice.updated
- Description
An existing invoice was updated.
- Name
invoice.deleted
- Description
An invoice was successfully deleted.
Example payload
{
"id": "a056V7R7NmNRjl70",
"type": "invoice.updated",
"payload": {
"id": "SIuAFUNKdSYHZF2w",
...
}
}
Security
To know for sure that a webhook was sent by InvoiceAhoy, you can verify the request signature.
Each webhook request contains a header named x-ia-signature
, and you can verify this signature by using your secret webhook key.
The signature is an HMAC hash of the request payload hashed using your secret key. Here is an example of how to verify the signature in your app:
Verifying a request
const signature = req.headers['x-ia-signature']
const hash = crypto.createHmac('sha256', secret).update(payload).digest('hex')
if (hash === signature) {
// Request is verified
} else {
// Request could not be verified
}
If your generated signature matches the x-ia-signature
header, you can be sure that the request was truly coming from InvoiceAhoy. It's essential to keep your secret webhook key safe — otherwise, you can no longer be sure that a given webhook was sent by InvoiceAhoy. Don't commit your secret webhook key to GitHub!