Building Real-Time Event-Driven Apps with FluxRail: Step-by-Step
Learn how to build real-time event-driven applications using FluxRail's Core product. This step-by-step guide covers webhook setup, event subscriptions, and triggering payouts.
Why Event-Driven Architecture Matters
In the world of blockchain and cross-border payments, waiting for a transaction to confirm can feel like watching paint dry. Event-driven architecture flips the script: instead of polling APIs or blocking on confirmations, your application reacts to events as they happen. FluxRail's Core product gives you real-time blockchain event monitoring across 36+ chains, delivered via signed webhooks. In this guide, we'll walk through building a real-time event-driven application step by step, using only the FluxRail REST API.
Step 1: Set Up Your Webhook Endpoint
Before you can receive events, you need a webhook endpoint. This is a URL on your server that FluxRail will call whenever a subscribed event occurs. For development, you can use a service like ngrok to expose your local server. Your endpoint must be publicly accessible and able to handle POST requests with JSON payloads.
FluxRail signs every webhook with HMAC SHA-256 using a secret you provide. Always verify the signature to ensure the request is genuine. Here's a minimal Node.js Express server that listens for webhooks and verifies the signature:
const express = require('express');
const crypto = require('crypto');
const app = express();
app.use(express.json());
const WEBHOOK_SECRET = 'your_webhook_secret';
app.post('/webhook', (req, res) => {
const signature = req.headers['x-fluxrail-signature'];
const payload = JSON.stringify(req.body);
const expected = crypto.createHmac('sha256', WEBHOOK_SECRET).update(payload).digest('hex');
if (signature !== expected) {
return res.status(401).send('Invalid signature');
}
console.log('Received event:', req.body);
res.status(200).send('OK');
});
app.listen(3000, () => console.log('Webhook server running on port 3000'));
Replace your_webhook_secret with a strong secret. You'll use this same secret when registering the webhook with FluxRail.
Step 2: Register Your Webhook with FluxRail
Now, tell FluxRail where to send events. Use the POST /api/v1/webhooks endpoint. You'll need your API key (starts with flux_live_ or flux_test_). For development, use test mode.
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://yourdomain.com/webhook",
"secret": "your_webhook_secret",
"events": ["transfer.completed", "transfer.failed", "payout.completed"]
}'
The response includes an id for the webhook. Save it. You can test the webhook with POST /api/v1/webhooks/{id}/test to ensure connectivity.
Step 3: Subscribe to Blockchain Events
FluxRail Core lets you monitor addresses or contracts. Create a subscription with POST /api/v1/subscriptions. For example, to watch a specific Ethereum address for incoming transfers:
curl -X POST https://api.fluxrail.io/api/v1/subscriptions \
-H "X-API-Key: flux_test_xxx" \
-H "Content-Type: application/json" \
-d '{
"chain": "ethereum",
"address": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
"events": ["transfer"]
}'
You can also subscribe to contract events by providing the contract address and event signature. FluxRail supports bulk creation via POST /api/v1/subscriptions/bulk if you need to monitor many addresses at once.
Step 4: Handle Incoming Events
When a subscribed event occurs, FluxRail sends a POST request to your webhook URL. The payload includes details like transaction hash, block number, from/to addresses, amount, and more. Your server should process it idempotently—use the event ID to avoid duplicates. Here's an example payload for a transfer event:
{
"id": "evt_123",
"type": "transfer",
"chain": "ethereum",
"tx_hash": "0xabc...",
"from": "0x...",
"to": "0x...",
"amount": "1.5",
"token": "ETH",
"block_number": 12345678,
"livemode": false
}
Store the event, trigger business logic (e.g., update user balance, send notification), and respond with 200 OK. If your server fails to respond, FluxRail retries with exponential backoff. You can view delivery attempts via GET /api/v1/webhooks/{id}/deliveries.
Step 5: Trigger Actions Based on Events
Now that you're receiving events, you can build powerful workflows. For instance, when a deposit is detected, automatically initiate a payout using FluxRail Payouts. First, ensure the customer is verified and has a destination set up. Then, on a transfer.completed event for a deposit address, call POST /api/v1/payouts/customers/{id}/send to send funds to a bank account. Remember, you must have an active webhook registered before sending payouts—which you already did in Step 2.
curl -X POST https://api.fluxrail.io/api/v1/payouts/customers/user-101/send \
-H "X-API-Key: flux_test_xxx" \
-H "Content-Type: application/json" \
-d '{
"amount": "100",
"dest_currency": "USD",
"method": "ach",
"destination": {
"account_owner_name": "Jane Doe",
"account_number": "000123456789",
"routing_number": "110000000",
"account_type": "checking"
}
}'
The payout will trigger its own webhooks (payout.pending_approval, payout.completed, etc.), which you can handle similarly.
Step 6: Monitor and Debug
Use FluxRail's dashboard and API to monitor webhook deliveries and event logs. GET /api/v1/events lists recent events, and GET /api/v1/events/stats gives you a summary. If a webhook fails, you can retry delivery with POST /api/v1/events/{id}/retry. For subscriptions, GET /api/v1/subscriptions/{id}/webhook-logs shows delivery attempts specific to that subscription.
Best Practices for Production
- Always verify signatures to prevent spoofing.
- Make handlers idempotent—events may be delivered more than once.
- Respond quickly (within 5 seconds) and process asynchronously if needed.
- Use test mode for development; it's fully simulated with no real funds.
- Monitor webhook health via the deliveries endpoint and set up alerts.
Conclusion
With FluxRail, building real-time event-driven applications is straightforward. You get unified access to blockchain events and payment rails, all through one API. Start with test mode, iterate quickly, and scale to production when ready. For more details, check out the FluxRail documentation.