Lab 2.1 - FastAPI Setup
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3 based on standard Python type hints. It is easy to use, fast to code, and easy to learn. In this step, we will create a simple Python backend using FastAPI that returns a list of students present in our class.
Objective
Create a Python backend using Rest API that returns a list of students present in the our class.
- The API should be accessible at
http://localhost:5000/students
. - The API should return a JSON response with the following format:
{
"students": [
{
"name": "Mehrshad Lotfi",
"email": "mehrshad@optiop.org"
},
{
"name": "Reza Mohammadi",
"email": "reza@optiop.org"
}
]
}
Instructions
Step 1
We checkout the documentation of FastAPI: https://fastapi.tiangolo.com/ Under the section, installation, we see that we can install FastAPI using pip:
pip install fastapi
pip install "uvicorn[standart]"
Step 2
From the FastAPI documentation, we get example code under example section:
from typing import Union
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
We run the example code using the following command:
uvicorn main:app --reload
Step 3
We test the API using the following urls:
Step 4
We modify the code to return the list of students:
@app.get("/")
def read_root():
return {
"students": [
{
"name": "Mehrshad Lotfi",
"email": "mehrshad@optiop.org"
},
{
"name": "Reza Mohammadi",
"email": "reza@optiop.org"
}
]
}
Step 5
We run the code again using the following command:
uvicorn main:app --reload
Step 6
We test the API using the following url:
The API is working as expected, but not on our desired port. We need to change the port to 5000.
Step 7
We go through the documentation https://fastapi.tiangolo.com/de/deployment/manually/
uvicorn main:app --reload --port 5000