This example demonstrates how to integrate webhooks with the Runes SDK for real-time event notifications and monitoring.
First, initialize the SDK with webhook configuration:
import { RunesSDK } from '../src';
import { WebhookEventType } from '../src/types/webhook.types';
const sdk = new RunesSDK({
host: 'your-node-url',
username: 'your-username',
password: 'your-password',
// Default webhook configuration
webhookConfig: {
url: 'https://default-webhook.com',
events: [WebhookEventType.TRANSACTION_CONFIRMED],
retryCount: 3,
timeout: 5000
}
});
Register multiple webhooks for different event types:
// Transaction webhook
const webhook1Id = 'transaction-webhook';
await sdk.registerWebhook(webhook1Id, {
url: 'https://your-webhook-1.com',
events: [
WebhookEventType.TRANSACTION_CONFIRMED,
WebhookEventType.TRANSACTION_FAILED
],
retryCount: 3,
timeout: 5000
});
// Monitoring webhook
const webhook2Id = 'monitoring-webhook';
await sdk.registerWebhook(webhook2Id, {
url: 'https://your-webhook-2.com',
events: [
WebhookEventType.MEMPOOL_FULL,
WebhookEventType.HIGH_FEES
],
retryCount: 5,
timeout: 10000
});
Process a transaction and receive webhook notifications:
// Example transaction
const transaction = {
from: 'sender-address',
to: 'receiver-address',
amount: '100000',
fee: '1000'
};
// Validate and send transaction
const validationResult = await sdk.validateTransaction(transaction);
if (validationResult.isValid) {
const txid = 'transaction-id';
// Watch transaction and receive webhook notifications
await sdk.watchTransaction(txid);
}
Example webhook payloads for different events:
{
"type": "TRANSACTION_CONFIRMED",
"data": {
"txid": "transaction-id",
"confirmations": 6,
"timestamp": 1641234567890
}
}
{
"type": "HIGH_FEES",
"data": {
"currentFee": "5000",
"recommendedFee": "2000",
"timestamp": 1641234567890
}
}
Unregister webhooks when they are no longer needed:
await sdk.unregisterWebhook(webhook1Id);
await sdk.unregisterWebhook(webhook2Id);
The example demonstrates:
- Multiple webhook registration
- Different event type handling
- Configurable retry and timeout settings
- Transaction monitoring with webhooks
- Webhook payload structure
- Proper webhook cleanup
When implementing webhooks:
- Use HTTPS endpoints
- Implement proper authentication
- Handle webhook retries
- Set appropriate timeouts
- Validate webhook payloads
- Monitor webhook delivery status
To run this example:
-
Set up your environment variables:
export NODE_URL=your-node-url export USERNAME=your-username export PASSWORD=your-password
-
Run the example:
ts-node webhook-integration.ts
Remember to:
- Secure webhook endpoints with HTTPS
- Validate webhook signatures
- Implement rate limiting
- Monitor for failed deliveries
- Keep webhook URLs confidential