Skip to content

โžก ๐Ÿ› ๏ธ ๐Ÿง ๐Ÿ“ณ

๐Ÿ—„ {

Warning

๐Ÿšฅ ๐Ÿ‘† ๐Ÿšซ "๐Ÿ•ด" ๐Ÿ—„, ๐Ÿ‘† ๐ŸŽฒ ๐Ÿšซ ๐Ÿ’ช ๐Ÿ‘‰.

๐Ÿ‘† ๐Ÿ’ช โš’ ๐Ÿ—„ operationId โš™๏ธ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ โฎ๏ธ ๐Ÿ”ข operation_id.

๐Ÿ‘† ๐Ÿ”œ โœ”๏ธ โš’ ๐Ÿ’ญ ๐Ÿ‘ˆ โšซ๏ธ ๐Ÿ˜ ๐Ÿ”  ๐Ÿ› ๏ธ.

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/", operation_id="some_specific_id_you_define")
async def read_items():
    return [{"item_id": "Foo"}]

โš™๏ธ โžก ๐Ÿ› ๏ธ ๐Ÿ”ข ๐Ÿ“› {

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ’š โš™๏ธ ๐Ÿ‘† ๐Ÿ”—' ๐Ÿ”ข ๐Ÿ“› operationIdโ“‚, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ” ๐Ÿคญ ๐ŸŒ ๐Ÿ‘ซ & ๐Ÿ” ๐Ÿ”  โžก ๐Ÿ› ๏ธ operation_id โš™๏ธ ๐Ÿ‘ซ APIRoute.name.

๐Ÿ‘† ๐Ÿ”œ โšซ๏ธ โฎ๏ธ โŽ ๐ŸŒ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ.

from fastapi import FastAPI
from fastapi.routing import APIRoute

app = FastAPI()


@app.get("/items/")
async def read_items():
    return [{"item_id": "Foo"}]


def use_route_names_as_operation_ids(app: FastAPI) -> None:
    """
    Simplify operation IDs so that generated API clients have simpler function
    names.

    Should be called only after all routes have been added.
    """
    for route in app.routes:
        if isinstance(route, APIRoute):
            route.operation_id = route.name  # in this case, 'read_items'


use_route_names_as_operation_ids(app)

Tip

๐Ÿšฅ ๐Ÿ‘† โŽ ๐Ÿค™ app.openapi(), ๐Ÿ‘† ๐Ÿ”œ โ„น operationIdโ“‚ โญ ๐Ÿ‘ˆ.

Warning

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ‘‰, ๐Ÿ‘† โœ”๏ธ โš’ ๐Ÿ’ญ ๐Ÿ”  1๏ธโƒฃ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข โœ”๏ธ ๐Ÿ˜ ๐Ÿ“›.

๐Ÿšฅ ๐Ÿ‘ซ ๐ŸŽ ๐Ÿ•น (๐Ÿ ๐Ÿ“).

๐Ÿšซ โšช๏ธโžก๏ธ ๐Ÿ—„

๐Ÿšซ โžก ๐Ÿ› ๏ธ โšช๏ธโžก๏ธ ๐Ÿ— ๐Ÿ—„ ๐Ÿ”— (& โžก๏ธ, โšช๏ธโžก๏ธ ๐Ÿง ๐Ÿงพ โš™๏ธ), โš™๏ธ ๐Ÿ”ข include_in_schema & โš’ โšซ๏ธ False:

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/", include_in_schema=False)
async def read_items():
    return [{"item_id": "Foo"}]

๐Ÿง ๐Ÿ“› โšช๏ธโžก๏ธ #๏ธโƒฃ

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“‰ โธ โš™๏ธ โšช๏ธโžก๏ธ #๏ธโƒฃ โžก ๐Ÿ› ๏ธ ๐Ÿ”ข ๐Ÿ—„.

โŽ \f (๐Ÿ˜– "๐Ÿ“จ ๐Ÿผ" ๐Ÿฆน) ๐Ÿค• FastAPI ๐Ÿ” ๐Ÿ”ข โš™๏ธ ๐Ÿ—„ ๐Ÿ‘‰ โ˜.

โšซ๏ธ ๐Ÿ† ๐Ÿšซ ๐ŸŽฆ ๐Ÿ†™ ๐Ÿงพ, โœ‹๏ธ ๐ŸŽ ๐Ÿงฐ (โœ… ๐Ÿ‰) ๐Ÿ”œ ๐Ÿ’ช โš™๏ธ ๐ŸŽ‚.

from typing import Set, Union

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: Union[str, None] = None
    price: float
    tax: Union[float, None] = None
    tags: Set[str] = set()


@app.post("/items/", response_model=Item, summary="Create an item")
async def create_item(item: Item):
    """
    Create an item with all the information:

    - **name**: each item must have a name
    - **description**: a long description
    - **price**: required
    - **tax**: if the item doesn't have tax, you can omit this
    - **tags**: a set of unique tag strings for this item
    \f
    :param item: User input.
    """
    return item

๐ŸŒ– ๐Ÿ“จ

๐Ÿ‘† ๐ŸŽฒ โœ”๏ธ ๐Ÿ‘€ โ” ๐Ÿ“ฃ response_model & status_code โžก ๐Ÿ› ๏ธ.

๐Ÿ‘ˆ ๐Ÿ”ฌ ๐Ÿ—ƒ ๐Ÿ”ƒ ๐Ÿ‘‘ ๐Ÿ“จ โžก ๐Ÿ› ๏ธ.

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐ŸŒ– ๐Ÿ“จ โฎ๏ธ ๐Ÿ‘ซ ๐Ÿท, ๐Ÿ‘” ๐Ÿ“Ÿ, โ™’๏ธ.

๐Ÿ“ค ๐ŸŽ‚ ๐Ÿ“ƒ ๐Ÿ“ฅ ๐Ÿงพ ๐Ÿ”ƒ โšซ๏ธ, ๐Ÿ‘† ๐Ÿ’ช โœ โšซ๏ธ ๐ŸŒ– ๐Ÿ“จ ๐Ÿ—„.

๐Ÿ—„ โž•

๐Ÿ•โ” ๐Ÿ‘† ๐Ÿ“ฃ โžก ๐Ÿ› ๏ธ ๐Ÿ‘† ๐Ÿˆธ, FastAPI ๐Ÿ” ๐Ÿ— ๐Ÿ”— ๐Ÿ—ƒ ๐Ÿ”ƒ ๐Ÿ‘ˆ โžก ๐Ÿ› ๏ธ ๐Ÿ”Œ ๐Ÿ—„ ๐Ÿ”—.

๐Ÿ“ก โ„น

๐Ÿ—„ ๐Ÿ”ง โšซ๏ธ ๐Ÿค™ ๐Ÿ› ๏ธ ๐ŸŽš.

โšซ๏ธ โœ”๏ธ ๐ŸŒ โ„น ๐Ÿ”ƒ โžก ๐Ÿ› ๏ธ & โš™๏ธ ๐Ÿ— ๐Ÿง ๐Ÿงพ.

โšซ๏ธ ๐Ÿ”Œ tags, parameters, requestBody, responses, โ™’๏ธ.

๐Ÿ‘‰ โžก ๐Ÿ› ๏ธ-๐ŸŽฏ ๐Ÿ—„ ๐Ÿ”— ๐Ÿ›Ž ๐Ÿ— ๐Ÿ” FastAPI, โœ‹๏ธ ๐Ÿ‘† ๐Ÿ’ช โ†” โšซ๏ธ.

Tip

๐Ÿ‘‰ ๐Ÿ”… ๐ŸŽš โ†” โ˜.

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ•ด ๐Ÿ’ช ๐Ÿ“ฃ ๐ŸŒ– ๐Ÿ“จ, ๐ŸŒ… ๐Ÿช ๐ŸŒŒ โšซ๏ธ โฎ๏ธ ๐ŸŒ– ๐Ÿ“จ ๐Ÿ—„.

๐Ÿ‘† ๐Ÿ’ช โ†” ๐Ÿ—„ ๐Ÿ”— โžก ๐Ÿ› ๏ธ โš™๏ธ ๐Ÿ”ข openapi_extra.

๐Ÿ—„ โ†”

๐Ÿ‘‰ openapi_extra ๐Ÿ’ช ๐Ÿ‘, ๐Ÿ–ผ, ๐Ÿ“ฃ ๐Ÿ—„ โ†”:

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/", openapi_extra={"x-aperture-labs-portal": "blue"})
async def read_items():
    return [{"item_id": "portal-gun"}]

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ“‚ ๐Ÿง ๐Ÿ› ๏ธ ๐Ÿฉบ, ๐Ÿ‘† โ†” ๐Ÿ”œ ๐ŸŽฆ ๐Ÿ†™ ๐Ÿ” ๐ŸŽฏ โžก ๐Ÿ› ๏ธ.

& ๐Ÿšฅ ๐Ÿ‘† ๐Ÿ‘€ ๐Ÿ“‰ ๐Ÿ—„ ( /openapi.json ๐Ÿ‘† ๐Ÿ› ๏ธ), ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ ๐Ÿ‘† โ†” ๐Ÿ• ๐ŸŽฏ โžก ๐Ÿ› ๏ธ ๐Ÿ’โ€โ™‚๏ธ:

{
    "openapi": "3.0.2",
    "info": {
        "title": "FastAPI",
        "version": "0.1.0"
    },
    "paths": {
        "/items/": {
            "get": {
                "summary": "Read Items",
                "operationId": "read_items_items__get",
                "responses": {
                    "200": {
                        "description": "Successful Response",
                        "content": {
                            "application/json": {
                                "schema": {}
                            }
                        }
                    }
                },
                "x-aperture-labs-portal": "blue"
            }
        }
    }
}

๐Ÿ›ƒ ๐Ÿ—„ โžก ๐Ÿ› ๏ธ ๐Ÿ”—

๐Ÿ“– openapi_extra ๐Ÿ”œ ๐Ÿ™‡ ๐Ÿ”— โฎ๏ธ ๐Ÿ” ๐Ÿ— ๐Ÿ—„ ๐Ÿ”— โžก ๐Ÿ› ๏ธ.

, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ ๐ŸŒ– ๐Ÿ’ฝ ๐Ÿ” ๐Ÿ— ๐Ÿ”—.

๐Ÿ–ผ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’ญ โœ & โœ” ๐Ÿ“จ โฎ๏ธ ๐Ÿ‘† ๐Ÿ‘ ๐Ÿ“Ÿ, ๐Ÿต โš™๏ธ ๐Ÿง โš’ FastAPI โฎ๏ธ Pydantic, โœ‹๏ธ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’š ๐Ÿ”ฌ ๐Ÿ“จ ๐Ÿ—„ ๐Ÿ”—.

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ‘ˆ โฎ๏ธ openapi_extra:

from fastapi import FastAPI, Request

app = FastAPI()


def magic_data_reader(raw_body: bytes):
    return {
        "size": len(raw_body),
        "content": {
            "name": "Maaaagic",
            "price": 42,
            "description": "Just kiddin', no magic here. โœจ",
        },
    }


@app.post(
    "/items/",
    openapi_extra={
        "requestBody": {
            "content": {
                "application/json": {
                    "schema": {
                        "required": ["name", "price"],
                        "type": "object",
                        "properties": {
                            "name": {"type": "string"},
                            "price": {"type": "number"},
                            "description": {"type": "string"},
                        },
                    }
                }
            },
            "required": True,
        },
    },
)
async def create_item(request: Request):
    raw_body = await request.body()
    data = magic_data_reader(raw_body)
    return data

๐Ÿ‘‰ ๐Ÿ–ผ, ๐Ÿ‘ฅ ๐Ÿšซ ๐Ÿ“ฃ ๐Ÿ™† Pydantic ๐Ÿท. ๐Ÿ‘, ๐Ÿ“จ ๐Ÿ’ช ๐Ÿšซ ๐ŸŽป ๐ŸŽป, โšซ๏ธ โœ ๐Ÿ”— bytes, & ๐Ÿ”ข magic_data_reader() ๐Ÿ”œ ๐Ÿˆš ๐ŸŽป โšซ๏ธ ๐ŸŒŒ.

๐Ÿ‘, ๐Ÿ‘ฅ ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ“ˆ ๐Ÿ”— ๐Ÿ“จ ๐Ÿ’ช.

๐Ÿ›ƒ ๐Ÿ—„ ๐ŸŽš ๐Ÿ†Ž

โš™๏ธ ๐Ÿ‘‰ ๐ŸŽ ๐ŸŽฑ, ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ Pydantic ๐Ÿท ๐Ÿ”ฌ ๐ŸŽป ๐Ÿ”— ๐Ÿ‘ˆ โคด๏ธ ๐Ÿ”Œ ๐Ÿ›ƒ ๐Ÿ—„ ๐Ÿ”— ๐Ÿ“„ โžก ๐Ÿ› ๏ธ.

& ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ‘‰ ๐Ÿšฅ ๐Ÿ’ฝ ๐Ÿ†Ž ๐Ÿ“จ ๐Ÿšซ ๐ŸŽป.

๐Ÿ–ผ, ๐Ÿ‘‰ ๐Ÿˆธ ๐Ÿ‘ฅ ๐Ÿšซ โš™๏ธ FastAPI ๐Ÿ› ๏ธ ๐Ÿ› ๏ธ โš— ๐ŸŽป ๐Ÿ”— โšช๏ธโžก๏ธ Pydantic ๐Ÿท ๐Ÿšซ ๐Ÿง ๐Ÿ”ฌ ๐ŸŽป. ๐Ÿ‘, ๐Ÿ‘ฅ ๐Ÿ“ฃ ๐Ÿ“จ ๐ŸŽš ๐Ÿ†Ž ๐Ÿ“, ๐Ÿšซ ๐ŸŽป:

from typing import List

import yaml
from fastapi import FastAPI, HTTPException, Request
from pydantic import BaseModel, ValidationError

app = FastAPI()


class Item(BaseModel):
    name: str
    tags: List[str]


@app.post(
    "/items/",
    openapi_extra={
        "requestBody": {
            "content": {"application/x-yaml": {"schema": Item.model_json_schema()}},
            "required": True,
        },
    },
)
async def create_item(request: Request):
    raw_body = await request.body()
    try:
        data = yaml.safe_load(raw_body)
    except yaml.YAMLError:
        raise HTTPException(status_code=422, detail="Invalid YAML")
    try:
        item = Item.model_validate(data)
    except ValidationError as e:
        raise HTTPException(status_code=422, detail=e.errors())
    return item

๐Ÿ‘, ๐Ÿ‘ ๐Ÿ‘ฅ ๐Ÿšซ โš™๏ธ ๐Ÿ”ข ๐Ÿ› ๏ธ ๐Ÿ› ๏ธ, ๐Ÿ‘ฅ โš™๏ธ Pydantic ๐Ÿท โŽ ๐Ÿ— ๐ŸŽป ๐Ÿ”— ๐Ÿ’ฝ ๐Ÿ‘ˆ ๐Ÿ‘ฅ ๐Ÿ’š ๐Ÿ“จ ๐Ÿ“.

โคด๏ธ ๐Ÿ‘ฅ โš™๏ธ ๐Ÿ“จ ๐Ÿ”—, & โš— ๐Ÿ’ช bytes. ๐Ÿ‘‰ โ›“ ๐Ÿ‘ˆ FastAPI ๐Ÿ† ๐Ÿšซ ๐Ÿ”„ ๐ŸŽป ๐Ÿ“จ ๐Ÿš€ ๐ŸŽป.

& โคด๏ธ ๐Ÿ‘† ๐Ÿ“Ÿ, ๐Ÿ‘ฅ ๐ŸŽป ๐Ÿ‘ˆ ๐Ÿ“ ๐ŸŽš ๐Ÿ”—, & โคด๏ธ ๐Ÿ‘ฅ ๐Ÿ”„ โš™๏ธ ๐ŸŽ Pydantic ๐Ÿท โœ” ๐Ÿ“ ๐ŸŽš:

from typing import List

import yaml
from fastapi import FastAPI, HTTPException, Request
from pydantic import BaseModel, ValidationError

app = FastAPI()


class Item(BaseModel):
    name: str
    tags: List[str]


@app.post(
    "/items/",
    openapi_extra={
        "requestBody": {
            "content": {"application/x-yaml": {"schema": Item.model_json_schema()}},
            "required": True,
        },
    },
)
async def create_item(request: Request):
    raw_body = await request.body()
    try:
        data = yaml.safe_load(raw_body)
    except yaml.YAMLError:
        raise HTTPException(status_code=422, detail="Invalid YAML")
    try:
        item = Item.model_validate(data)
    except ValidationError as e:
        raise HTTPException(status_code=422, detail=e.errors())
    return item

Tip

๐Ÿ“ฅ ๐Ÿ‘ฅ ๐Ÿค-โš™๏ธ ๐ŸŽ Pydantic ๐Ÿท.

โœ‹๏ธ ๐ŸŽ ๐ŸŒŒ, ๐Ÿ‘ฅ ๐Ÿ’ช โœ”๏ธ โœ” โšซ๏ธ ๐ŸŽ ๐ŸŒŒ.