Skip to content

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

๐Ÿ“ค ๐Ÿ“š ๐Ÿ”ข ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšถโ€โ™€๏ธ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ”— โšซ๏ธ.

Warning

๐Ÿ‘€ ๐Ÿ‘ˆ ๐Ÿ‘ซ ๐Ÿ”ข ๐Ÿšถโ€โ™€๏ธ ๐Ÿ”— โžก ๐Ÿ› ๏ธ ๐Ÿ‘จโ€๐ŸŽจ, ๐Ÿšซ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข.

๐Ÿ“จ ๐Ÿ‘” ๐Ÿ“Ÿ

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”ฌ (๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ”) status_code โš™๏ธ ๐Ÿ“จ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ.

๐Ÿ‘† ๐Ÿ’ช ๐Ÿšถโ€โ™€๏ธ ๐Ÿ”— int ๐Ÿ“Ÿ, ๐Ÿ’– 404.

โœ‹๏ธ ๐Ÿšฅ ๐Ÿ‘† ๐Ÿšซ ๐Ÿ’ญ โšซ๏ธโ” ๐Ÿ”  ๐Ÿ”ข ๐Ÿ“Ÿ, ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ โŒจ ๐Ÿ“‰ status:

from typing import Set, Union

from fastapi import FastAPI, status
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, status_code=status.HTTP_201_CREATED)
async def create_item(item: Item):
    return item
from typing import Union

from fastapi import FastAPI, status
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, status_code=status.HTTP_201_CREATED)
async def create_item(item: Item):
    return item
from fastapi import FastAPI, status
from pydantic import BaseModel

app = FastAPI()


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


@app.post("/items/", response_model=Item, status_code=status.HTTP_201_CREATED)
async def create_item(item: Item):
    return item

๐Ÿ‘ˆ ๐Ÿ‘” ๐Ÿ“Ÿ ๐Ÿ”œ โš™๏ธ ๐Ÿ“จ & ๐Ÿ”œ ๐Ÿšฎ ๐Ÿ—„ ๐Ÿ”—.

๐Ÿ“ก โ„น

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ from starlette import status.

FastAPI ๐Ÿšš ๐ŸŽ starlette.status fastapi.status ๐Ÿช ๐Ÿ‘†, ๐Ÿ‘ฉโ€๐Ÿ’ป. โœ‹๏ธ โšซ๏ธ ๐Ÿ‘Ÿ ๐Ÿ”— โšช๏ธโžก๏ธ ๐Ÿ’ƒ.

๐Ÿ”–

๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ ๐Ÿ”– ๐Ÿ‘† โžก ๐Ÿ› ๏ธ, ๐Ÿšถโ€โ™€๏ธ ๐Ÿ”ข tags โฎ๏ธ list str (๐Ÿ›Ž 1๏ธโƒฃ str):

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, tags=["items"])
async def create_item(item: Item):
    return item


@app.get("/items/", tags=["items"])
async def read_items():
    return [{"name": "Foo", "price": 42}]


@app.get("/users/", tags=["users"])
async def read_users():
    return [{"username": "johndoe"}]
from typing import 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, tags=["items"])
async def create_item(item: Item):
    return item


@app.get("/items/", tags=["items"])
async def read_items():
    return [{"name": "Foo", "price": 42}]


@app.get("/users/", tags=["users"])
async def read_users():
    return [{"username": "johndoe"}]
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


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


@app.post("/items/", response_model=Item, tags=["items"])
async def create_item(item: Item):
    return item


@app.get("/items/", tags=["items"])
async def read_items():
    return [{"name": "Foo", "price": 42}]


@app.get("/users/", tags=["users"])
async def read_users():
    return [{"username": "johndoe"}]

๐Ÿ‘ซ ๐Ÿ”œ ๐Ÿšฎ ๐Ÿ—„ ๐Ÿ”— & โš™๏ธ ๐Ÿง ๐Ÿงพ ๐Ÿ”ข:

๐Ÿ”– โฎ๏ธ ๐Ÿ”ข

๐Ÿšฅ ๐Ÿ‘† โœ”๏ธ ๐Ÿฆ ๐Ÿˆธ, ๐Ÿ‘† 5๏ธโƒฃ๐Ÿ“† ๐Ÿ”š ๐Ÿ†™ ๐Ÿ“ˆ ๐Ÿ“š ๐Ÿ”–, & ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ’š โš’ ๐Ÿ’ญ ๐Ÿ‘† ๐Ÿ•ง โš™๏ธ ๐ŸŽ ๐Ÿ”– ๐Ÿ”— โžก ๐Ÿ› ๏ธ.

๐Ÿ‘ซ ๐Ÿ’ผ, โšซ๏ธ ๐Ÿ’ช โš’ ๐Ÿ”‘ ๐Ÿช ๐Ÿ”– Enum.

FastAPI ๐Ÿ•โ€๐Ÿฆบ ๐Ÿ‘ˆ ๐ŸŽ ๐ŸŒŒ โฎ๏ธ โœ… ๐ŸŽป:

from enum import Enum

from fastapi import FastAPI

app = FastAPI()


class Tags(Enum):
    items = "items"
    users = "users"


@app.get("/items/", tags=[Tags.items])
async def get_items():
    return ["Portal gun", "Plumbus"]


@app.get("/users/", tags=[Tags.users])
async def read_users():
    return ["Rick", "Morty"]

๐Ÿ“„ & ๐Ÿ“›

๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ summary & description:

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",
    description="Create an item with all the information, name, description, price, tax and a set of unique tags",
)
async def create_item(item: Item):
    return item
from typing import 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",
    description="Create an item with all the information, name, description, price, tax and a set of unique tags",
)
async def create_item(item: Item):
    return item
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


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


@app.post(
    "/items/",
    response_model=Item,
    summary="Create an item",
    description="Create an item with all the information, name, description, price, tax and a set of unique tags",
)
async def create_item(item: Item):
    return item

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

๐Ÿ“› ๐Ÿ˜‘ ๐Ÿ“ & ๐Ÿ“” ๐Ÿ’— โธ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ โžก ๐Ÿ› ๏ธ ๐Ÿ“› ๐Ÿ”ข #๏ธโƒฃ & 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
    """
    return item
from typing import 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
    """
    return item
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: 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
    """
    return item

โšซ๏ธ ๐Ÿ”œ โš™๏ธ ๐ŸŽ“ ๐Ÿฉบ:

๐Ÿ“จ ๐Ÿ“›

๐Ÿ‘† ๐Ÿ’ช โœ” ๐Ÿ“จ ๐Ÿ“› โฎ๏ธ ๐Ÿ”ข response_description:

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",
    response_description="The created 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
    """
    return item
from typing import 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",
    response_description="The created 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
    """
    return item
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


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


@app.post(
    "/items/",
    response_model=Item,
    summary="Create an item",
    response_description="The created 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
    """
    return item

Info

๐Ÿ‘€ ๐Ÿ‘ˆ response_description ๐Ÿ”— ๐ŸŽฏ ๐Ÿ“จ, description ๐Ÿ”— โžก ๐Ÿ› ๏ธ ๐Ÿข.

Check

๐Ÿ—„ โœ” ๐Ÿ‘ˆ ๐Ÿ”  โžก ๐Ÿ› ๏ธ ๐Ÿšš ๐Ÿ“จ ๐Ÿ“›.

, ๐Ÿšฅ ๐Ÿ‘† ๐Ÿšซ ๐Ÿšš 1๏ธโƒฃ, FastAPI ๐Ÿ”œ ๐Ÿ” ๐Ÿ— 1๏ธโƒฃ "๐Ÿ† ๐Ÿ“จ".

๐Ÿ˜ข โžก ๐Ÿ› ๏ธ

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ’ช โ„ข โžก ๐Ÿ› ๏ธ ๐Ÿ˜ข, โœ‹๏ธ ๐Ÿต โŽ โšซ๏ธ, ๐Ÿšถโ€โ™€๏ธ ๐Ÿ”ข deprecated:

from fastapi import FastAPI

app = FastAPI()


@app.get("/items/", tags=["items"])
async def read_items():
    return [{"name": "Foo", "price": 42}]


@app.get("/users/", tags=["users"])
async def read_users():
    return [{"username": "johndoe"}]


@app.get("/elements/", tags=["items"], deprecated=True)
async def read_elements():
    return [{"item_id": "Foo"}]

โšซ๏ธ ๐Ÿ”œ ๐ŸŽฏ โ„ข ๐Ÿ˜ข ๐ŸŽ“ ๐Ÿฉบ:

โœ… โ” ๐Ÿ˜ข & ๐Ÿšซ-๐Ÿ˜ข โžก ๐Ÿ› ๏ธ ๐Ÿ‘€ ๐Ÿ’–:

๐ŸŒƒ

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”— & ๐Ÿšฎ ๐Ÿ—ƒ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ’ช ๐Ÿšถโ€โ™€๏ธ ๐Ÿ”ข โžก ๐Ÿ› ๏ธ ๐Ÿ‘จโ€๐ŸŽจ.