Deploying a Discord Bot as a Vercel Serverless Function
My last post talked about Discord slash commands and some of the benefits of the new HTTP API, but I didn't spend much time talking about hosting. Using HTTP instead of the Gateway for your bot unlocks some cool new options, like hosting it as a serverless function through a provider like Vercel!
Writing a Vercel Serverless Function Bot
We only need to create two files to get up and running - a package.json and an api/index.js page.
Some people have reported that the raw-body package does not work for them. If your function times out while trying to verify a Discord interaction, try using the following snippet instead:
Make sure you add your Application ID, Token, and Public Key as project environment variables or secrets (for this example, call them APPLICATION_ID, TOKEN, and PUBLIC_KEY).
And that's it! You can deploy this project to Vercel, get a project URL, and plug it straight into your Discord application (make sure you add /api to it). Using Vercel as a host for my new bots has worked wonderfully so far.