Webhooks Deep Dive
Advanced webhook handling and best practices
Webhooks Deep Dive
Advanced guide to implementing production-ready webhook handling.
Overview
Webhooks allow Koywe to send real-time notifications about order status changes to your server.
Why Use Webhooks?
Benefits:
- Real-time order status updates
- No polling required
- Scalable architecture
- Reliable delivery with retries
Webhook Signature Verification
Critical: Always verify webhook signatures to ensure authenticity.
Verification Process
Common Mistakes:
- Using
express.json()instead ofexpress.raw()- this modifies the body - Using parsed JSON for signature calculation - must use raw body
- Wrong secret - verify youโre using webhook secret, not API secret
Idempotency
Handle duplicate webhook deliveries:
Idempotent Processing
Event Types
Order Events
Event Payload Structure
Event Handling
Complete Event Handler
Production Handler
Response Times
Respond to webhooks quickly (< 5 seconds):
Async Processing
Best Practice: Respond immediately (200 OK) and process the webhook asynchronously. This prevents timeouts and ensures reliable delivery.
Retry Mechanism
Koywe automatically retries failed webhooks:
- Retry Schedule: 1 min, 5 min, 15 min, 1 hour, 6 hours, 24 hours
- Max Retries: 6 attempts over 24 hours
- Retry Condition: Non-200 response or timeout
Handling Retries
Idempotent Handler
Testing Webhooks
Local Testing with ngrok
Using webhook.site
For quick testing without code:
- Visit https://webhook.site
- Copy your unique URL
- Use as webhook URL in API
- View incoming webhooks in browser
Production Checklist
Before going live:
- Signature verification implemented
- Idempotency handling in place
- Quick response times (< 5 seconds)
- Async processing implemented
- Error logging configured
- Event persistence to database
- Monitoring and alerts setup
- Tested with ngrok/webhook.site
- Webhook endpoint is HTTPS
- Firewall allows Koywe IPs
Monitoring and Debugging
Webhook Logs
Query webhook delivery attempts:
Node.js
Replay Webhooks
Replay a webhook manually:
Replay Event
Security Best Practices
Critical Security Measures:
- Always verify signatures - Never skip this step
- Use HTTPS - Webhooks over HTTP are insecure
- Validate event structure - Check required fields exist
- Use webhook secret - Donโt use API secret
- Log all webhooks - For audit trail
- Rate limit your endpoint - Protect against attacks