Overview
This tutorial will guide you through deploying Nuwa LLM Gateway on Railway platform, enabling your gateway to provide multi-provider LLM access with DID authentication and usage-based pricing. The LLM Gateway unifies multiple LLM providers (OpenAI, OpenRouter, LiteLLM) behind a single endpoint with automatic payment integration.Why Railway?
Railway is an ideal platform for deploying LLM Gateway:- ✅ Full support for Node.js applications and environment variables
- ✅ Supports long connections and real-time communication
- ✅ Simple deployment via Docker image or NPM package
- ✅ Automatic HTTPS and domain generation
- ✅ Flexible environment variable management
- ✅ Built-in logging and monitoring
Prerequisites
Before starting, ensure you have:- Railway Account: Sign up for a Railway account
- Service Key: Follow the Service Key guide to obtain your service key
- Provider API Keys: At least one LLM provider API key (OpenAI, OpenRouter, etc.)
Step 1: Create Empty Service on Railway
- Log in to Railway Dashboard
- Click “New Project”
- Select “Empty Service”
- Name your service, for example:
nuwa-llm-gateway
Step 2: Configure Environment Variables
Add the following environment variables in your Railway project’s Variables page:Required Variables
Your service DID private key, obtained from the Service Key guide. Used for DID authentication and payment processing.
Rooch network type (test/main). Currently only test network is supported.
Default:
testLLM Provider API Keys (At least one required)
OpenAI API key for accessing GPT models and other OpenAI services.
Format:
sk-proj-...OpenRouter API key for accessing multiple model providers.
Format:
sk-or-v1-...LiteLLM API key if using LiteLLM proxy.
Optional Configuration Variables
Service port, automatically set by Railway.
Global pricing multiplier (range 0-2). Example:
1.10 for +10% markup.JSON string for custom model pricing. Example:
{"gpt-4":{"promptPerMTokUsd":25,"completionPerMTokUsd":50}}Enable debug logging for troubleshooting.
Environment variables are case-sensitive. Ensure you use the exact names as specified above.
Step 3: Deploy LLM Gateway
Method A: Using Docker Image (Recommended)
1
Connect Docker Image
- In the service settings page, find the “Source” section
- Click “Connect” or “Deploy”
- Select “Docker Image”
- Enter the image address:
ghcr.io/nuwa-protocol/llm-gateway:latest - Click “Deploy”
2
Monitor Deployment Process
- Railway will automatically start pulling the image and deploying
- View deployment logs in Railway Dashboard
- Wait for deployment to complete
- Look for “Server started on port 8080” in the logs
Method B: Using NPM Package
1
Create Dockerfile
Create a
Dockerfile in your project:2
Deploy from Repository
- Push your Dockerfile to a Git repository
- Connect the repository to Railway
- Railway will automatically build and deploy
Step 4: Generate Public Domain
After successful deployment, you need to manually generate a public domain:- In the Railway service page, find the “Settings” tab
- Scroll to the “Networking” section
- Click the “Generate Domain” button
- Railway will generate a domain for you, similar to:
https://your-gateway-name.up.railway.app
Railway services do not automatically generate public domains by default. You need to manually generate one to access the service from outside.
https://nuwa-llm-gateway.up.railway.app. You can use this domain to access your LLM Gateway.
Step 5: Verify Deployment
Check the Nuwa payment service discovery endpoint:Test Provider Routes
Test the provider-specific routes:Actual API calls require DID authentication. Use the Nuwa Login Demo or integrate with Nuwa Payment Kit for testing.
Step 6: Configure for Production Use
Update Your Applications
Update your applications to use the Railway-deployed gateway:Monitor Usage and Logs
- Railway Logs: View real-time logs in Railway Dashboard
- Usage Statistics: Monitor request patterns and response times
- Error Tracking: Set up alerts for deployment failures or errors
Troubleshooting
Deployment Failure
Issue: Container fails to start Solutions:- Check Railway deployment logs for specific error messages
- Verify
SERVICE_KEYenvironment variable is correctly set - Ensure at least one provider API key is configured
- Check environment variable format (boolean values should be
true/false, not"true"/"false")
Cannot Access Service
Issue: Service deployed successfully but cannot be accessed externally Solutions:- Confirm public domain has been generated (Settings → Networking → Generate Domain)
- Check if service is running (look for “Server started” in logs)
- Verify port configuration is correct (should be 8080)
- Review Railway service logs for runtime errors
Provider API Errors
Issue: LLM provider requests fail Solutions:- Verify provider API keys are valid and have sufficient quota
- Check provider-specific rate limits and usage policies
- Ensure API keys have correct permissions for the models you’re using
- Review gateway logs for specific provider error messages
Authentication Issues
Issue: DID authentication fails Solutions:- Confirm
SERVICE_KEYis valid and properly formatted - Check
ROOCH_NETWORKmatches your DID network (test/main) - Verify your service DID has sufficient gas for transactions
- Review authentication logs for specific error details
Scaling and Optimization
Performance Optimization
- Pricing Configuration: Use
PRICING_MULTIPLIERto adjust profit margins - Provider Selection: Configure multiple providers for redundancy
Monitoring and Alerts
- Set up Railway alerts for deployment failures
- Monitor response times and error rates
- Track usage patterns to optimize pricing and capacity
Security Best Practices
- Monitor access logs for unusual patterns
- Keep service keys secure and never commit them to version control
- Use environment variables for all sensitive configuration
Next Steps
After successful deployment, you can:- Integrate with Caps: Use your gateway URL in Cap Studio configurations
- Monitor Revenue: Track usage and revenue through the Revenue dashboard
- Optimize Pricing: Adjust pricing based on usage patterns and market conditions

