DEV Community

Cover image for Deploying a FastAPI App on Render: A Step-by-Step Guide
Ganesh Bora
Ganesh Bora

Posted on

Deploying a FastAPI App on Render: A Step-by-Step Guide

FastAPI has become a go-to framework for building Python APIs—it's fast, async-native, and auto-generates OpenAPI docs. But once your local server runs perfectly, the next question is: how do you get it on the internet? Render offers a clean path with a generous free tier. Here's how to take a FastAPI app from your laptop to a live URL.

1. Prepare Your App for Deployment

Before touching Render, make sure your project is deployment-ready. At minimum, you need:

  • A main.py file (or similar) containing your FastAPI instance:
  from fastapi import FastAPI

  app = FastAPI()

  @app.get("/")
  def read_root():
      return {"Hello": "World"}
Enter fullscreen mode Exit fullscreen mode
  • A requirements.txt listing all dependencies, including fastapi and uvicorn:
  fastapi
  uvicorn
Enter fullscreen mode Exit fullscreen mode

Add any other packages your app uses—database drivers, auth libraries, etc.

  • A Git repository on GitHub or GitLab. Render pulls your code from there, so commit everything and push.

You don't strictly need a Procfile—Render lets you set the start command directly—but it can be handy for local testing with tools like Heroku. If you include one, it should look like:

web: uvicorn main:app --host 0.0.0.0 --port $PORT
Enter fullscreen mode Exit fullscreen mode

2. Create a Web Service on Render

  1. Log in to render.com and click New +Web Service.
  2. Connect your GitHub or GitLab account if you haven't already, then select your repository.
  3. Render will detect the language and suggest defaults. Override them with:
    • Build Command: pip install -r requirements.txt
    • Start Command: uvicorn main:app --host 0.0.0.0 --port $PORT
  4. Choose an instance type. The Free tier is perfect for experiments—it sleeps after 15 minutes of inactivity and wakes on the next request (with a few seconds of cold start).
  5. Pick a region (choose one close to your users) and click Create Web Service.

Render will build your app, install dependencies, and start the server. Within a couple of minutes, you'll get a URL like https://your-app.onrender.com.

3. Environment Variables and Secrets

Hardcoding secrets in your code is a bad idea. Render lets you manage them cleanly:

  • In your web service dashboard, go to Environment.
  • Add variables like DATABASE_URL, API_KEY, or SECRET_TOKEN.
  • In your code, read them with os.getenv():
  import os
  database_url = os.getenv("DATABASE_URL")
Enter fullscreen mode Exit fullscreen mode

These variables are injected at runtime, so you never commit sensitive data to Git.

4. Connecting a Database (Optional)

If your app needs a database, Render offers managed PostgreSQL with a free tier. To add one:

  1. From the Render dashboard, click New +PostgreSQL.
  2. Choose a name, instance type, and region.
  3. Once created, copy the Internal Database URL from the database's dashboard.
  4. Add it as an environment variable in your web service (e.g., DATABASE_URL).

If you're using an async driver like asyncpg or aiosqlite, make sure it's in requirements.txt. Render's managed DB handles backups and scaling for you—no extra config needed.

5. Common Pitfalls and How to Avoid Them

  • Wrong port binding. Always use --host 0.0.0.0 and --port $PORT. Render assigns a random port; hardcoding 8000 will fail.
  • Cold starts on the free tier. After idle, the first request may take a few seconds. That's normal—consider a paid tier if you need consistent low latency.
  • CORS errors. If your frontend runs on a different domain, configure CORS in FastAPI:
  from fastapi.middleware.cors import CORSMiddleware

  app.add_middleware(
      CORSMiddleware,
      allow_origins=["https://your-frontend.com"],
      allow_methods=["*"],
      allow_headers=["*"],
  )
Enter fullscreen mode Exit fullscreen mode
  • Build failures. If pip install fails, check your requirements.txt for typos or incompatible versions. Specify a Python version in Render's settings if needed.
  • Missing dependencies. Forgot to add a package? Add it to requirements.txt, commit, and push—Render will rebuild automatically.

Wrapping Up

Deploying FastAPI on Render is straightforward: prepare your app, create a web service, set environment variables, and optionally attach a database. Render watches your Git repo, so every push triggers a new deployment. Start with a simple endpoint on the free tier, then expand as your project grows. In under ten minutes, you'll have a production-ready API with a public URL—no servers to manage, no SSH to wrestle with.

Top comments (0)