Bridging Cloudflare Alerts to Any Webhook Endpoint

Cloudflare's Notification Center ships with built-in integrations for a range of popular services. But if the tool you rely on isn't in that list, a generic webhook plus a Cloudflare Worker gives you a straightforward escape hatch. With the newly open-sourced webhook relay Worker, transforming Cloudflare's generic webhook response into any format your service expects is largely a matter of editing the conversion logic in one place.

The idea is simple: configure Cloudflare to fire a generic webhook when an alert triggers, and let a Worker intercept that request, reshape the payload, and forward it to your messaging platform. The example below walks through wiring this up for Rocket Chat.

When Cloudflare sends a generic webhook, the payload follows this schema — the text and data fields vary depending on which alert fired:

{
   "name": "Your custom webhook",
   "text": "The alert text",
   "data": {
       "some": "further",
       "info": [
           "about",
           "your",
           "alert",
           "in"
       ],
       "json": "format"
   },
   "ts": 123456789
}

Rocket Chat, meanwhile, expects its own structure for incoming webhooks:

{
   "text": "Example message",
   "attachments": [
       {
           "title": "Rocket Chat",
           "title_link": "https://rocket.chat",
           "text": "Rocket.Chat, the best open source chat",
           "image_url": "/images/integration-attachment-example.png",
           "color": "#764FA5"
       }
   ]
}

What You'll Need

Before diving in, make sure you have a Cloudflare account and Wrangler, the Workers CLI, installed. The relay Worker itself lives in the cf-webhook-relay repository, so start by cloning it:

  • git clone https://github.com/cloudflare/cf-webhook-relay.git

Setting Up the Relay

With the code local, the remaining setup breaks down into configuring credentials, adapting the payload conversion, and pointing Cloudflare's notifications at the Worker.

First, check the payload format your target tool requires. For Rocket Chat, that's the example structure shown above. You'll need a Rocket Chat account with an incoming webhook integration enabled to receive the requests:

Example configuration of incoming webhook integration in Rocket Chat

Next, store the pieces of configuration the Worker needs. Set an encrypted Wrangler secret for request authentication, and add the Rocket Chat URL as an environment variable. (In this example the secret is not encrypted.) See the Workers environment variables documentation for details:

Workers environment variables configuration in Cloudflare dashboard

Now modify the Worker to accept POST requests. It checks the secret passed as a query parameter for authentication:

if (headers.get("cf-webhook-auth") !== WEBHOOK_SECRET) {
    return new Response(":(", {
        headers: {'content-type': 'text/plain'},
            status: 401
    })
}

With authentication handled, update the payload conversion logic. The Worker takes the incoming notification payload — in Cloudflare's generic format — and reshapes it into the Rocket Chat structure:

let incReq = await request.json()
let msg = incReq.text
let webhookName = incReq.name
let rocketBody = {
    "text": webhookName,
    "attachments": [
        {
            "title": "Cloudflare Webhook",
            "text": msg,
            "title_link": "https://cloudflare.com",
            "color": "#764FA5"
        }
    ]
}

Then configure the Worker to POST the converted payload to the Rocket Chat webhook URL:

const rocketReq = {
    headers: {
        'content-type': 'application/json',
    },
    method: 'POST',
    body: JSON.stringify(rocketBody),
}
const response = await fetch(
    ROCKET_CHAT_URL,
    rocketReq,
)
const res = await response.json()
console.log(res)
return new Response(":)", {
    headers: {'content-type': 'text/plain'},
})

Deploying and Connecting

Once the code is ready, set the deployment configuration in wrangler.toml and publish. The Worker will show up in the Cloudflare dashboard, where you can manage and monitor it with the usual Workers tooling:

View and Manage workers in Cloudflare dashboard
Monitoring workers in Cloudflare dashboard

Next, register the Worker's URL as a generic webhook under notification destinations in the Cloudflare dashboard — see the generic webhooks documentation for the exact steps:

Webhooks destination under Destinations in Notifications tab
Workers URL as a generic webhook in Cloudflare dashboard example

Finally, create a notification and choose that generic webhook as the destination, per the notifications guide:

Example of notification creation

Once the Worker is running, every Cloudflare notification routed to that generic webhook will flow through to Rocket Chat. The same pattern works for any communication service that exposes an incoming webhook — just swap out the payload transform.

Sample webhook notification in Rocket Chat