FastAPI Endpoints
This is a file-based router for FastAPI that automatically discovers and registers route files based on their filenames.
This tool simplifies the organization and scaling of your FastAPI projects by allowing you to structure your endpoints in a modular way.
With fastapi-endpoints, you can easily manage complex applications by keeping your routes clean, intuitive, and maintainable.
This project is inspired from SettleAPI/settle-fastapi-extensions
Please refer to the documentation here:
Installation
pip install fastapi-endpoints
Usage
Auto discovery
The auto discovery feature allows you to organize your routes in separate files and directories and will be automatically registered by the router.
All routers should be under the routers directory within your FastAPI application.
# src/app.py
from fastapi import FastAPI
from fastapi_endpoints import auto_include_routers
from . import routers
app = FastAPI()
auto_include_routers(app, routers)
# src/routers/users.py
from fastapi import APIRouter
users_router = APIRouter()
# Define your routes here
The routes under src/routers/users.py will be automatically registered by the router. The prefix to the routes will be the path to the file relative to the routers directory.
For the example above, the routes will be available under /users.
The auto discovery feature also supports nested directories. For example, if you have the following directory structure:
routers
|── __init__.py
├── api_v1
│ ├── __init__.py
│ ├── users.py
│ └── posts.py
└── api_v2
├── __init__.py
├── users.py
└── posts.py
app.py
The routes under src/routers/api_v1/users.py will be available under /api/v1/users.
The same applies to the other files. The routes under src/routers/api_v2/users.py will be available under /api/v2/users.
If you want to have a directory specific for multiple routes, you can create a root.py file within the directory. The routes defined in the root.py file will be registered under the directory prefix. For example, if you have the following directory structure:
routers
|── __init__.py
├── api_v1
│ ├── __init__.py
│ ├── users.py
│ ├── posts.py
│ └── tables
│ ├── __init__.py
│ ├── docs.py
│ └── root.py
app.py
The routes under src/routers/api_v1/tables/root.py will be available under /api/v1/tables, while the routes under src/routers/api_v1/tables/docs.py will be available under /api/v1/tables/docs.
Exclude Routers
You can exclude routers from being registered by the router by defining the EXCLUDED_ROUTERS variable in the __init__.py file of any submodule of the routers module. All excluded routers will be bundled together and excluded from the registration process.
For example, if you have the following directory structure:
routers
|── __init__.py
├── api_v1
│ ├── __init__.py
│ ├── users.py
│ └── posts.py
app.py
You can exclude the users.py router from being registered by defining the EXCLUDED_ROUTERS variable in the api_v1/__init__.py file.
# src/routers/api_v1/__init__.py
from . import users
EXCLUDED_ROUTERS = [users]
You can also exclude an entire directory by defining the EXCLUDED_ROUTERS variable in the __init__.py file of the directory.
# src/routers/__init__.py
from . import api_v1
EXCLUDED_ROUTERS = [api_v1]
Development
You are required to have Poetry installed in your system to manage the dependencies.
Setup
poetry install
Running tests
pytest
Linting & Formatting
To check the format:
ruff format --check src/
To check the lint:
ruff check src/
After running both commands, the format errors can be applied by running the formatter without the --check flag.
Metadata
Release files for fastapi-endpoints 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fastapi_endpoints-0.1.2.tar.gz | 4.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_endpoints-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.8 kB
Release files / fastapi_endpoints-0.1.2.tar.gz
| Download URL | fastapi_endpoints-0.1.2.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f9c2cef7c8a06ef971f211c62a319046aa6e2cf9139d1b360d57056940b2ddf5
|
|
BLAKE2b-256 checksum How to use checksums |
6f0e41fe0deb845d8c34bc102da47065f0ca2514097337d98757c637de7b0a45
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.9
|
Release files / fastapi_endpoints-0.1.2-py3-none-any.whl
| Download URL | fastapi_endpoints-0.1.2-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
553c8ad67e4bed5dd3df76fb1eaa5badc1c51942a44f71bfd5d268141f9e2dc5
|
|
BLAKE2b-256 checksum How to use checksums |
3ec6ccd58143fe43ffc8e7feda996c7057664c6838875b2bb3ead01558d09368
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.9
|