How to Set Up Railway Environment Variables

Share
How-To Guide

How to Set Up Railway Environment Variables

Manage secrets and configuration in Railway

TL;DR

TL;DR: Click your service in Railway, go to the Variables tab, and add key-value pairs. Railway automatically redeploys when you save. Use variable references ( $ ) to share values between services. Use Shared Variables for project-wide secrets.

Step-by-Step Setup

1

Open your project

Go to railway.app/dashboard and click on your project.

2

Select your service

Click on the service you want to configure (e.g., your web app, API, or database).

3

Click the Variables tab in the service panel.

4

Add variables

You have two options:

  • Add individually: Click "New Variable" and enter key/value
  • Raw Editor: Click "RAW Editor" to paste multiple variables at once
# In Raw Editor, paste like a .env file:
DATABASE_URL=postgresql://...
STRIPE_SECRET_KEY=sk_test_xxxxx
OPENAI_API_KEY=sk-xxxxx
5

Save and deploy

Railway automatically triggers a redeploy when you save variables. You'll see the deployment start in the Deployments tab.

Railway-Specific Features

Variable References

Railway lets you reference variables from other services using the ${{SERVICE.VAR}} syntax:

# Reference the database URL from your Postgres service
DATABASE_URL=${{Postgres.DATABASE_URL}}

# Reference another service's variable
API_URL=${{api.RAILWAY_PUBLIC_DOMAIN}}

Built-in Railway Variables

Railway provides several built-in variables automatically:

# Available in every service
RAILWAY_ENVIRONMENT=production
RAILWAY_PUBLIC_DOMAIN=your-app.up.railway.app
RAILWAY_PRIVATE_DOMAIN=your-app.railway.internal
RAILWAY_PROJECT_ID=xxx
RAILWAY_SERVICE_ID=xxx

Shared Variables

For variables needed across multiple services:

  1. Click on "Shared Variables" at the project level
  2. Add your variable (e.g., STRIPE_SECRET_KEY)
  3. In each service, reference it: STRIPE_SECRET_KEY=${{shared.STRIPE_SECRET_KEY}}

Pro tip: Use Shared Variables for API keys that multiple services need. This way you only update the key in one place when rotating.

Using the Railway CLI

# Link to your project
railway link

# List variables for current service
railway variables

# Add a variable
railway variables set STRIPE_SECRET_KEY=sk_test_xxxxx

# Get a specific variable
railway variables get DATABASE_URL

# Delete a variable
railway variables delete OLD_KEY

# Run local dev with Railway variables
railway run npm run dev

Common Variable Patterns

# Database (auto-populated when adding Railway Postgres)
DATABASE_URL=${{Postgres.DATABASE_URL}}

# Redis (auto-populated when adding Railway Redis)
REDIS_URL=${{Redis.REDIS_URL}}

# Internal service communication
API_INTERNAL_URL=http://${{api.RAILWAY_PRIVATE_DOMAIN}}:3000

# External URL for webhooks
WEBHOOK_URL=https://${{RAILWAY_PUBLIC_DOMAIN}}/api/webhook

# Secrets (use Shared Variables)
STRIPE_SECRET_KEY=${{shared.STRIPE_SECRET_KEY}}
OPENAI_API_KEY=${{shared.OPENAI_API_KEY}}

Private vs Public Domains

RAILWAY_PRIVATE_DOMAIN is for internal service-to-service communication (faster, no internet). RAILWAY_PUBLIC_DOMAIN is your public URL. Use private domains when services talk to each other within Railway.

Do I need to restart after changing variables?

No, Railway automatically triggers a new deployment when you save variable changes. The new deployment will have the updated values.

How do I use Railway variables locally?

Use railway run to run commands with your Railway environment variables. For example: railway run npm run dev. This injects all your service's variables into the local process.

Can I have different variables for staging and production?

Yes, Railway supports environments. You can create a "staging" environment and set different variable values for each environment. Click on "Environments" in your project to manage them.

Related guides:Environment Variables Guide · Vercel Environment Variables · How to Hide API Keys

How-To Guides

How to Set Up Railway Environment Variables