Handle Webhook Notifications
To receive Metronome webhooks, you need a webhook handler listening at a publicly accessible HTTPS URL that performs the below tasks.Acknowledge the Notification
Upon receiving a notification, your endpoint must return a successful status code, such as200 OK. If Metronome receives a status code >299, notification retries are attempted until a successful status code is returned.
If your webhook endpoint does not properly acknowledge the notification, Metronome continuously retries it with exponential backoff until it hits a 15-minute retry cadence. Once the retry process hits 15 minutes, Metronome repeats the notification until it is either accepted or two days have passed since the initial notification attempt (~200 retries).
It is recommended that you process webhooks asynchronously: store the webhook payload in a queue, return a 200 response code, and only then validate or process the payload. Removing webhook processing from the receiving path reduces the likelihood of your systems getting blocked.
Prepare for Duplicate Notifications
Under normal circumstances, Metronome sends each notification exactly once. However, there are a few situations that could cause you to receive the same notification multiple times:- Retries — As mentioned above, if Metronome receives an error when attempting to deliver the notification to your webhook handler, we retry the notification. Depending on the nature of the error, it’s possible that your endpoint receives a notification without acknowledging it, in which case your endpoint receives the same notification again.
- Multiple webhook URLs — If you configure Metronome to notify multiple webhook URLs, or even the same URL multiple times, notifications are sent multiple times. If you want to ensure duplicate notifications are ignored, you can use the notification’s
idfield to deduplicate.
Verify Notifications
Because your webhook endpoint is a public URL, anyone could send a request to it. Before you take actions based on the notification, you should verify the information it contains. You can do this in two ways: by fetching data from the Metronome API yourself or by verifying the webhook request’s signature.INFOMetronome may introduce backward-compatible changes to existing webhook shapes without notice, such as adding a new webhook field. Metronome recommends that you validate only fields that are expected to be present according to the documentation at time of integration.
Call the Metronome API
Webhook notifications contain only minimal information about the event that occurred. This means it’s often useful to call the appropriate Metronome API endpoint to get the full details. For example, if you receive a webhook notification informing you that an invoice has been finalized, you can call the/customers/{customer_id}/invoices/{invoice_id} endpoint to fetch the details of the invoice mentioned in the notification. In this way, the notification serves as a hint that something has changed, but your code relies only on data obtained directly from the Metronome API.
Verify Signatures
If the above strategy doesn’t work for your use case, Metronome also provides a method to verify the authenticity of notifications as you receive them by using theMetronome-Webhook-Signature HTTP header. The value of this header is a cryptographic signature of the HTTP request, using a secret key set up when you configure your webhook.
To validate the signature, first concatenate the value of the request’s X-Metronome-Date header and the exact bytes of the request body, separated by a newline character (\n). Then compute the HMAC-SHA256 of the resulting string, keyed by the webhook’s secret key. Finally, compare the hexadecimal representation of the HMAC you computed with the one found in the Metronome-Webhook-Signature header. If they don’t match, the webhook notification did not come from Metronome.
X-Metronome-Date header is included to aid in deduplication. You should ignore webhook requests that are older than five minutes, which means your webhook handler only needs to store recent notification IDs to prevent duplicates.
The following code example shows how to perform signature validation:
correct-horse-battery-staple:
Webhook IP Addresses
Webhook notifications may come from the following IP addresses. This list may change, but if it does we’ll give at least 30 days notice.Send Webhooks to Slack
If you want to receive Metronome notifications in Slack without building a custom webhook handler, you can configure Metronome to send notifications directly to a Slack channel. From there, you can triage and act on them. To send Metronome webhooks to Slack:- Create an incoming webhook in Slack and copy the generated webhook URL. It’ll look something like:
https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX - Create a webhook destination in Metronome for your Slack channel. In the Metronome app, go to Developer > Notifications > Webhooks > Add and paste your Slack webhook URL.
- Set up a test notification to verify the integration. In the Metronome app, go to Developer > Notifications > Create Notification and create a notification like Low credit balance reached.
- Verify the integration by testing the webhook connection. Trigger your test notification and check your Slack channel for a message from Metronome.