Building Real-Time Event-Driven Applications with FluxRail
Learn how to build real-time event-driven applications using FluxRail's Core product. This step-by-step guide covers webhook registration, subscription creation, secure event handling, and integration with other FluxRail services.
Why Event-Driven Architecture Matters
In the fast-paced world of blockchain and cross-border payments, reacting to events as they happen is no longer a luxury—it's a necessity. Whether you're building a wallet that needs to show incoming transactions instantly, a trading bot that triggers on specific contract events, or a payroll system that disburses funds upon confirmation, real-time event handling is the backbone of responsive applications. FluxRail's Core product provides a robust, scalable solution for monitoring blockchain events across 36+ chains and delivering them to your application via signed webhooks.
This guide will walk you through the process of setting up real-time event-driven applications using FluxRail's REST API. We'll cover subscribing to address and contract events, handling webhook deliveries securely, and building a simple listener to process events. All examples use curl or standard HTTP requests—no SDKs required.
Prerequisites
- A FluxRail account (sign up at fluxrail.io)
- Your API key (test or live) from the dashboard
- A publicly accessible endpoint to receive webhooks (for local development, consider using ngrok)
- Basic familiarity with REST APIs and JSON
Step 1: Register a Webhook Endpoint
Before you can receive events, you need to tell FluxRail where to send them. Webhooks are signed with HMAC SHA-256 using a secret you provide, ensuring authenticity. Let's register a webhook:
curl -X POST https://api.fluxrail.io/api/v1/webhooks \
-H "X-API-Key: flux_test_xxx" \
-H "Content-Type: application/json" \
-d '{
"url": "https://your-app.com/webhooks/fluxrail",
"secret": "your_webhook_secret"
}'The response will include an id for the webhook. Keep this handy. You can also test the webhook using POST /api/v1/webhooks/{id}/test to ensure connectivity.
Step 2: Create Subscriptions
Subscriptions define which blockchain events you want to monitor. You can subscribe to specific addresses (e.g., a user's wallet) or contract events (e.g., a token transfer). For example, to monitor incoming USDT transfers to a specific address on Ethereum:
curl -X POST https://api.fluxrail.io/api/v1/subscriptions \
-H "X-API-Key: flux_test_xxx" \
-H "Content-Type: application/json" \
-d '{
"type": "address",
"chain": "ethereum",
"address": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
"webhook_id": "wh_12345"
}'For contract events, specify the contract address and the event signature (topic0). FluxRail supports both predefined event types and custom topics. You can create multiple subscriptions in bulk using POST /api/v1/subscriptions/bulk.
Step 3: Handle Webhook Deliveries Securely
When an event occurs, FluxRail sends a POST request to your webhook URL with a JSON payload. Each request includes an X-FluxRail-Signature header containing the HMAC SHA-256 signature of the raw request body, computed with your secret. Always verify this signature to prevent spoofing.
Here's a Node.js example using Express to verify and process events:
const express = require('express');
const crypto = require('crypto');
const app = express();
app.use(express.json({ verify: (req, res, buf) => { req.rawBody = buf; } }));
app.post('/webhooks/fluxrail', (req, res) => {
const signature = req.headers['x-fluxrail-signature'];
const expected = crypto.createHmac('sha256', 'your_webhook_secret')
.update(req.rawBody)
.digest('hex');
if (signature !== expected) {
return res.status(401).send('Invalid signature');
}
const event = req.body;
console.log('Received event:', event.type);
// Process event based on type
if (event.type === 'address.activity') {
// Handle incoming transaction
}
res.status(200).send('OK');
});
app.listen(3000, () => console.log('Listening on port 3000'));FluxRail retries failed deliveries with exponential backoff, so ensure your endpoint responds with a 2xx status quickly. You can view delivery logs via GET /api/v1/webhooks/{id}/deliveries.
Step 4: Retrieve Events and Stats
In addition to webhooks, you can query historical events using GET /api/v1/events. Filter by chain, address, contract, or time range. For analytics, GET /api/v1/events/stats provides aggregated counts. This is useful for reconciliation or building dashboards.
curl "https://api.fluxrail.io/api/v1/events?chain=ethereum&address=0x742d35Cc6634C0532925a3b844Bc454e4438f44e&limit=10" \
-H "X-API-Key: flux_test_xxx"Step 5: Leverage Other FluxRail Products
Once you're handling events, you can integrate other FluxRail products to create powerful workflows. For instance, upon detecting an incoming stablecoin deposit, you might trigger a gas-free transfer using the Pay product, or convert assets via Liquidity, or even initiate a fiat payout. All through the same API and webhook system.
Remember, for Pay transfers and Payouts, an active webhook is required—so you're already set!
Best Practices
- Idempotency: Use the event ID to deduplicate processing.
- Security: Always verify webhook signatures and use HTTPS.
- Scalability: Design your webhook handler to be stateless and horizontally scalable.
- Monitoring: Set up alerts for failed deliveries and use FluxRail's stats endpoints.
Conclusion
FluxRail's Core product empowers you to build real-time, event-driven applications with minimal effort. By following these steps, you can subscribe to blockchain events, securely receive webhooks, and integrate with FluxRail's payment and liquidity products to create end-to-end solutions. Start building today—your users will thank you for the instant responsiveness.
For more details, explore the FluxRail documentation.