Skip to content

Netlify Functions

@discord0/adapter-netlify deploys your app as a Netlify Function with the Netlify CLI.

Terminal window
npx discord0 init my-bot --adapter netlify

.discord0/netlify contains:

  • functions/interactions.mjs, which serves /interactions and each webhook path,
  • a netlify.toml,
  • a placeholder public/index.html, and
  • a package.json that sets engines.node to 22.x.

The Netlify CLI asks you to log in and link a project on the first deploy. Deploys are drafts unless you pass --prod:

Terminal window
npx discord0 deploy --prod

Use the production URL for your Interactions Endpoint URL.

Add DISCORD_PUBLIC_KEY and DISCORD_BOT_TOKEN in Project configuration → Environment variables, or from .discord0/netlify:

Terminal window
cd .discord0/netlify
npx netlify-cli env:set DISCORD_PUBLIC_KEY <public key>
npx netlify-cli env:set DISCORD_BOT_TOKEN <bot token>

Variables apply to new deploys, so deploy again after setting them.

The adapter passes deferred work to the function’s context.waitUntil().

When you leave autoDeferAfterMs unset, the adapter defaults it to 500ms. A cold start can use most of Discord’s 3-second window before the handler runs, and the lower default still leaves room for the acknowledgement. Set autoDeferAfterMs on new Discord() to override. See Deferred work.

  • 60 seconds in total. A function can run until its execution time limit, and that limit includes all waitUntil work. Synchronous functions have a 60-second limit, which you can’t change. (Default values)
  • Netlify bills and logs the full duration, until the deferred work finishes.

Netlify’s background functions run for up to 15 minutes, but they answer with an immediate 202 and no body, which Discord doesn’t accept as an interaction response. For longer jobs, trigger a background function from your handler and edit the reply from there.

Each scheduled task becomes a scheduled function, functions/task-<name>.mjs. Scheduled functions run in UTC, on published deploys only, and stop after 30 seconds.