Skip to content

Create Webhooks

This guide helps you create webhooks using the Chaingateway API. It includes details on writing a basic webhook receiver, subscribing to a webhook, and handling Chain-specific parameters.

Our webhook system works as an Instant Payment Notification system. It can be used to notify your software of an incoming payment or token receipt. It consists of two parts of code:

The Webhook: This is the endpoint URL where the webhook notifications will be sent. You need to provide this URL to the Chaingateway API when subscribing to the webhook. You also need to specify the filter parameters like sender, receiver, contractaddress, token_id and type (transaction type). If an event on the blockchain happens matching those criterias, we will send out a webhook to the url you specified.

The Webhook Receiver: This is the code that runs on your server and listens for incoming webhook notifications. It receives the webhook payload and performs the necessary actions in your software based on the received data.

To set up the webhook system, you need to implement the webhook receiver code on your server and configure the webhook URL in the Chaingateway API. Once set up, whenever a payment or token receipt occurs, the Chaingateway API will send a POST request to your webhook URL, triggering the webhook receiver code and allowing your software to take appropriate actions.

A webhook receiver is a server endpoint that listens for incoming HTTP POST requests from ChainGateway. Below are examples of basic webhook receivers in different programming languages.

const express = require('express');
const app = express();
const port = 3000;
app.use(express.json());
app.post('/webhook', (req, res) => {
console.log('Received webhook:', req.body);
res.status(200).send('Webhook received');
});
app.listen(port, () => {
console.log(`Webhook receiver listening at http://localhost:${port}`);
});

Create a Webhook to subscribe to a Blockchain Event

Section titled “Create a Webhook to subscribe to a Blockchain Event”

To subscribe to a webhook, you need to send a request to the ChainGateway API with the appropriate parameters.

  • url (required): The URL of your webhook receiver endpoint. Must be a valid URL.
  • from: The sender address (optional, at least one of from or to is required).
  • to: The receiver address (optional, at least one of from or to is required).
  • contractaddress: The contract address of the token (optional).
  • token_id: Required when neither from nor to are present.
  • type: The type of transaction (optional). Possible values: TRX, TRC10, TRC20, TRC721.

Replace YOUR_API_TOKEN with your actual API token.

Terminal window
curl --request POST \
--url https://api.chaingateway.io/v2/webhooks \
--header 'Accept: application/json' \
--header 'content-type: application/json' \
--header 'Authorization: YOUR_API_TOKEN' \
--data '{
"url": "http://yourdomain.com/webhook",
"from": "TXSenderAddress",
"to": "TXReceiverAddress",
"contractaddress": "0xTokenContractAddress",
"token_id": "12345",
"type": "TRC20"
}'
  • Ensure that your webhook receiver endpoint is publicly accessible and can handle HTTP POST requests.
  • Secure your webhook endpoint by validating the source of incoming requests and ensuring the data integrity.
  • Chaingateway does not store sensitive information such as the token. Therefore, store this information securely.
  • For obtaining an API key and learning about authorization, please refer to the Getting Started section of the Chaingateway documentation.