Requirements
- You’ve created a Runpod account
- You’ve created a Runpod API key
- You’ve installed Python 3.10, 3.11, 3.12, or 3.13.
- You’ve completed the Flash quickstart or are familiar with Flash basics
What you’ll build
By the end of this tutorial, you’ll have a working REST API that:- Accepts text input via
POST /analyze - Returns system health via
GET /health - Provides API information via
GET /info - Runs GPU-accelerated sentiment analysis via
POST /sentiment(optional GPU route) - Deploys to Runpod Serverless with proper authentication
Step 1: Set up your project
Create a new directory for your project and set up a Python virtual environment:YOUR_API_KEY with your actual Runpod API key.
Step 2: Create the API server file
Create a new file calledapi.py:
Step 3: Define the load-balanced endpoint
Add the following code toapi.py:
Worker Quota Considerations: The
workers setting determines the maximum number of concurrent workers. Standard Runpod accounts have a total quota of 30 workers across all endpoints. If you have other endpoints running, you may need to reduce workers to (0, 1). Check your quota in the Runpod console.Step 4: Add API routes
Add three routes to your API - health check, info, and text analysis:api endpoint, meaning they deploy to a single Serverless endpoint.
Step 5: Add a GPU-accelerated route (optional)
For GPU-accelerated sentiment analysis, add a separate endpoint:The sentiment analysis route uses a separate GPU endpoint because it requires different hardware than the CPU routes. This is a common pattern: use CPU endpoints for lightweight API logic and GPU endpoints for ML inference.GPU Availability: Using
GpuGroup.ANY provides better availability than specific GPU types like GpuGroup.ADA_24. First requests to GPU endpoints may take 3-10 minutes due to:- GPU provisioning (depends on current availability)
- Dependency installation (transformers, torch)
- Model downloads (distilbert is ~250MB)
Step 6: Add the main execution block
Add the following at the end ofapi.py to enable local testing:
Step 7: Test locally
Run your script to test the API locally:Local Testing Limitations: The GPU sentiment endpoint requires
transformers and torch to be installed locally for testing. For full testing of all endpoints including GPU routes, use flash dev (covered in Step 9) instead of direct Python execution.Step 8: Build a Flash app for production
To deploy your API to production, create a Flash app:Create lb_worker.py (CPU routes):
Replace the contents of lb_worker.py with:
Create gpu_worker.py (GPU route):
If you added the GPU sentiment route, replace the contents of gpu_worker.py with:
Configure environment:
YOUR_API_KEY with your actual Runpod API key.
Step 9: Test with the development server
Start the Flash development server:Development Server Path Prefixes: The
flash dev server adds worker file prefixes to routes (e.g., /lb_worker/health, /gpu_worker/sentiment). When deployed to production, endpoints use the paths as defined in the route decorators (e.g., /health, /sentiment) without the prefixes.Step 10: Deploy to production
When you’re ready to deploy, useflash deploy:
Step 11: Call your deployed API
Call your production endpoints with authentication:Production Path Note: In production, the endpoints use the exact paths defined in your route decorators (e.g.,
/health, /sentiment), without the worker file prefixes used in flash dev.Understanding the deployment architecture
Your deployed API creates two independent Serverless endpoints: Key points:- CPU endpoint (
text-api) handles three routes on one Serverless endpoint - GPU endpoint (
gpu-sentiment) handles GPU inference on a separate endpoint - Both endpoints scale independently based on load
- All requests require authentication with your API key
Troubleshooting
Worker quota exceeded
Issue:Max workers across all endpoints must not exceed your workers quota (30)
Solution:
- Check your current worker usage in the Runpod console
- Reduce
workersin your configuration: - Clean up unused endpoints before deploying new ones
GPU endpoint timeout
Issue: Request times out after 60 seconds on first GPU endpoint call Solutions:- This is normal for the first request - GPU provisioning takes time
- Wait 1-3 minutes and try again
- Use
GpuGroup.ANYinstead of specific GPU types for better availability - Consider using CPU for development testing:
Port already in use
Issue:ERROR: [Errno 48] Address already in use when running flash dev
Solutions:
Import errors in sentiment analysis
Issue:ModuleNotFoundError: No module named 'transformers'
Solution: Ensure dependencies are specified on the endpoint:
Endpoint stays in queue
Issue: GPU sentiment route stays inIN_QUEUE status
Solutions:
- Check GPU availability in console
- Use flexible GPU selection:
- Increase worker quota if at limit
Next steps
Now that you’ve built a REST API with Flash, you can:Add more routes
Expand your API with additional functionality:Add authentication middleware
Implement custom authentication for your API:Monitor your API
- Track endpoint health in the Runpod console
- Monitor request counts and error rates
- Adjust
workersbased on traffic patterns