Skip to content

Webhooks from other services

A webhook route accepts HTTP requests from an outside API, such as GitHub, Stripe, or a service of your own, and can post to Discord in answer. Register it on the app with an exact path, a verifier, and a handler:

import { Discord, verifyHmac } from "discord0";
app.webhook(
"/webhooks/github",
{
verify: verifyHmac({
secret: () => process.env.GITHUB_WEBHOOK_SECRET,
header: "x-hub-signature-256",
prefix: "sha256=",
}),
},
(c) => {
const push = c.json<{ ref: string; repository: { full_name: string } }>();
c.waitUntil(
c.send(
process.env.DEPLOYS_CHANNEL_ID!,
`Pushed to ${push.ref} in ${push.repository.full_name}`,
),
);
return new Response(null, { status: 202 });
},
);

discord0 deploy routes the path to the app on every host. Hosted adapters print each Webhook endpoint URL after the Interactions endpoint line, and the Node.js and Bun servers log each webhook URL when they start. Give that URL to the outside service.

/interactions stays Discord’s signed interactions endpoint. A webhook route never reaches it, and Discord’s Ed25519 check never runs on a webhook route.

verify runs before the handler, with the request and its raw body. It’s required. Pass false only for a route that checks requests itself or needs no check.

The verifier returns The response
true The handler runs.
false 401 Unauthorized
A Response That response.
It throws 401, and it’s logged

verifyHmac() checks HMAC signatures, in constant time. Set header to the header that carries the signature, and prefix to any text before it. encoding is hex (the default) or base64, and algorithm is SHA-256 (the default), SHA-1, or SHA-512. Pass secret as a function to read it on each request. While the secret is unset or empty, requests answer 503.

Set payload when the service signs more than the raw body. It receives the request and returns the signed string or bytes. Slack, for example, signs v0:<timestamp>:<body>.

A service whose signature header holds several values, such as Stripe’s t=<timestamp>,v1=<signature>, needs a verifier of its own. Its argument has body (the raw bytes), text(), json(), headers, method, and url, as the handler’s c does.

The handler receives c, with the members above and these:

Member Description
c.waitUntil(promise) Keeps work running after the response is sent, for as long as the host allows. A rejection is logged.
c.send(id, msg) Posts a message to a channel as the bot and returns it.
c.executeWebhook(hook, msg) Posts through a Discord webhook, by its URL or { id, token }, and returns the message. No bot token needed.

Return a Response, or nothing for 204 No Content. A handler that throws answers 500, and the error is logged.

Outside services often wait only a few seconds for an answer, so answer first and post with c.waitUntil, as above. waitUntil keeps the post alive the same way it keeps deferred interaction replies alive.

Paths are exact and static, such as /webhooks/github. Each segment uses letters, digits, ., _, ~, and -, with no parameters, query, or trailing slash. Each path is unique across the app and its mounted sub-apps. It can’t be the interactions path (/interactions by default) or the events path (/events by default). Requests to any other path answer 404.

Routes accept POST unless you set methods, such as ["GET", "POST"] for a service that checks the URL with a GET first. Other methods answer 405. Bodies are limited to the app’s maxBodyBytes (1 MiB by default), and a larger body answers 413. Set maxBodyBytes on the route to change the limit.

app.webhook(
{ path: "/hooks/build", verify: false, methods: ["GET", "POST"] },
handler,
);
Adapter Routing
Cloudflare The Worker receives every path.
Vercel A rewrite per path in vercel.json. Paths under /api fail.
Netlify Each path joins the function’s config.path. Paths under /.netlify fail.
AWS The function URL receives every path.
Node.js The server receives every path, and logs each webhook URL.
Bun The server receives every path, and logs each webhook URL.

discord0 deploy --dry-run lists each route it generated.