Webhook Router
Receive webhooks and route different events to different destinations.
This example uses the http() adapter and choice() operation which are planned for a future release. It serves as a reference for the intended API design. Check the Adapters documentation and Operations documentation for currently available features.
import { craft, http, log, fetch } from '@routecraft/routecraft'
export default craft()
.id('webhook-router')
.from(http({ port: 3000, path: '/webhook', method: 'POST' }))
.tap(log())
.choice([
{
when: data => data.event === 'user.signup',
then: craft().to(fetch({
url: 'https://api.sendgrid.com/send-welcome'
}))
},
{
when: data => data.event === 'payment',
then: craft().to(fetch({
url: 'https://analytics.company.com/track'
}))
},
{
when: () => true,
then: craft().tap(() => console.log('Unknown event'))
}
])
Input Data
HTTP POST requests to http://localhost:3000/webhook:
{ "event": "user.signup", "data": { "email": "user@example.com" } }
{ "event": "payment", "data": { "amount": 100, "currency": "USD" } }
{ "event": "unknown.event", "data": { "foo": "bar" } }
What It Does
- Listens for POST requests at
/webhookon port 3000 - Logs all incoming webhook data
- Routes events based on
eventfield:user.signup→ sends to SendGrid APIpayment→ sends to Analytics API- Unknown events → logs to console
Result
- user.signup webhook → HTTP POST to
https://api.sendgrid.com/send-welcome - payment webhook → HTTP POST to
https://analytics.company.com/track - unknown.event webhook → Console log: "Unknown event"
All webhooks are also logged via tap(log()).