Function Signature
Description
Creates a middleware function for use in Express-compatible web servers for verifying Event Webhooks from Discord. Unlike interaction webhooks, event webhooks are asynchronous and expect a204 No Content response immediately, while the event is processed after the response is sent.
Parameters
The public key from the Discord developer dashboard. This is used to verify the Ed25519 signature of incoming webhook event requests.
Returns
An Express middleware function that validates incoming Discord webhook event requests.
Behavior
The middleware performs the following actions:- Signature Validation: Extracts
X-Signature-Ed25519andX-Signature-Timestampheaders and validates the request using theverifyKeyfunction - Invalid Signature Handling: Returns
401 Unauthorizedif the signature is missing or invalid - PING Auto-Response: Automatically responds with
204 No Contentfor Discord’s PING webhook events (used for initial endpoint verification) - Immediate Response: Returns
204 No Contentfor valid webhook events before processing - Async Processing: Calls
next()AFTER sending the response, allowing you to process the event asynchronously - Body Parsing: Parses the JSON body and attaches it to
req.bodybefore callingnext()
Usage Example
Basic Setup
With Event Type Handling
With Error Handling
Important Notes
Unlike
verifyKeyMiddleware, this middleware sends the HTTP response BEFORE calling next(). Your handler code runs asynchronously after Discord has already received the 204 No Content response.If
req.body has been modified by other middleware, the function will attempt to reconstruct the raw buffer, but this is risky and may fail. The middleware will log a warning in this case.Response Behavior
| Scenario | Status Code | Body | next() Called |
|---|---|---|---|
| Missing/invalid signature | 401 | [discord-interactions] Invalid signature | No |
| PING event | 204 | Empty | No |
| Valid webhook event | 204 | Empty | Yes (after response) |
Key Differences from verifyKeyMiddleware
- Response Timing: Sends
204 No Contentimmediately, then processes the event - PING Handling: Returns
204for PING (vs200with PONG JSON for interactions) - Use Case: For asynchronous event notifications (vs synchronous interaction responses)
- Processing Model: Fire-and-forget event processing (vs request-response for interactions)
Error Handling
The middleware will return a401 Unauthorized response with the message [discord-interactions] Invalid signature in the following cases:
- Missing
X-Signature-Ed25519orX-Signature-Timestampheaders - Invalid signature that fails verification
- Malformed request data
See Also
- verifyKey - Low-level signature verification function
- verifyKeyMiddleware - Middleware for interaction webhooks
- WebhookType - Enum of webhook types
- WebhookEventType - Enum of webhook event types