FastAPI

Prince Prashant Saini
5 min readApr 12, 2021

What is Fast API🤷‍♂️

FastAPI is a Python framework and set of tools that enable developers to use a REST interface to call commonly used functions to implement applications. It is accessed through a REST API to call common building blocks for an app.

🤷‍♂️Requirements🤷‍♂️

Python 3.6+

🤔first, create Virtual Env🤔

pip install virtualenv

🤔Then activate Virtual env🤔

virtualenv env #we have checking env
.\env\Scripts\activate.ps1 #
here ps1 is vs powershell 1

🤔if last command not working use this command in powershell admin

Set-ExecutionPolicy unrestricted

🤔Then activate and install Flask

pip install flask

🤔Then we install FastAPI

pip install fastapi

🤔Then we install the ASGI server

pip install uvicorn[standard]

😊Now we create our own code

we have imported the module of FastAPI then we have created two routes one is our home page “/” and the other is prefix that is “/items/{item_id}” both paths take GET request 😎 . Now in both, we have iteam_id and q. Here itea_id we used int that means we give integer here then in q we give str put it optional but we have to give sting only if we not print any think they show null

from typing import Optional

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def read_root():
return {"Hello": "Prince Prashant Saini"}


@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
return {"item_id": item_id, "q": q}

🤔Now we have run with help of ASGI server 🤔

uvicorn main:app

😁Now check webpage

now working ✌✌😎

Now see we have also give /iteam/{iteam_id}
we have given two key-value pair “item_id”: item_id, “q”: q we have give many but we use this two so we have use this

so we have use this link

http://127.0.0.1:8000/items/9?q=pps

now working both ✌✌✌😎

Now see intrective API😁 they give automated API when we use this link

http://127.0.0.1:8000/docs

now here we have the same keys item_id and 1 now we have put on that same value 9 and pps . here we have the same is item_id is required and q is not

if q is empty they show null always 😒

Then we see redoc they alternative automatic documentation we also check this if they give 200 its means success other then 200 or red it means we have missed something or error. Here👇They show both its means one time working fine and one time give me error

http://127.0.0.1:8000/redoc

if we add one more route for get for company details so we have create one more function for a company here we have appended all the company details then fetch

from typing import Optional

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def read_root():
return {"Hello": "Prince Prashant Saini"}


@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
return {"item_id": item_id, "q": q}

@app.get("/company/{company_name}")
def read_item(company_name: str):
return {"comapny_name": company_name}

now added one more route now test save with Ctrl+S they auto reloaded because we use — reload

uvicorn main:app — reload

now it loaded check-in website its work or not

http://127.0.0.1:8000/company/katonics.ai

it gives output so it means we have created soo many routes that are we have needed ✌😂

Now PUT

Now for put you have to create new file or modify main.py its your choice because we use reload so we use same

so for this we have pydantic
Basic model usage. from pydantic import BaseModel class User(BaseModel): id: int name = ‘Jane Doe’ User here is a model with two fields id which is an integer and is required, and name which is a string and is not required (it has a default value
Data validation and settings management using python type annotations. pydantic enforces type hints at runtime, and provides user friendly errors when data is invalid

now we created our own code for that. Here we import the modules then we have create one class for items like name , price , is_offer is optional . then we have create routes first routes for home “/” then we have one items route that we have to get method so we have use app.get then last we have put method so we have use app.put here we have to use item_id and item

from typing import Optional

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
name: str
price: float
is_offer: Optional[bool] = None


@app.get("/")
def read_root():
return {"Hello": "World"}


@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
return {"item_id": item_id, "q": q}


@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
return {"item_name": item.name, "item_id": item_id}

Then ctrl+S then its reload automatically because we have used — reload now after reload we have to check

now we have open docs for that

http://127.0.0.1:8000/docs

now its work

Now we change something like we have change at last item.name to item.price

--

--