Put Your API Online with Render
Render's free web service builds a FastAPI app from its GitHub repository with pip install -r requirements.txt, starts it with uvicorn main:app --host 0.0.0.0 --port $PORT, keeps its secrets in environment variables and checks /health before sending anyone to a deploy, and then every push deploys again, while a rollback puts an earlier deploy back.
Journeyman · FastAPI · REST API · Updated
After this lesson you can
- Deploy a FastAPI app as a Render web service from GitHub, with its build and start commands, environment variables and health check path
- Explain how a free Render web service behaves, such as its sleep after 15 idle minutes, a deploy on every push and rolling back
Key takeaways
- Render's free web service runs your API from its GitHub repository, at an address ending in .onrender.com that anyone can open.
- Its form takes the build command pip install -r requirements.txt and the start command uvicorn main:app --host 0.0.0.0 --port $PORT.
- Secrets go in the service's environment variables, never in the code, and the health check path /health tells Render when a deploy is ready.
- A free service sleeps after 15 minutes with no requests, and the next request wakes it, which takes about a minute.
- Every push to the branch deploys again, and Rollback on the Deploys page puts one of the two deploys before it back.
The trails API is ready for a host: pinned packages, a .python-version file, a start command, settings from the environment and a /health route. Now it goes online, so a hiker at the trailhead can open it on a phone. You'll do this for every project you want to show someone: a portfolio link works only when it's live.
Free, and what free means
This course uses Render, whose free web service runs Python apps straight from a GitHub repository. Render's documentation sets the free web service's limits, and they're worth knowing before you click anything:
- 750 free hours a month for your workspace, your account's space on Render. One service running all month uses at most 744 of them, so one fits. If they run out, Render suspends your free services until the next month.
- It sleeps after 15 minutes with no requests. The next request wakes it, which takes about a minute; a browser sees Render's loading page meanwhile. That slow first answer is called a cold start.
- Its files don't last, as Where the Data Lives When Your API Is Online showed, so the trails API seeds its demo trails each time it starts.
- Bandwidth has a monthly limit. Going over it is billed when a payment method is on file; without one, Render suspends your free services for the rest of the month instead.
Render's docs say plainly not to use free instances for production applications: they're for trying things out, which fits a course project and a portfolio link. Sign up at render.com, and choose Free whenever a plan is offered. If Render asks for a credit card, stop there: nothing in this course needs a paid plan.
A web service from GitHub
Push the API to a GitHub repository of your own first. Then, in the Render Dashboard, click New, then Web Service. Choose Git Provider, connect your GitHub account, and pick the repository. Fill in the form:
| Setting | Value |
|---|---|
| Name | trail-conditions, which becomes part of the address |
| Branch | main |
| Language | Python 3 |
| Build Command | pip install -r requirements.txt |
| Start Command | uvicorn main:app --host 0.0.0.0 --port $PORT |
| Compute | Free |
Render reads .python-version for the Python to install. (A PYTHON_VERSION environment variable would win over the file, and it needs all three numbers, such as 3.15.2.)
Secrets and the health check
Open Advanced before you create the service. Two things go there:
Environment variables. The trails API's two settings have defaults, so it needs none. An API with sign-in needs its secret here, such as the
JWT_SECRETthat signs tokens. Make a long random one on your own computer:python -c "import secrets; print(secrets.token_urlsafe(32))"Paste what it prints as the value, in Render and nowhere else: not in the code, the README or a commit. Later, the service's Environment page changes them, with + Add Environment Variable and then Save, rebuild, and deploy.
Health Check Path:
/health. Render calls it every few seconds. A new deploy gets no visitors until/healthanswers with a success code within five seconds, and a running one that stops answering for 60 seconds is restarted.
Then click Create Web Service.
The deploy, the link, and every push after
The service's Deploys page shows the deploy's log as it runs: pip install installing your pinned packages, then the start command. Once /health answers, the deploy is live, at the address on the service's page, ending in .onrender.com. The service's Logs page shows what the running API prints, starting with your lifespan's line, Trails API started: ran migrations [1], added 6 demo trails, and then each request as it comes in.
Open /docs at that address: it's the same docs page as on your laptop, now on the internet. /trails?status=muddy answers 200 OK with the two muddy trails, Lookout Ridge Trail and Meadow View Loop.
From now on, every push to main deploys again. That's called auto-deploy, and Render's default setting for it is On Commit. A deploy whose build fails, or whose /health never answers, doesn't replace the one that's live: the old version keeps serving. If a deploy goes live and turns out wrong, open Deploys, click Rollback on the last good deploy, then Rollback to this deploy. A free service can go back to its two most recent earlier deploys. Rolling back turns auto-deploy off, so turn it back on in Settings once the fix is pushed.
Sources
- Render, Deploy for Free: 750 free instance hours a month per workspace; a free web service spins down after 15 minutes without traffic and takes about a minute to spin up; an ephemeral filesystem; the bandwidth limit; not for production; rollbacks only to the two most recent previous deploys.
- Render, Deploy a FastAPI App: the build command
pip install -r requirements.txtand the start commanduvicorn main:app --host 0.0.0.0 --port $PORT. - Render, Setting Your Python Version:
PYTHON_VERSIONcomes before.python-versionand must have all three numbers. - Render, Health Checks: checks every few seconds, a success within five seconds, no traffic to a new deploy until it passes, and a restart after 60 seconds of failures.
- Render, Deploying on Render: auto-deploy On Commit by default, and a failed deploy leaves the last good one running.
- Render, Rollbacks: Rollback, then Rollback to this deploy; a rollback from the Dashboard turns auto-deploy off.
Check yourself
2 questions to try on the lesson page.
So what
Your API is a link now: a hiker, a friend or an employer can open it, and every push you make is live a few minutes later.