Welcome to FastAPI Control 👋
FastAPI utility to implement class-based routing with controllers and dependency injection.
Install
pip install fastapi-control
Usage
from fastapi import FastAPI
from fastapi_control import (
APIController,
add_controller,
add_controllers,
controller,
get,
inject,
)
# Optionally declares an abstraction
class GreeterAbstraction:
def greet(self):
raise NotImplementedError()
# Implement the abstraction and make it available to the injection system
# using the @inject decorator
@inject(alias=GreeterAbstraction)
class GretterImplementation:
def greet(self):
return "Hello, world!"
# It is also possible to implement without abstraction and make it available
# to the injection system directly
@inject()
class SpanishGretterImplementation:
def greet(self):
return "Hola, mundo!"
@inject()
class NestedGretterImplementation:
# When the @inject decorator is used, the arguments of the __init__
# method are automatically injected (if the @inject decorator was used
# in the argument type declarations)
def __init__(self, spanish_gretter: SpanishGretterImplementation) -> None:
self.gretter = spanish_gretter
def greet(self):
return self.gretter.greet()
# With the @controller decorator and inheriting from APIController, we can
# declare class-based routing (also called controller) and it has the same
# parameters as FastAPI's APIRouter
@controller(prefix="/home")
class HomeController(APIController):
# When the @controller decorator is used, the arguments of the __init__
# method are automatically injected (if the @inject decorator was used
# in the argument type declarations)
def __init__(
self,
gretter: GreeterAbstraction,
spanish_gretter: SpanishGretterImplementation,
nested_gretter: NestedGretterImplementation,
) -> None:
self.gretter = gretter
self.spanish_gretter = spanish_gretter
self.nested_gretter = nested_gretter
# The @get decorator declares the method as a GET endpoint (there are
# also @post, @put, @delete, @patch decorators) and the behavior is the
# same as the corresponding FastAPI decorators.
@get(path="/greet")
def get_greet(self):
return self.gretter.greet()
@get(path="/spanish_greet")
def get_spanish_greet(self):
return self.spanish_gretter.greet()
@get(path="/nested_greet")
def get_nested_greet(self):
return self.nested_gretter.greet()
api = FastAPI()
# Finally, it is necessary to add the controllers to the FastAPI instance
add_controllers(api)
# If you want to have multiple FastAPI instances with different controllers,
# you can use the add_controller method to add the desired controllers to
# the desired FastAPI instance one by one.
other_api = FastAPI()
add_controller(other_api, HomeController)
Clearing the controller registry
Every @controller is registered in a global registry used by add_controllers. If you need a clean slate (for example, to isolate tests), use reset_controllers:
from fastapi_control import reset_controllers
reset_controllers() # the registry is now empty
Requirements
- Python >= 3.10
- FastAPI >= 0.141
Inspirations
This project is based on and inspired by the NEXTX and FastApi-RESTful projects.
The difference with FastApi-RESTful is that FastAPI Control implements an automatic dependency injection system independent of FastAPI.
The difference with NEXTX is that FastAPI Control only aims to solve the problem of class-based routes and automatic dependency injection, and uses the kink library for dependency injection which is still under maintenance while NEXTX uses python-inject which has not been maintained since 2020.
Many thanks to the creators and maintainers of those projects for providing inspiration and guidance for this one.
Authors
👨🏻💻 Leynier Gutiérrez González
- Website: leynier.dev
- LinkedIn: @leynier
- Github: @leynier
- Twitter: @leynier41
🤝 Contributing
Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.
Show your support
Give a ⭐️ if this project helped you!
Metadata
Release files for fastapi-control 0.5.0
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_control-0.5.0.tar.gz | 8.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_control-0.5.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.7 kB
Release files / fastapi_control-0.5.0.tar.gz
| Download URL | fastapi_control-0.5.0.tar.gz |
|---|---|
| Size | 8.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
166b196edcbbf59254bc44d4251244543c3208eea1bbd399af4cdc4c0ee44f2a
|
|
BLAKE2b-256 checksum How to use checksums |
57bf5eb68fd7e2e90457142f34a6d9de1df0381c23711f519c062ec8b629b72b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 15, 2026.
Transparency logRelease files / fastapi_control-0.5.0-py3-none-any.whl
| Download URL | fastapi_control-0.5.0-py3-none-any.whl |
|---|---|
| Size | 9.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0b3b4561c2b2636573b21882e307ead80021551af3386ad68f9431cae41a11d9
|
|
BLAKE2b-256 checksum How to use checksums |
b7c2403d1c84c4b665433f57e80daee620bcf3a6b5e6313d0022ef49a23f6947
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 15, 2026.
Transparency log