fastapi

fastapi/fastapi · updated Apr 16, 2026

$npx skills add https://github.com/fastapi/fastapi --skill fastapi
0 commentsdiscussion
summary

FastAPI best practices and conventions for clean, modern API development.

  • Use Annotated for all parameter and dependency declarations to maintain type safety, reusability, and compatibility with other contexts
  • Declare return types or response models on path operations to enable automatic validation, filtering, serialization, and OpenAPI documentation
  • Use async def only when calling async code with await ; default to regular def for blocking operations to avoid event loop blocking
skill.md

FastAPI

Official FastAPI skill to write code with best practices, keeping up to date with new versions and features.

Use the fastapi CLI

Run the development server on localhost with reload:

fastapi dev

Run the production server:

fastapi run

Add an entrypoint in pyproject.toml

FastAPI CLI will read the entrypoint in pyproject.toml to know where the FastAPI app is declared.

[tool.fastapi]
entrypoint = "my_app.main:app"

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 app file path to the fastapi command:

fastapi dev my_app/main.py

Prefer to set the entrypoint in pyproject.toml when possible.

Use Annotated

Always prefer the Annotated style for parameter and dependency declarations.

It keeps the function signatures working in other contexts, respects the types, allows reusability.

In Parameter Declarations

Use Annotated for parameter declarations, including Path, Query, Header, etc.:

from typing import Annotated

from fastapi import FastAPI, Path, Query

app = FastAPI()


@app.get("/items/{item_id}")
async def read_item(
    item_id: Annotated[int, Path(ge=1, description="The item ID")],
    q: Annotated[str | None, Query(max_length=50)] = None,
):
    return {"message": "Hello World"}

instead of:

# DO NOT DO THIS
@app.get("/items/{item_id}")
async def read_item(
    item_id: int = Path(ge=1, description="The item ID"),
    q: str | None = Query(default=None, max_length=50),
):
    return {"message": "Hello World"}

For Dependencies

Use Annotated for dependencies with Depends().

Unless asked not to, create a new type alias for the dependency to allow re-using it.

from typing import Annotated

from fastapi import Depends, FastAPI

app = FastAPI()


def get_current_user():
    return {"username": "johndoe"}


CurrentUserDep = Annotated[dict, Depends(get_current_user)]


@app.get("/items/")
async def read_item(current_user: CurrentUserDep):
    return {"message": "Hello World"}

instead of:

# DO NOT DO THIS
@app.get("/items/")
async def read_item(current_user: dict = Depends(get_current_user)):
    return {"message": "Hello World"}

Do not use Ellipsis for path operations or Pydantic models

Do not use ... as a default value for required parameters, it's not needed and not recommended.

Do this, without Ellipsis (...):

from typing import Annotated

from fastapi import FastAPI, Query
from pydantic import BaseModel, Field


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float = Field(gt=0)


app = FastAPI()


@app.post("/items/")
async def create_item(item: Item, project_id: Annotated[int, Query()]): ...

instead of this:

# DO NOT DO THIS
class Item(BaseModel):
    name: str = ...
    description: str | None = None
    price: float = Field(..., gt=0)


app = FastAPI()


@app.post("/items/")
async def create_item(item: Item, project_id: Annotated[int, Query(...)]): ...

Return Type or Response Model

When possible, include a return type. It will be used to validate, filter, document, and serialize the response.

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = None


@app.get("/items/me")
async def get_item() -> Item:
    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 (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 integers, or a dict, etc.

When to use response_model instead

If the return type is not the same as the type that you want to use to validate, filter, or serialize, use the response_model parameter on the decorator instead.

from typing import Any

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = None


@app.get("/items/me", response_model=Item)
async def get_item() -> Any:
    return {"name": "Foo", "description": "A very nice Item"}

This can be particularly useful when filtering data to expose only the public fields and avoid exposing sensitive information.

from typing import Any

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class InternalItem(BaseModel

Discussion

Product Hunt–style comments (not star reviews)
  • No comments yet — start the thread.
general reviews

Ratings

4.858 reviews
  • Chaitanya Patil· Dec 28, 2024

    fastapi fits our agent workflows well — practical, well scoped, and easy to wire into existing repos.

  • Min Khan· Dec 28, 2024

    We added fastapi from the explainx registry; install was straightforward and the SKILL.md answered most questions upfront.

  • Neel Ramirez· Dec 28, 2024

    fastapi reduced setup friction for our internal harness; good balance of opinion and flexibility.

  • Luis Gupta· Dec 16, 2024

    Registry listing for fastapi matched our evaluation — installs cleanly and behaves as described in the markdown.

  • Isabella Smith· Dec 12, 2024

    Useful defaults in fastapi — fewer surprises than typical one-off scripts, and it plays nicely with `npx skills` flows.

  • Piyush G· Nov 19, 2024

    fastapi is among the better-maintained entries we tried; worth keeping pinned for repeat workflows.

  • Kofi Ghosh· Nov 19, 2024

    Keeps context tight: fastapi is the kind of skill you can hand to a new teammate without a long onboarding doc.

  • Neel Rao· Nov 19, 2024

    I recommend fastapi for anyone iterating fast on agent tooling; clear intent and a small, reviewable surface area.

  • Min Zhang· Nov 11, 2024

    Solid pick for teams standardizing on skills: fastapi is focused, and the summary matches what you get after install.

  • Amina Ndlovu· Nov 7, 2024

    Useful defaults in fastapi — fewer surprises than typical one-off scripts, and it plays nicely with `npx skills` flows.

showing 1-10 of 58

1 / 6