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.
package.jsonjson
1{
2"name":"my-bot",
3"version":"1.0.0",
4"dependencies":{
5"discord-interactions":"^2.0.2",
6"raw-body":"^2.4.1"
7}
8}
The only two dependencies here are used to verify incoming Discord requests (an API requirement).
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.