You have built a Discord bot and tested it locally. Now you want it to run 24/7 without keeping your computer on. Railway is a cloud platform that hosts applications easily. This article explains how to deploy your Discord bot to Railway so it stays online and responds to commands.
Key Takeaways: Deploying a Discord Bot on Railway
- Railway Dashboard > New Project > Deploy from GitHub: Connect your bot’s repository to Railway for automatic deployment.
- Environment Variables > DISCORD_TOKEN: Store your bot token securely in Railway to keep it private.
- Railway > Deployments > Restart: Manually restart your bot after code changes or if it goes offline.
Overview of Railway and Discord Bot Deployment
Railway is a Platform as a Service that runs your code in the cloud. It supports Node.js, Python, Go, and many other runtimes. Your Discord bot must be configured to run as a long-lived process. Railway automatically detects the runtime from your project files and starts your bot.
Before deploying, your bot code must be in a Git repository hosted on GitHub. Railway pulls the code from that repository and deploys it. You need a Railway account, a GitHub account, and a Discord bot token from the Discord Developer Portal.
Railway uses environment variables to store secrets like your bot token. Do not hardcode the token in your code. Instead, read it from an environment variable named DISCORD_TOKEN or similar. This keeps your token safe and makes the deployment portable.
Steps to Deploy a Discord Bot to Railway
Follow these steps exactly. The process assumes you have a functional Discord bot written in Node.js or Python. Adjust the runtime commands if you use a different language.
Step 1: Prepare Your Bot Code for Deployment
- Create a GitHub repository
Push your bot code to a public or private GitHub repository. Railway can only deploy code from a repository, not from a local folder. - Add a start script
For Node.js bots, ensurepackage.jsonhas astartscript. Example:"start": "node index.js". For Python bots, add aProcfilewithworker: python bot.pyor use astart.shscript. - Remove hardcoded tokens
Replace any hardcoded token withprocess.env.DISCORD_TOKENfor Node.js oros.getenv('DISCORD_TOKEN')for Python. Commit and push the changes.
Step 2: Connect Railway to GitHub
- Log in to Railway
Go to railway.app and sign in with your GitHub account. Railway uses GitHub OAuth for authentication. - Create a new project
Click New Project on the dashboard. Select Deploy from GitHub repo. - Authorize Railway
If prompted, grant Railway access to your GitHub repositories. You can choose to give access to all repos or only selected ones. - Select your bot repository
Find and click the repository containing your bot code. Railway will start the deployment automatically.
Step 3: Set Environment Variables
- Open your project dashboard
After the repository is selected, Railway shows your project dashboard. Click the Variables tab. - Add DISCORD_TOKEN
Click New Variable. Set the key asDISCORD_TOKENand the value as your bot token from the Discord Developer Portal. Do not share this token with anyone. - Add other required variables
If your bot needs other environment variables likeCLIENT_IDorGUILD_ID, add them here. Click Save after each variable.
Step 4: Configure the Start Command
- Go to the Deployments tab
Click Deployments in the left sidebar. You will see the current deployment status. - Set the start command
If Railway did not auto-detect the start command, click Edit on the deployment. Under Start Command, enternpm startfor Node.js orpython bot.pyfor Python. Click Save. - Redeploy the bot
Click Deploy to apply the changes. Railway will rebuild and restart the bot with the new start command.
Step 5: Verify the Bot is Online
- Check the logs
In the Deployments tab, click View Logs. Look for messages likeReady!orLogged in as BotName. If you see errors, read them carefully and fix the code or variables. - Test the bot in Discord
Open your Discord server and send a command your bot handles. If the bot responds, the deployment is successful. - Enable public networking (optional)
If your bot uses webhooks or HTTP endpoints, go to Networking and generate a public URL. Add that URL to your bot’s code if needed.
Common Issues After Deploying a Discord Bot on Railway
Bot Goes Offline After a Few Minutes
Railway may put your bot to sleep if it detects no incoming traffic. Discord bots do not receive HTTP traffic — they use WebSockets. Railway does not automatically keep the bot awake. To prevent this, upgrade to a paid Railway plan that offers always-on deployments. The free plan sleeps after 5 minutes of inactivity.
Bot Crashes with “DiscordAPIError: Missing Permissions”
This error means your bot lacks the required permissions in the Discord server. Re-invite the bot with the correct OAuth2 scopes and permissions. Go to Discord Developer Portal > OAuth2 > URL Generator, select bot and the needed permissions, then use the generated URL to invite the bot again.
Environment Variable Not Being Read
Check that the variable name in Railway matches exactly what your code uses. Variable names are case-sensitive. For example, DISCORD_TOKEN is different from discord_token. Also ensure you saved the variable after entering it.
Railway Plans: Free vs Paid for Discord Bot Hosting
| Feature | Free Plan | Paid Plan (Starts at $5/month) |
|---|---|---|
| Always-on | No — bot sleeps after 5 minutes of inactivity | Yes — bot runs 24/7 |
| CPU and RAM | Limited, shared resources | Dedicated resources, higher limits |
| Deployments | Unlimited | Unlimited |
| Custom domains | No | Yes |
You have now deployed your Discord bot to Railway. The bot runs in the cloud and can stay online 24/7 if you use a paid plan. Next, consider adding a health check endpoint to monitor the bot’s uptime. You can also set up GitHub Actions to automatically deploy new code changes to Railway whenever you push to the main branch.