High-Performance, Robust, Auto-docs APIs using FastAPI in Python

Building APIs and using them in your application has become an inevitable task to make your application more modular and the development process even faster and use resources efficiently.
Think of APIs as the 24*7 retail store in your locality. You can go to the retail store and buy whatever the store offers, at any time. All the customers have access to the same resource i.e. the store. As a customer you don’t have to make the product and maintain the inventory or the hassle of growing or maintaining the store.
“Why to waste resources to invent the wheel again and again use it and make it better”
A very familiar example of API in action is “Google Sign-In” which allows a web app to authenticate a user by using his google account. This helps to save resources and development time and building a centralized ecosystem of a service which can be accessed by everyone who wants to use its feature.
Python has became popular to develop web apps by using Django and flask framework. Many Multinational companies uses python for there backend services and extensively for developing and deploying Machine Learning application.
For a long time Flask has been the de-facto choice for developing API using python, but FastAPI may take this crown from Flask when it comes to develop APIs.
FastAPI
FastAPI is obviously open-source and inspired by its predecessors and is built upon many pre-existing tools.
Features of FastAPI
- Fast (High-performance)
- Fast to code
- Fewer bugs
- Intuitive
- Robust
- Production-ready
- Based on the OpenAPI and JSON schema standard.
- Automatic API Documentation ready
- Asynchronous
FastAPI is dependent on starlette for web parts and pydantic for data parts.
A Simple FastAPI implementation
To use fastapi framework we need to install the packages “fastapi”
Download the fastapi package by using the command .
pip install fastapi

Now that we have installed the fast api we will make a simple api
#import fastapi
from fastapi import FastAPI
#make the app
app = FastAPI()
#define routes/endpoints
@app.get("/")
def root():
return {"What is this url" : "It's Root"}
#define endpoint home
@app.get("/home")
def home():
return {"What is this endpoint" : " It's Home"}
The above program make two endpoint root and home which returns the text when called.
FastAPI dynamic path and Query
#import fastapi
from fastapi import FastAPI
#make the app
app = FastAPI()
#define endpoint
@app.get("/")
def root():
return {"What is this url" : "It's Root"}
@app.get("/Home")
def home():
return {"What is this endpoint" : " It's Home"}
#define end point with dynamic path and query
#example: http://127.0.0.1:8000/login/joe?q=is my name
@app.get("/login/{name}")
def login(name : str, q : str = None):
return{"Welcome" : name, "query": q}
To see the above code in action you need a development server. You can use uvicorn to fire up a development server and run the fastapi code.
To install the development serve use command
pip install uvicorn
To run the script use command
uvicorn main:app --reload
Automatic documentation and Easy debugging
One thing why developers are loving fastAPI is because it generates an automatic interactive documentation of the API and its feature/ endpoints.
you can access the docs by hitting ” http://127.0.0.1:8000/docs “

The interactive documentation by swagger UI (docs) lists all the endpoints of the API and lets you interactively test the endpoints with different paths and queries.

Anaother alernate choice of automatic documentation is ReDocs.

Stay updated with the latest advancements Explore event, webinars and conferences