PIP-Talk - Week 42

PIP-Talk - Week 42!

The topic for this week is the web framework: FastAPI

Need to implement as simple, robust and fast API ?

Then you should have a closer look on the new FastAPI rather than Flask.

Did you know ?

FastAPI was the third most loved web framework in Stack Overflow 2021 Developer Survey and itś used by large companies like Uber and Netflix to develop some of their applications.

Why FastAPI?

FastAPI was launched in 2019, later than popular frameworks such as Django (2006) and Flask (2011), and gained popularity as it is fast and high-performant. Compared to the Flask framework, FastAPI has the following benefits,

  • Asyncio for concurrency: Called with async and await keywords
  • Pydantic for data validation: Enforces schema and detects data type at runtime
  • Swagger UI for automated documentation generation: Enable testing of API endpoints with extension /docs.
  • Security and authentication features


MIT license


  • Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available.
  • Fast to code: Increase the speed to develop features by about 200% to 300%. *
  • Fewer bugs: Reduce about 40% of human (developer) induced errors. *
  • Intuitive: Great editor support. Completion everywhere. Less time debugging.
  • Easy: Designed to be easy to use and learn. Less time reading docs.
  • Short: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.
  • Robust: Get production-ready code. With automatic interactive documentation.
  • Standards-based: Based on (and fully compatible with) the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.

Get started

install FastAPI

$ pip install fastapi
$ pip install uvicorn[standard]

Writing API with FastAPI

Implementing APIs follow the same overall structure,

  1. Define an API object, such as app = FastAPI()
  2. Define data type, using Pydantic
  3. Map HTTP method and path to the relevant Python function.
  4. Start the API application, run: bash uvicorn main:app --reload

Note: main refers to the Python filename and app refers to the API object.


Here is a couple of simple examples to write a API with FastAPI.

Writing a API with 5 lines of code

Is this is possible ? Sure, FastAPI have a very short and simple syntax.

from fastapi import FastAPI

app = FastAPI()

def read_root():
    return {"Hello World!"}

Simple example with pydantic validation

from fastapi import FastAPI
from pydantic import BaseModel

# 1. Define an API object
app = FastAPI()

# 2. Define data type
class Msg(BaseModel):
    msg: str

# 3. Map HTTP method and path to python function
async def root():
    return {"message": "Hello World. Welcome to the API home page!"}

async def function_demo_get():
    return {"message": "This is /path endpoint, use post request to transform text to uppercase"}

async def function_demo_post(inp: Msg):
    return {"message": inp.msg.upper()}

async def function_demo_get_path_id(path_id: int):
    return {"message": f"This is /path/{path_id} endpoint, use post request to retrieve result"}

# 4. Start the API application (on command line)
# !uvicorn main:app --reload

Links and resources