Skip to Content
nyx logonyx guide
Vercel

Vercel

Run an HTTP bot on Vercel Functions.

Reading time:1 min read

This page assumes you've read Serverless, and have a createBot() function like the one there. If your bot lives in a Next.js app, use a route handler instead.

Installing

In your project, install the bot and Vercel's helpers:

npm i @nyx-discord/http @discordjs/core @vercel/functions

Writing the function

Every file in api/ is a function, served at its path. Your secrets are in process.env when the module loads, so the bot can be created right away. Pass waitUntil from @vercel/functions, so the function stays alive until your handlers finish:

api/interactions.ts
import {  } from '@nyx-discord/http';
import {  } from '@vercel/functions';

const  = (. as , false).(() =>
  (.()),
);

export async function (: ) {
  return (await )(, {  });
}

For webhook events, add an api/webhook-events.ts that does the same with toFetchHandler(bot.getClient().getWebhookEventHandler()).

Running locally

Put your secrets in .env.local:

APP_ID=...
DISCORD_PUBLIC_KEY=...
TOKEN=...
npx vercel dev

Test it without Discord at http://localhost:3000/api/interactions, or expose it with a tunnel and set the tunnel's URL as your app's Interactions Endpoint URL.

Deploying

Add your secrets to the project, for each of APP_ID, DISCORD_PUBLIC_KEY and TOKEN:

npx vercel env add TOKEN production
npx vercel deploy --prod

Remember to deploy your commands too.

Setting the endpoint

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

Edit on GitHub

Last updated on

On this page