David's Blog

Building Fast and Efficient Web APIs with FastAPI in Python

By David Li on 2025-03-05T02:02:13.000Z

Building Fast and Efficient Web APIs with FastAPI in Python

FastAPI is a modern, fast, and efficient web framework for building APIs in Python. It is built on top of the Starlette framework and Pydantic, leveraging the latest features of the Python programming language. FastAPI is easy to use and provides automatic validation, serialization, and documentation for your API, making it a powerful and developer-friendly choice for building web applications.

In this article, we’ll explore the basics of FastAPI, including how to set up a FastAPI project, define API endpoints, and use FastAPI’s powerful features such as dependency injection and validation.

Setting Up a FastAPI Project

To start using FastAPI, first, you need to install it. You can do this using pip:

pip install fastapi

You’ll also need an ASGI server such as uvicorn to run your FastAPI application. Install it with:

pip install uvicorn

Now, let’s create a simple FastAPI application. Create a new Python file called main.py, and add the following code:

from fastapi import FastAPI

app = FastAPI()

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

In this example, we import FastAPI, create a new app instance, and define a single route using the @app.get decorator. This route listens for GET requests at the root path (”/”) and returns a JSON response.

To run the application, open a terminal and navigate to the directory containing main.py. Then, execute the following command:

uvicorn main:app --reload

This command tells uvicorn to run the FastAPI app instance defined in main.py. The --reload flag enables auto-reloading of the application whenever you make changes to the code.

Open your browser and navigate to http://127.0.0.1:8000/. You should see the JSON response {"Hello": "World"}.

Defining API Endpoints

To create more complex APIs, you can define additional routes using FastAPI’s route decorators, such as @app.get, @app.post, @app.put, and @app.delete. You can also use path parameters and query parameters to capture values from the request.

For example, let’s create a new route to retrieve an item by its ID:

from typing import Optional

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

In this example, we define a new route for GET requests to /items/{item_id}, where item_id is a path parameter. FastAPI automatically converts the path parameter to the specified type (in this case, an integer). We also define a query parameter q with a default value of None.

Using FastAPI’s Powerful Features

FastAPI offers many powerful features, such as automatic validation and serialization, dependency injection, and more.

Validation and Serialization

FastAPI uses Pydantic to automatically validate request and response data and convert it to and from JSON. To define a Pydantic model, create a new class that inherits from BaseModel and define its attributes.

For example, let’s create a simple model for an item:

from pydantic import BaseModel

class Item(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None

You can then use this model in your route functions to automatically validate the request data and serialize the response data:

@app.post("/items/")
def create_item(item: Item):
    return item

Dependency Injection

FastAPI also provides a powerful dependency injection system, allowing you to easily manage and share resources across your application.

For example, let’s create a simple database connection manager:

from fastapi import Depends

class Database:
    def connect(self):
        ## Connect to the database
        pass

    def disconnect(self):
        ## Disconnect from the database
        pass

def get_db():
    db = Database()
    db.connect()
    try:
        yield db
    finally:
        db.disconnect()

You can then use the Depends function in your route functions to automatically manage the database connection:

@app.get("/items/{item_id}")
def read_item(item_id: int, db: Database = Depends(get_db)):
    ## Use the database connection to retrieve the item
    pass

Conclusion

FastAPI is a powerful and flexible web framework for building APIs in Python. Its automatic validation, serialization, and dependency injection features make it easy to build robust and efficient web applications. With FastAPI, you can quickly build high-performance web APIs thatare easy to maintain and scale.

In this article, we’ve covered the basics of setting up a FastAPI project, defining API endpoints, and using some of FastAPI’s powerful features. As you continue to develop your application, you can explore more advanced features such as OAuth2 authentication, WebSocket support, and custom exception handlers.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-11-21.