How to Build a Discord Bot with Free APIs
Discord bots are one of the most fun things you can build as a developer. They're interactive, people actually use them, and they're a great excuse to explore free APIs. In this guide, we'll build a multi-command Discord bot that pulls data from several free APIs — jokes, weather, trivia, and random facts.
What you'll need
- Node.js 18 or newer installed on your machine
- A Discord account and a server where you have admin permissions
- A text editor (VS Code works great)
- About 30 minutes
Step 1: Create a Discord application
Head to the Discord Developer Portal and click "New Application." Give it a name like "API Bot," then navigate to the Bot tab and click "Add Bot." Copy the token — you'll need it soon. Under the OAuth2 tab, select the "bot" scope and the permissions you need (Send Messages, Read Message History, Embed Links). Use the generated URL to invite the bot to your server.
Step 2: Set up the project
Create a new directory and initialize the project:
Create a .env file to store your token. Never commit this file to version control.
Step 3: Choose your APIs
We'll use three free APIs that require no authentication — perfect for a bot:
- JokeAPI — Serves programming jokes, puns, and general humor. No API key needed.
- Open-Meteo — Weather forecasts by coordinates, completely free and keyless.
- Open Trivia Database — Multiple-choice trivia questions across dozens of categories.
You can browse hundreds more options on our API search page to find APIs that match your bot's personality.
Step 4: Write the bot
Create an index.js file with the following structure:
How it works
The bot listens for messages starting with ! prefixes. Each command hits a different API, formats the response, and sends it back to the channel. The weather command uses an embed for a cleaner look, and the trivia command uses Discord's spoiler syntax to hide the answer.
Step 5: Run and test
In your Discord server, try !joke, !weather Tokyo, and !trivia. You should see responses within a second or two.
Adding more commands
The pattern is always the same: fetch data from an API, format it, send a reply. Here are ideas for expanding your bot:
- !cat — Random cat images from The Cat API
- !quote — Inspirational quotes from the Quotable API
- !define [word] — Dictionary lookups from Free Dictionary API
- !country [name] — Country facts from REST Countries
Check out our API categories page for inspiration — the Animals, Entertainment, and Science categories are especially popular for bots.
Deployment tips
Running the bot on your laptop isn't practical long-term. Here are affordable hosting options:
- Railway.app — Free tier supports small Node.js apps. Push your code, set the DISCORD_TOKEN environment variable, done.
- Fly.io — Generous free tier with always-on VMs. Great for bots that need to stay connected.
- A cheap VPS — A $5/month server from DigitalOcean or Hetzner gives you full control. Use PM2 to keep the bot running.
Whichever you choose, make sure to set your bot token as an environment variable — never hardcode it in your source files.
What's next
Once the basic bot works, consider upgrading to slash commands (Discord's modern interaction model), adding a cooldown system to prevent API rate-limit issues, or storing user preferences in a SQLite database. The core idea stays the same: your bot is a bridge between Discord users and the massive ecosystem of free APIs out there.