FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.7+ based on standard Python type hints. It's fast because it's written in pure Python, but it's also fast because it's asynchronous. This means it can handle many requests at the same time without blocking.

Key Features

  • Asynchronous: Handle many requests at the same time without blocking.
  • Fast: High performance with Starlette and Pydantic.
  • Type Checking: Use Python type hints to validate request data and return types.
  • Documentation: Automatically generate interactive API documentation with Swagger UI.
  • Dependency Injection: Use Python's type system to automatically inject dependencies.

Getting Started

To get started with FastAPI, you can install it using pip:

pip install fastapi

Once installed, you can create a basic FastAPI app like this:

from fastapi import FastAPI

app = FastAPI()

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

Run the app with:

uvicorn your_module:app --reload

Usage

Here's a simple example of a FastAPI endpoint that returns a JSON response:

from fastapi import FastAPI, HTTPException

app = FastAPI()

@app.get("/items/{item_id}")
async def read_item(item_id: int):
    if item_id == 404:
        raise HTTPException(status_code=404, detail="Item not found")
    return {"item_id": item_id}

Resources

FastAPI Logo