Skip to content

Webhook events from Discord

Discord can send some events to your app as signed HTTP requests, with no Gateway connection. These are webhook events, such as APPLICATION_AUTHORIZED when someone installs the app, and ENTITLEMENT_CREATE when someone buys a SKU. On a serverless host, they’re the only way to receive events. Handle one with app.event():

app.event("APPLICATION_AUTHORIZED", (c) => {
const where = c.data.guild ? `in ${c.data.guild.name}` : "to their account";
return c.send(
process.env.INSTALLS_CHANNEL_ID!,
`${c.data.user.username} installed the app ${where}`,
);
});
app.event("ENTITLEMENT_CREATE", (c) => {
console.log(`User ${c.data.user_id} bought SKU ${c.data.sku_id}`);
});

c.data is typed by the event type. Types without typed data, such as LOBBY_MESSAGE_CREATE, are accepted too, with unknown data.

Discord sends webhook events to their own URL, apart from the Interactions Endpoint URL. The app serves them at /events once it has an app.event() handler.

  1. Deploy. discord0 deploy routes /events on every host. On a hosted adapter, it prints the URL next to the Interactions endpoint:

    Interactions endpoint: https://my-bot.example.workers.dev/interactions
    Webhook Events URL: https://my-bot.example.workers.dev/events

    On Node.js and Bun, serve() logs the URL when the server starts.

  2. In the Developer Portal, open your app’s Webhooks page, paste that URL into Endpoint URL, and save. Discord sends a PING, which the app answers.

  3. Turn on Events, and enable each event type you handle. Deploy prints the list.

For local development, discord0 dev logs the events URL too. Give Discord the same URL through your HTTPS tunnel, as for interactions.

To serve events at another path, set eventsPath:

const app = new Discord({ eventsPath: "/discord/events" });
  • Each request is checked against the app’s public key, like an interaction, with the same Ed25519 signature and timestamp window. Requests that fail answer 401.
  • Discord’s PING answers 204.
  • An event answers 204 at once, before its handlers run. They then finish through the host’s waitUntil, as deferred work does, so the same limits apply on each host.
  • An event type may have several handlers, such as one per sub-app. They run together, and an event with no handler is still answered 204.
  • The app logs a handler’s error. Discord retries only events that go unanswered, so it doesn’t resend an event whose handler failed.
Member Description
c.type The event type, such as APPLICATION_AUTHORIZED.
c.data The event’s data, typed by its type.
c.timestamp When the event occurred, as a Date.
c.applicationId The ID of the app the event is for.
c.event The event object as Discord sent it.
c.send() Posts a message to a channel as the bot, and returns it.
c.executeWebhook() Posts through a Discord webhook, by its URL or { id, token }.

app.dispatchEvent() runs the handlers for an event body, skipping the signature check, and resolves when they finish. Handler errors reject:

await app.dispatchEvent({
version: 1,
application_id: "123",
type: 1,
event: {
type: "APPLICATION_AUTHORIZED",
timestamp: new Date().toISOString(),
data: {
user: {
id: "456",
username: "tester",
discriminator: "0",
global_name: null,
avatar: null,
},
scopes: ["applications.commands"],
},
},
});