FastAPI

FastAPI is a modern, high-performance Python web framework for building APIs.
It is built on Starlette and Pydantic(specialized libraries to provide its high-performance web capabilities and data management), and uses Python type hints for validation and automatic OpenAPI documentation.
Starlette (Web Network) + Pydantic (Data Validation) + Automatic Documentation & Tools
Features:
1. Fast to run — comparable to Node.js and Go for API workloads.
2. Automatic request/response validation via Pydantic models.
3. Interactive API docs at /docs (Swagger UI) and /redoc.
4. Native async/await support for I/O-bound endpoints.

Install


# In venv
pip3 install fastapi
pip3 install uvicorn    # Install ASGI server called uvicorn to run FastAPI app
      

Create, Run FastAPI Project

uvicorn

Start Uvicorn from Command line
# cat main.py
from fastapi import FastAPI

app = FastAPI()                   # Create Object of FastAPI class

@app.get("/")                     # get() for REST endpoint
async def index():
   resp_json = {"message": "Hello World"}
   return resp_json

// Run the project
# uvicorn main:app --reload --port 8001
Uvicorn running on http://127.0.0.1:8001          << Open browser
      
Starting Uvicorn Server Programmatically
# cat test.py
import uvicorn
from fastapi import FastAPI
app = FastAPI()

@app.get("/")
async def index():
   return {"message": "Hello World"}

if __name__ == "__main__":
   uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)

# python test.py
            

Inbuilt Rest Endpoints

docs

No need to write documentation. Just open http://127.0.0.1:8001/docs to get information of all REST endpoints exposed by app.
FastAPI uses Swagger UI to produce this documentation.

http://127.0.0.1:8001/docs
fastapi docs
Click on 'try it out' > 'Execute' > Curl command internally executed
The request URL, the response headers, and the JSON format of the servers response.
fastapi docs execute