Mounting
Connect the client to your server or runtime.
Discord sends requests to two URLs, which you set on the Developer Portal:
- The Interactions Endpoint URL, handled by the client itself.
- The Webhook Events URL, handled by
client.getWebhookEventHandler(). It's optional, see Webhook events.
Both are handlers with one method, handle(), which takes a request and returns the response to send. The package also includes two adapters for the most common signatures:
| Adapter | Signature | Use it with |
|---|---|---|
toNodeListener | (req: IncomingMessage, res: ServerResponse) => Promise<void> | node:http, Express |
toFetchHandler | (request: Request, context?: { waitUntil?(promise): void }) => Promise<Response> | Hono, Cloudflare Workers, Bun, Deno, Next.js |
Both answer non-POST requests with 405.
Discord's signature is checked against the raw request body. Make sure nothing parses the body before the client sees it, like express.json() or Fastify's default JSON parser.
node:http
import { } from 'node:http';
import { } from '@nyx-discord/http';
const = ();
const = (.());
((, ) => {
const =
. === '/interactions' ?
: . === '/webhook-events' ?
: null;
if (!) return void .(404).();
(, ).(.);
}).(3000);Express
import { } from '@nyx-discord/http';
// no express.json() on these routes
.('/interactions', ());
.('/webhook-events', (.()));Fastify
Fastify parses JSON by default, so register a parser that keeps the body as a Buffer, and call handle() directly. Passing Fastify's request as raw makes it available to your handlers through TypedFields.HttpRequest:
.('application/json', { : 'buffer' }, (, , ) => (null, ));
const = [
['/interactions', ],
['/webhook-events', .()],
] as ;
for (const [, ] of ) {
.(, async (, ) => {
const = await .({
: . as ,
: .['x-signature-ed25519'],
: .['x-signature-timestamp'],
: ,
});
return .(.).(.).(.);
});
}Hono
import { } from '@nyx-discord/http';
const = ();
const = (.());
.('/interactions', () => (..));
.('/webhook-events', () => (..));When Hono runs on Cloudflare Workers, pass c.executionCtx as the second argument too, so the worker stays alive until your handlers finish.
Cloudflare Workers
Pass the worker's ctx as the handler's second argument, so the worker stays alive until your handlers finish (see Serverless platforms):
import { } from '@nyx-discord/http';
const = ();
const = (.());
export default {
(: , : unknown, : ) {
const { } = new (.);
const = === '/webhook-events' ? : ;
return (, );
},
};On Workers, your secrets only arrive with the first request, so the client can't be created when the module loads. See Cloudflare Workers for a complete setup.
Bun and Deno
Both accept a default export with a fetch handler:
import { } from '@nyx-discord/http';
const = ();
const = (.());
export default {
(: ) {
const { } = new (.);
return === '/webhook-events' ? () : ();
},
};Next.js
Use a route handler for each URL, passing after() as waitUntil, so the function stays alive until your handlers finish:
import { } from 'next/server';
import { } from '@nyx-discord/http';
const = ();
export const = (: ) => (, { : });import { } from 'next/server';
import { } from '@nyx-discord/http';
const = (.());
export const = (: ) => (, { : });Anything else
Call handle() with the raw body (a string or Uint8Array) and the two signature headers, then send back what it returns. The webhook event handler works the same way:
const = await .({
: ,
: ['x-signature-ed25519'],
: ['x-signature-timestamp'],
: , // optional, for TypedFields.HttpRequest
});
.; // the status code
.; // the headers to set
.; // the body, empty when there's none
.; // settles once the handlers finish, for waitUntil()Last updated on
