Deploying FastAPI Applications Using Render
Last Updated :
25 Apr, 2024
Deploying FastAPI applications involves making your FastAPI-based web application accessible over the internet or an intranet so that users can interact with it. FastAPI is a modern, fast (hence the name), web framework for building APIs with Python, known for its simplicity and performance. This article will show how to Deploy FastAPI Applications Using Render.
Deploying FastAPI Applications Using Render
Below is the step-by-step process for Deploying FastAPI Applications in Python Using Render:
File Structure

Step 1: Create DockerFile and render.yaml file
DockerFile : Dockerfile uses a pre-configured environment tailored for FastAPI applications. It copies the application code into the container for isolation and portability, simplifying deployment and ensuring consistent performance across different environments.
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8
COPY ./app /app
render.yaml : In this file configuration defines a service named "app" using Python environment, where "pip install -r requirements.txt" is the build command and "uvicorn main:app --host 0.0.0.0 --port 80" is the start command, launching the FastAPI app on host 0.0.0.0 and port 80.
services:
- name: app
env: python
buildCommand: pip install -r requirements.txt
startCommand: uvicorn main:app --host 0.0.0.0 --port 80
Step 2: Create Requirements.txt File
To generate the requirements.txt
file, execute the following command:
pip freeze > requirements.txt
Step 3: Create FastAPI Code
main.py: In this example, below FastAPI code establishes endpoints for managing student records in a MongoDB database. It connects to the database, defines models for student data, and implements CRUD operations (create, read, update, delete) via HTTP endpoints. Each endpoint handles requests, interacts with the database, and returns responses or error messages accordingly.
Python3
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from pymongo import MongoClient
from bson import ObjectId
app = FastAPI()
# Connect to MongoDB Atlas
client = MongoClient("mongodb://localhost:27017")
db = client["library_management"]
students_collection = db["students"]
class Address(BaseModel):
city: str
country: str
class Student(BaseModel):
name: str
age: int
address: Address
@app.post("/students", status_code=201)
async def create_student(student: Student):
result = students_collection.insert_one(student.dict())
return {"id": str(result.inserted_id)}
@app.get("/students", response_model=list[Student])
async def list_students(country: str = None, age: int = None):
query = {}
if country:
query["address.country"] = country
if age:
query["age"] = {"$gte": age}
students = list(students_collection.find(query, {"_id": 0}))
return students
@app.get("/students/{id}", response_model=Student)
async def get_student(id: str):
student = students_collection.find_one({"_id": ObjectId(id)}, {"_id": 0})
if student:
return student
else:
raise HTTPException(status_code=404, detail="Student not found")
@app.patch("/students/{id}", status_code=204)
async def update_student(id: str, student: Student):
updated_student = student.dict(exclude_unset=True)
result = students_collection.update_one(
{"_id": ObjectId(id)}, {"$set": updated_student})
if result.modified_count == 0:
raise HTTPException(status_code=404, detail="Student not found")
else:
return
@app.delete("/students/{id}", status_code=200)
async def delete_student(id: str):
result = students_collection.delete_one({"_id": ObjectId(id)})
if result.deleted_count == 0:
raise HTTPException(status_code=404, detail="Student not found")
else:
return {"message": "Student deleted successfully"}
Run the server using below command:
uvicron main:app --reload
Output
Step 4: Push Code on Github
First Push the code in github by creating new repository on your github account. To know how to create a Github repository Click Here. To know how to Push the code in Github Click Here .
Step 5: Create Account on Render
First we need to go on render website by click here and then create the account on Render. click on the get started for free for create the account on render.

Step 6: Login On Render
After Creating the account on Render login/register on Render website via through google or by registration and login.

Step 7: Select Web Services
After Creating the account and successfully login we can see a option of new when we click on new button then we will see the option of web service click on the web service option for hosting the API as API is run on web platform.

Step 8: Connect Github Repositery
After select web service we can see two option first is the build and deploy from git repo and next is the deploy an existing image as we are deploying from github so we select the First option and click on next button.

Step 9: Verify Github Account
After that we can see two option first is the we can connect our API repository directly by verify the Github account or we can enter the our github repo link in the bottom of the input field we can choose any option as per our conveniences.

Step 10: Fill All Required Details
After connecting/ fill the repo link click on continue button then our repo details will automatically fetch by the server. we need to only write the start command as we start the FastAPI using below command
uvicorn main:app --host 0.0.0.0 --port $PORT
Enter this command on start command input filed and select free and then click on next button.

Step 11: API Created Successfully
After clicking on the next button if you follow the all steps correctly your API deploy successfully and the link of the API shown at the left top corner of the web page. Our deployed API link is https://fastapi-1-adwc.onrender.com/.

Similar Reads
Testing FastAPI Application The web framework in Python that is used for creating modern and fast APIs is called FastAPI. Once we have created the FastAPI, there is a need to test if the API is working fine or not according to the requirements. In this article, we will discuss the various ways to test the FastAPI application.
3 min read
Deploying Scalable Applications with Azure In modern software development, deploying a scalable application is an important task. With the increase in the number of users, the applications have to handle a large user database, for this, they require a robust infrastructure that is also scalable which will ensure seamless performance and reli
7 min read
Deploying a React Application in Kubernetes Kubernetes is an open-source free manager for your computer program. These programs can be in the containers. Each container holds a program and everything it needs to run. To Keep track of all these containers that contain your application this is where the Kubernetes role comes in. Kubernetes does
5 min read
Understanding AWS App Runner: Simplifying Application Deployment In the IT industries one of the major important things is to manage and deploy the website applications which can be difficult and time taking processes. Here comes AWS App runner which is mainly used to quickly design and address the entire application lifecycle. It also provides a fully managed se
11 min read
Containerize Spring WebFlux Application In todayâs Web development, containerization has become a dominant way of deploying and managing applications. The containers package application provides a consistent environment in the development and production phases. Docker is a popular tool for building, deploying, and running applications usi
3 min read
Docker Compose For Java Applications: Simplifying Deployment Docker is an open-source platform for developers and sysadmins to build, ship, and run distributed applications. If we want to define and run more than one container then we should go for docker compose. We have to configure multiple container environments in a single file so that it simplifies the
8 min read