Skip to content

Extending the API

FastAPI

FastAPI is a high-performant REST API framework for Python. It's built on top of Starlette, an ASGI (Asynchronous Server Gateway Interface) implementation for Python, and it uses Pydantic for data validation. It can generate OpenAPI documentation from your code and also produces a Swagger UI that you can use to test your application. OpenAPI uses a subset of JSON Schema to describe APIs and define the validation rules of the API payloads and parameters.

To run FastAPI applications, we use the process manager uvicorn. Check out the official documentation for more details.

FastAPI

Codebase structure

The API is grouped by features.

Features

The API has a feature-based folder structure following the principles of Clean Architecture.

├── api/
│   └── src/
│       ├── common/
│       ├── entities/
│       ├── features/
│       │   ├── health_check/
│       │   ├── todo/
│       │   ├── whoami/
│       │   └── ...
│       ├── data_providers/
│       └── tests/
│           ├── unit/
│           └── integration/
└── ...
  • common contains shared code like authentication, exceptions, response decorator
  • entities contains all entities, enums, exceptions, interfaces, types and logic specific to the domain layer
  • features contains use-cases (application logic), repository interfaces, and controllers
  • data providers contains classes for accessing external resources such as databases, file systems, web services, and repository implementations
  • tests contains unit and integrations tests

Get started

  1. Create the domain model by adding entities
  2. Extend the API by adding features
  3. Add a use case to handle application logic
  4. Add a controller to handle API requests
    • Add an endpoint to the controller that executes the use case
  5. Add a data provider, repository interface and repository to handle communication to external services such as databases.

Note

Entities and data providers can be shared between features (the entrypoints and use-cases).

Configuration

All configuration parameters are expected to be environment variables, and are defined in this file api/src/config.py.

--8<-- "api/src/app/config.py"

See configuration for a description of the different configuration options available.