Skip to Content
nyx logonyx guide
Netlify

Netlify

Run an HTTP bot on Netlify Functions.

Reading time:1 min read

This page assumes you've read Serverless, and have a createBot() function like the one there.

Installing

In your project, install the bot and Netlify's types:

npm i @nyx-discord/http @discordjs/core
npm i -D @netlify/functions

Writing the function

Functions go in netlify/functions/, and config.path sets the URLs they're served at. Your secrets are in process.env when the module loads, so the bot can be created right away. Netlify's context has a waitUntil, so pass it as is to keep the function alive until your handlers finish:

netlify/functions/discord.mts
import {  } from '@nyx-discord/http';
import type { ,  } from '@netlify/functions';

const  = (. as , false).(() => {
  const  = .();
  return {
    : (),
    : (.()),
  };
});

export default async (: , : ) => {
  const { ,  } = await ;
  const {  } = new (.);
  const  =  === '/webhook-events' ?  : ;

  return (, );
};

export const :  = {
  : ['/interactions', '/webhook-events'],
};

Running locally

Put your secrets in .env:

APP_ID=...
DISCORD_PUBLIC_KEY=...
TOKEN=...
npx netlify dev

Test it without Discord at http://localhost:8888/interactions, or expose it with npx netlify dev --live and set that URL as your app's Interactions Endpoint URL.

Deploying

Add your secrets to the site, from your .env file:

npx netlify env:import .env
npx netlify deploy --prod

Remember to deploy your commands too.

Setting the endpoint

On the Developer Portal, set your app's Interactions Endpoint URL to https://<site>.netlify.app/interactions, and its Webhook Events URL (if you use them) to https://<site>.netlify.app/webhook-events.

Edit on GitHub

Last updated on

On this page