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.pyfile (or similar) containing your FastAPI instance:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
-
A
requirements.txtlisting all dependencies, includingfastapianduvicorn:
fastapi
uvicorn
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
2. Create a Web Service on Render
- Log in to render.com and click New + → Web Service.
- Connect your GitHub or GitLab account if you haven't already, then select your repository.
- 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
-
Build Command:
- 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).
- 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, orSECRET_TOKEN. - In your code, read them with
os.getenv():
import os
database_url = os.getenv("DATABASE_URL")
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:
- From the Render dashboard, click New + → PostgreSQL.
- Choose a name, instance type, and region.
- Once created, copy the Internal Database URL from the database's dashboard.
- 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.0and--port $PORT. Render assigns a random port; hardcoding8000will 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=["*"],
)
-
Build failures. If
pip installfails, check yourrequirements.txtfor 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)