Everything you need to know when it comes to getting notifications about transactions for your Crypto.org application.
Alchemy Notify works by using webhooks, a way for you to subscribe to events that occur on your application. This page will walk through what webhooks are and how you can use them in order to integrate Alchemy Notify with your application.
What are Webhooks?
Webhooks are a way for users to receive notifications when an event occurs on your application. Rather than continuously polling the server to check if the state has changed, webhooks provide information to you as it becomes available, which is a lot more efficient and beneficial for developers. Webhooks work by registering a URL to send notifications to once certain events occur.
Webhooks are typically used to connect two different applications. One application is the "sender," which subscribes to events and sends them off to the the second "receiver" application, which takes actions based upon that received data. When an event occurs on the sender application it sends that data to the webhook URL of the receiver application. The receiver application can then send a callback message, with an HTTP status code to let the sender know the data was received successfully or not.
You can think of webhook notifications just like SMS notifications. The entity sending the message has your registered phone number and they send a specific message payload to that phone number. You then have the ability to respond confirming you have received it, creating a two-way communication stream.
Types of Notifications
Alchemy offers four different types of webhooks each described below.
1. Mined Transactions
The Mined Transaction Webhook is used to notify you anytime a transaction that was sent through your application gets successfully mined. This is extremely useful if you want to notify customers the moment their transactions goes through.
2. Dropped Transactions
The Dropped Transactions Webhook is used to notify you anytime a transaction that was sent through your application gets dropped.
Creating Webhooks
Setting up a webhook is as simple as adding a new URL to your application.
If you want to make your webhooks extra secure, you can verify that they originated from Alchemy by generating a HMAC SHA-256 hash code using your Authentication Token and request body.
1. Find your Authentication Token
Navigate to the top right corner of your Notify page to copy your "Auth Token".
2. Validate the signature received
Every outbound request will contain a hashed authentication signature in the header which is computed by concatenating your auth token and request body then generating a hash using the HMAC SHA256 hash algorithm.
In order to verify this signature came from Alchemy, you simply have to generate the HMAC SHA256 hash and compare it with the signature received.
Example Request Header
POST /yourWebhookServer/push HTTP/1.1Content-Type:application/json;X-Alchemy-Signature:your-hashed-signature
Example Signature Validation Function
functionisValidSignature(request) { consttoken='Auth token provided by Alchemy on the Webhook setup page';constheaders=request.headers;constsignature= headers['x-alchemy-signature']; // Lowercase for NodeJSconstbody=request.body; consthmac=crypto.createHmac('sha256', token) // Create a HMAC SHA256 hash using the auth tokenhmac.update(body,'utf8') // Update the token hash with the request body using utf8constdigest=hmac.digest('hex'); return (signature === digest); // If signature equals your computed hash, return true}