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:
.env file:
.env
Required variables
DISCORD_BOT_TOKEN
Your Discord bot’s authentication token. This is obtained from the Discord Developer Portal.- Required: Yes
- Format: String (alphanumeric token)
- Where to get it: Discord Developer Portal
YOUTUBE_API_KEY
API key for YouTube Data API v3. This enables the bot to search for videos on YouTube.- Required: Yes
- Format: String (API key)
- Where to get it: Google Cloud Console
Get YouTube API key
- Go to Google Cloud Console
- Create a new project or select an existing one
- Enable the YouTube Data API v3
- Go to Credentials and create an API key
- Copy the API key to your
.envfile
Security best practices
Follow these security guidelines:- Never share your bot token or API keys publicly
- Add
.envto.gitignoreto prevent accidental commits - Regenerate tokens if they are exposed
- Use environment-specific
.envfiles 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
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:
.env file.
Next steps
With your environment variables configured, you’re ready to:- Run the bot with
python bot.py - Invite the bot to your Discord server
- Start playing music with
!play
