Hosting a Reddit API Discord app on Cloudflare Workers
When building Discord apps, your app can receive common events from the client as webhooks when users interact with your app through interactions like application commands or message components. Discord will send these events to a pre-configured HTTPS endpoint (called an Interactions Endpoint URL in an app’s configuration) as a JSON payload with details about the event. This tutorial walks through building a Discord app powered byr/aww using JavaScript:
 All of the code for this app can be found on GitHub.
All of the code for this app can be found on GitHub.
Features and technologies used
- Discord Interactions API (specifically slash commands)
- Cloudflare Workers for hosting
- Reddit API to send messages back to the user
Creating an app on Discord
To start, we’ll create the app through the Discord Developer Dashboard:- Visit https://discord.com/developers/applications
- Click New Application, and choose a name
- Copy your Public Key and Application ID, and put them somewhere locally (we’ll need these later)
 
- Now click on the Bot tab on the left sidebar.
- Grab the tokenfor your bot, and store it somewhere safe (I like to put these tokens in a password manager like 1password or lastpass).
For security reasons, you can only view your bot token once. If you misplace your token, you’ll have to generate a new one.
Adding bot permissions
Now we’ll configure the bot with permissions required to create and use slash commands, as well as send messages in channels.- Click on the OAuth2tab, and choose theURL Generator. Click thebotandapplications.commandsscopes.
- Check the boxes next to Send MessagesandUse Slash Commands, then copy theGenerated URL.
 
- Paste the URL into the browser and follow the OAuth flow, selecting the server where you’d like to develop and test your bot.
Creating your Cloudflare Worker
Cloudflare Workers are a convenient way to host Discord apps due to the free tier, simple development model, and automatically managed environment (no VMs!).When using Cloudflare Workers, your app won’t be able to access non-ephemeral CDN media. For example, trying to fetch an image like 
https://cdn.discordapp.com/attachments/1234/56789/my_image.png would result in a 403 error. Cloudflare Workers are still able to access ephemeral CDN media.- Visit the Cloudflare Dashboard
- Click on the Workerstab, and create a new service using the same name as your Discord bot
- Make sure to install the Wrangler CLI and set it up.
Storing secrets
The production service needs access to some of the information we saved earlier. To set those variables, run:For example, if my URL was 
https://discord.com/channels/123456/789101112, the Guild ID is the first number—in this case 123456.Running locally
This depends on the beta version of the 
wrangler package, which better supports ESM on Cloudflare Workers.Project structure
A brief look at the cloned app’s project structure:Registering commands
Before testing our app, we need to register our desired slash commands. For this app, we’ll have a/awwww command, and a /invite command. The name and description for these are kept separate in commands.js:
register.js. Commands can be registered globally, making them available for all servers with the app installed, or they can be registered on a single server.
In this example - we’ll just focus on global commands:
Running the server
This command needs to be run locally, once before getting started:Setting up ngrok
When a user types a slash command, Discord will send an HTTP request to a public endpoint. During local development this can be a little challenging, so we’re going to use a tool calledngrok to create an HTTP tunnel.
 This is going to bounce requests off of an external endpoint, and forward them to your machine. Copy the HTTPS link provided by the tool. It should look something like
This is going to bounce requests off of an external endpoint, and forward them to your machine. Copy the HTTPS link provided by the tool. It should look something like https://8098-24-22-245-250.ngrok.io.
Now head back to the Discord Developer Dashboard, and update the Interactions Endpoint URL for your app:
 This is the process we’ll use for local testing and development. When you’ve published your app to Cloudflare, you will want to update this field to use your Cloudflare Worker URL.
This is the process we’ll use for local testing and development. When you’ve published your app to Cloudflare, you will want to update this field to use your Cloudflare Worker URL.
Deployment
This repository is set up to automatically deploy to Cloudflare Workers when new changes land on themain branch. To deploy manually, run npm run publish, which uses the wrangler publish command under the hood.
Publishing via a GitHub Action requires obtaining an API Token and your Account ID from Cloudflare. These are stored as secrets in the GitHub repository, making them available to GitHub Actions.
The following configuration in .github/workflows/ci.yaml demonstrates how to tie it all together:
Code deep dive
Most of the interesting code in this app lives insrc/server.js. Cloudflare Workers require exposing a fetch function, which is called as the entry point for each request. This code will largely do two things for us: validate the request is valid and actually came from Discord, and hand the request over to a router to help give us a little more control over execution.
/. From here, we will use the discord-interactions npm module to help us interpret the event, and to send results.
Next steps
In case you need to reference any of the code, you can find the repo on GitHub
- Use message components in your app to add more interactivity (like buttons and select menus).
- Take a look at different public APIs on GitHub.
- Join the Discord Developers server to ask questions about the API, attend events hosted by the Discord API team, and interact with other developers.