Skip to main content

Environment variables

MusicBot requires two environment variables to function properly. These are loaded from a .env file in the project root directory.

Create .env file

Create a .env file in the root directory of your MusicBot installation:
touch .env
Add the following environment variables to your .env file:
.env
YOUTUBE_API_KEY=your_youtube_api_key_here
DISCORD_BOT_TOKEN=your_discord_bot_token_here

Required variables

DISCORD_BOT_TOKEN

Your Discord bot’s authentication token. This is obtained from the Discord Developer Portal.
The bot will not start without this token. You’ll see the error:
CRITICAL: DISCORD_BOT_TOKEN not found in environment variables. The bot cannot connect to Discord.
Learn how to obtain your bot token in the Discord bot setup guide.

YOUTUBE_API_KEY

API key for YouTube Data API v3. This enables the bot to search for videos on YouTube.
The bot requires this key to search for songs. Without it, you’ll see:
CRITICAL: YOUTUBE_API_KEY not found in environment variables. The bot cannot search videos.

Get YouTube API key

  1. Go to Google Cloud Console
  2. Create a new project or select an existing one
  3. Enable the YouTube Data API v3
  4. Go to Credentials and create an API key
  5. Copy the API key to your .env file

Security best practices

Keep your secrets secure! Never commit your .env file to version control.
Follow these security guidelines:
  • Never share your bot token or API keys publicly
  • Add .env to .gitignore to prevent accidental commits
  • Regenerate tokens if they are exposed
  • Use environment-specific .env files for development and production
  • Restrict API key access in Google Cloud Console to only required APIs

Example .env file

Here’s a complete example (with placeholder values):
.env
# Discord Bot Configuration
DISCORD_BOT_TOKEN=MTIzNDU2Nzg5MDEyMzQ1Njc4OQ.AbCdEf.GhIjKlMnOpQrStUvWxYz

# YouTube API Configuration
YOUTUBE_API_KEY=AIzaSyAaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQq
Replace the example values with your actual credentials before running the bot.

Verify configuration

After creating your .env file, verify it’s loaded correctly by checking the bot startup logs:
python bot.py
If configured correctly, you should see:
INFO:discord: Bot connected as YourBotName
If you see critical errors about missing environment variables, double-check your .env file.

Next steps

With your environment variables configured, you’re ready to:
  1. Run the bot with python bot.py
  2. Invite the bot to your Discord server
  3. Start playing music with !play

Build docs developers (and LLMs) love