@ -31,16 +31,16 @@ FastAPI CLI will read the entrypoint in `pyproject.toml` to know where the FastA
entrypoint = "my_app.main:app"
entrypoint = "my_app.main:app"
```
```
This way the same command can be used without having to pass a path every time.
### Use `fastapi` with a path
### Use `fastapi` with a path
When adding the entrypoint to `pyproject.toml` is not possible, or the user explicitly asks not to, or it's running an independent small app, you can pass the path to the file to the `fastapi` command:
When adding the entrypoint to `pyproject.toml` is not possible, or the user explicitly asks not to, or it's running an independent small app, you can pass the app file path to the `fastapi` command:
```bash
```bash
fastapi dev my_app/main.py
fastapi dev my_app/main.py
```
```
Prefer to set the entrypoint in `pyproject.toml` when possible.
## Use `Annotated`
## Use `Annotated`
Always prefer the `Annotated` style for parameter and dependency declarations.
Always prefer the `Annotated` style for parameter and dependency declarations.
return Item(name="Plumbus", description="All-purpose home device")
return Item(name="Plumbus", description="All-purpose home device")
```
```
**Important**: Return types or response models are what filter data ensuring no sensitive information is exposed. And they are used to serialize data with Pydantic, this is the main idea that can increase response performance.
**Important**: Return types or response models are what filter data ensuring no sensitive information is exposed. And they are used to serialize data with Pydantic (in Rust), this is the main idea that can increase response performance.
The return type doesn't have to be a Pydantic model, it could be a different type, like a list of Pydantic models, or a dict, etc.
The return type doesn't have to be a Pydantic model, it could be a different type, like a list of integers, or a dict, etc.
### When to use `response_model` instead
### When to use `response_model` instead
@ -288,7 +260,7 @@ Do not use `ORJSONResponse` or `UJSONResponse`, they are deprecated.
Instead declare a return type or response model. Pydantic will handle the data serialization on the Rust side.
Instead declare a return type or response model. Pydantic will handle the data serialization on the Rust side.
### Including Routers
## Including Routers
When declaring routers, prefer to add router level parameters like prefix, tags, etc. to the router itself, instead of in `include_router()`.
When declaring routers, prefer to add router level parameters like prefix, tags, etc. to the router itself, instead of in `include_router()`.
@ -417,24 +389,33 @@ app = FastAPI()
@dataclass
@dataclass
class CommonQueryParams:
class DatabasePaginator:
q: str | None = None
offset: int = 0
skip: int = 0
limit: int = 100
limit: int = 100
q: str | None = None
def get_page(self) -> dict:
# Simulate a page of data
return {
"offset": self.offset,
"limit": self.limit,
"q": self.q,
"items": [],
}
def get_common_params(
q: str | None = None, skip: int = 0, limit: int = 100
Use `async`*path operations* only when fully certain that the logic called inside is compatible with async and await (it's called with `await`) or doesn't block.
Use `async`*path operations* only when fully certain that the logic called inside is compatible with async and await (it's called with `await`) or that doesn't block.
```python
```python
from fastapi import FastAPI
from fastapi import FastAPI
@ -539,10 +529,14 @@ def read_items():
return {"message": result}
return {"message": result}
```
```
## Use uv
## Use uv, ruff, ty
If uv is available, use it to manage dependencies.
If uv is available, use it to manage dependencies.
If Ruff is available, use it to lint and format the code. Consider enabling the FastAPI rules.
If ty is available, use it to check types.
## SQLModel for SQL databases
## SQLModel for SQL databases
When working with SQL databases, prefer using SQLModel as it is integrated with Pydantic and will allow declaring data validation with the same models.
When working with SQL databases, prefer using SQLModel as it is integrated with Pydantic and will allow declaring data validation with the same models.
FastAPI supports these type annotations and will create a Pydantic `TypeAdapter` for them, so that types can work as normally.
FastAPI supports these type annotations and will create a Pydantic `TypeAdapter` for them, so that types can work as normally and there's no need for the custom logic and types in RootModels.