FastAPI has become one of the go-to frameworks for building APIs in Python, and for good reason—it is fast, it has automatic documentation, and async support makes it a solid choice for modern web applications. But getting a FastAPI app from your local machine to a live server can be frustrating if you have not done it before. Heroku remains a popular option despite the rise of competitors, and this guide walks through everything you need to get your app running without the usual headaches.

What You Need Before You Start

Before deploying, make sure you have four things ready: a FastAPI application that runs locally without errors, a requirements.txt file listing fastapi, uvicorn, and gunicorn, a Heroku account (the Eco plan costs $5 per month but dynos sleep after 30 minutes of inactivity), and the Heroku CLI installed and logged in on your machine. You also need Git initialized in your project directory with your code committed. If your app uses a database, note that Heroku supports PostgreSQL natively while MySQL requires a third-party addon.

The Three Files That Make or Break Your Deployment

Heroku needs three files to know how to run your FastAPI app. First is the Procfile—a plain text file in your project root with no extension containing one line that tells Heroku to use Uvicorn as the server and bind to the dynamic port it assigns at runtime. The critical mistake most guides get wrong is hardcoding a port like 8000 instead of referencing the PORT environment variable, which causes an immediate crash after deployment. Second is runtime.txt, which specifies your Python version—Python 3.11 and 3.12 are both supported in 2026. Skipping this file means Heroku picks a version that may not match your local environment. Third is requirements.txt, which must list every package your app imports including fastapi, uvicorn, and gunicorn. Gunicorn serves as the process manager on Heroku, and if it is missing, your build fails immediately.

Deploying via Git

With those files in place, open your terminal, log into the Heroku CLI, and run heroku create to generate a new app with a random name while setting up a Git remote called heroku. Use heroku create custom-name if you want a specific app name. Set any environment variables—API keys, secret keys, database URLs—using heroku config:set before pushing your code. These should never be hardcoded in source files. Push your code to Heroku using git push heroku main (or master depending on your branch). Heroku detects Python, installs dependencies from requirements.txt, runs the Procfile command, and starts your app. The entire process takes about a minute. Access your live URL with heroku open or find it in the dashboard.

Connecting a Database

If your FastAPI app uses PostgreSQL, add it through the CLI with heroku addons:create heroku-postgresql:essential-0 ($5 per month for 1GB storage and up to 25 connections) or through the dashboard. Heroku automatically creates a DATABASE_URL environment variable containing the full connection string that you pass to SQLAlchemy when creating your engine—no manual host, port, or credentials needed. Running Alembic migrations on Heroku works differently than locally: you execute them against your live app using heroku run rather than pointing your local Alembic at the production database. This keeps your migration process consistent with how the app actually runs in production.

Common Errors That Trip Most Developers Up

The H10 App Crashed error is the most common issue and almost always comes from a hardcoded port number instead of referencing the PORT environment variable—check your Procfile and redeploy. Worker timeout errors occur because Heroku's default request timeout is 30 seconds, which catches ML inference endpoints or heavy computation; the fix is optimizing your endpoint, moving long-running tasks to background workers, or increasing gunicorn workers. ModuleNotFoundError means a package exists in your local environment but not in requirements.txt—Heroku builds a fresh environment from that file during deployment. CORS errors after deployment happen because FastAPI's default allowed origins set to localhost will block requests from real production domains; update your CORS middleware configuration to include your actual domain.

The Real Cost of Heroku for FastAPI in 2026

Heroku removed its free tier back in 2022, and the actual monthly bill for a production setup is higher than most guides show. The Eco plan at $5 per month is not suitable for APIs because sleeping dynos add latency whenever traffic has been absent for 30 minutes. The Basic plan at $7 per month keeps your dyno awake but provides no autoscaling—if traffic spikes, requests queue and performance suffers. Standard dynos start at $25 to $50 per month and support autoscaling with multiple dynos, which is the minimum realistic tier for production APIs handling variable traffic. Add Heroku Postgres Essential-1 ($9 per month) for more storage or connections beyond what Essential-0 provides, plus monitoring addons and other services, and a production-ready FastAPI setup realistically costs $40 to $80 monthly before significant traffic arrives.

Key Takeaways

  • Your Procfile must reference the PORT environment variable—hardcoding 8000 causes immediate crashes
  • Gunicorn must be in requirements.txt or your build fails; it is not optional on Heroku
  • Run Alembic migrations against your live app using heroku run, never locally against production
  • Eco dynos sleep after 30 minutes of inactivity and are unsuitable for APIs that need instant responses
  • A realistic production FastAPI setup on Heroku costs $40 to $80 per month before heavy traffic

The Bottom Line

Heroku still works fine for deploying FastAPI, but the configuration overhead is real—Procfile tuning, dyno management, addon setup, and debugging cryptic errors are all things you have to handle yourself. For simple hobby projects this is manageable; for production APIs with unpredictable AI or ML workloads, platforms that automate infrastructure management remove friction without sacrificing reliability.