Webhooks
Webhooks allow you to set up a notification system that can be used to receive updates on certain requests made to the Blockradar API.
Introduction
Generally, when you make a request to an API endpoint, you expect to get a near-immediate response. However, some requests may take a long time to process, which can lead to timeout errors. In order to prevent a timeout error, a pending response is returned. Since your records need to be updated with the final state of the request, you need to either:
- Make a request for an update (popularly known as polling) or,
- Listen to events by using a webhook URL.
Helpful Tip
We recommend that you use webhook to provide value to your customers over using callbacks or polling. With callbacks, we don’t have control over what happens on the customer’s end. Neither do you. Callbacks can fail if the network connection on a customer’s device fails or is weak or if the device goes off after a transaction.
Polling vs Webhooks
Polling requires making a GET
request at regular intervals to get the final status of a request. For example, when you issue an address to a customer for deposits, you keep making a request for transactions linked to the address until you find one.
With webhooks, the resource server, Blockradar in this case, sends updates to your server when the status of your request changes. The change in status of a request is known as an event. You’ll typically listen to these events on a POST endpoint called your webhook URL.
The table below highlights some differences between polling and webhooks:
Polling | Webhooks | |
---|---|---|
Mode of update | Manual | Automatic |
Rate limiting | Yes | No |
Impacted by scaling | Yes | No |
Create a webhook URL
A webhook URL is simply a POST endpoint that a resource server sends updates to. The URL needs to parse a JSON request and return a 200 OK
:
When your webhook URL receives an event, it needs to parse and acknowledge the event. Acknowledging an event means returning a 200 OK
in the HTTP header. Without a 200 OK
in the response header, we’ll keep sending events for the next 2 hours and 35 minutes:
- We’ll attempt to send webhooks 5 times with an increasing delay between each attempt, starting with 5 minutes and increasing exponentially up to 80 minutes. The total duration for these 5 attempts will span approximately 2 hours and 35 minutes.
Avoid long-running tasks
If you have long-running tasks in your webhook function, you should acknowledge the event before executing the long-running tasks. Long-running tasks will lead to a request timeout and an automatic error response from your server. Without a 200 OK response, we retry as described in the paragraph above.
Testing Webhooks Locally
During development, you can configure your webhook URL in your TEST master wallet environment to a local address, such as http://localhost
or 127.0.0.1
.
To expose your local server to the internet for testing purposes, consider using a tool like ngrok or webhook.site. These tools allow you to see what the webhook payload looks like and simulate webhook events locally before deploying your application to a live environment.
Using these tools, you can ensure that your webhook integration is functioning correctly and handle any issues in a controlled, local environment before moving to production.
Resend Transaction Webhook
Incase where for some reasons you don’t receive transaction webhook and the backoff time is elapse, we’ve provided an API you can use to resend a transaction webhook
Use with caution!
As this can lead to processing already completed transactions, make sure you have proper validation in place while using this endpoint.
Verify event origin
Since your webhook URL is publicly available, you need to verify that events originate from Blockradar and not a bad actor. There are two ways to ensure events to your webhook URL are from Blockradar:
- Signature validation (recommended)
Signature validation
Events sent from Blockradar carry the x-blockradar-signature header. The value of this header is a HMAC SHA512 signature of the event payload signed using your secret key. Verifying the header signature should be done before processing the event:
Go Live Checklist
Now that you’ve successfully created your webhook URL, here are some ways to ensure you get a delightful experience:
- Add the webhook URL on your Master wallets on the Blockradar dashboard
- Ensure your webhook URL is publicly available (localhost URLs cannot receive events)
- If using
.htaccess
, kindly remember to add the trailing/
to the URL - Test your webhook to ensure you’re getting the JSON body and returning a
200 OK
HTTP response - If your webhook function has long-running tasks, you should first acknowledge receiving the webhook by returning a
200 OK
before proceeding with the long-running tasks - If we don’t get a
200 OK
HTTP response from your webhooks, we flag it as a failed attempt - We’ll attempt to send webhooks 5 times with an increasing delay between each attempt, starting with 5 minutes and increasing exponentially up to 80 minutes. The total duration for these 5 attempts will span approximately 2 hours and 35 minutes.
Supported events
Types of events
Here are the events we currently raise. We would add more to this list as we hook into more actions in the future.
Event | Description | |
---|---|---|
deposit.success | A deposit has been successfully made. | |
deposit.swept.success | An attempt to sweep a deposit into master wallet has been successful. | |
withdraw.success | A withdrawal attempt has been successful. | |
withdraw.failed | A withdrawal attempt has failed. | |
deposit.swept.failed | An attempt to sweep a deposit into master wallet has failed. |
Happy hacking! ❤️