Skip to content

๐Ÿ—ƒ & ๐Ÿฉบ ๐Ÿ“›

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ›ƒ ๐Ÿ“š ๐Ÿ—ƒ ๐Ÿ“ณ ๐Ÿ‘† FastAPI ๐Ÿˆธ.

๐Ÿ—ƒ ๐Ÿ› ๏ธ

๐Ÿ‘† ๐Ÿ’ช โš’ ๐Ÿ“„ ๐Ÿ‘ ๐Ÿ‘ˆ โš™๏ธ ๐Ÿ—„ ๐Ÿ”ง & ๐Ÿง ๐Ÿ› ๏ธ ๐Ÿฉบ โšœ:

๐Ÿ”ข ๐Ÿ†Ž ๐Ÿ“›
title str ๐Ÿ“› ๐Ÿ› ๏ธ.
description str ๐Ÿ“ ๐Ÿ“› ๐Ÿ› ๏ธ. โšซ๏ธ ๐Ÿ’ช โš™๏ธ โœ.
version string โฌ ๐Ÿ› ๏ธ. ๐Ÿ‘‰ โฌ ๐Ÿ‘† ๐Ÿ‘ ๐Ÿˆธ, ๐Ÿšซ ๐Ÿ—„. ๐Ÿ–ผ 2.5.0.
terms_of_service str ๐Ÿ“› โš– ๐Ÿ•โ€๐Ÿฆบ ๐Ÿ› ๏ธ. ๐Ÿšฅ ๐Ÿšš, ๐Ÿ‘‰ โœ”๏ธ ๐Ÿ“›.
contact dict ๐Ÿ“ง โ„น ๐ŸŽฆ ๐Ÿ› ๏ธ. โšซ๏ธ ๐Ÿ’ช ๐Ÿ”Œ ๐Ÿ“š ๐Ÿ‘.
contact ๐Ÿ‘
๐Ÿ”ข๐Ÿ†Ž๐Ÿ“›
namestrโš– ๐Ÿ“› ๐Ÿ“ง ๐Ÿ‘จโ€๐Ÿ’ผ/๐Ÿข.
urlstr๐Ÿ“› โ˜ ๐Ÿ“ง โ„น. ๐Ÿ”œ ๐Ÿ“ ๐Ÿ“›.
emailstr๐Ÿ“ง ๐Ÿ“ข ๐Ÿ“ง ๐Ÿ‘จโ€๐Ÿ’ผ/๐Ÿข. ๐Ÿ”œ ๐Ÿ“ ๐Ÿ“ง ๐Ÿ“ข.
license_info dict ๐Ÿ›‚ โ„น ๐ŸŽฆ ๐Ÿ› ๏ธ. โšซ๏ธ ๐Ÿ’ช ๐Ÿ”Œ ๐Ÿ“š ๐Ÿ‘.
license_info ๐Ÿ‘
๐Ÿ”ข๐Ÿ†Ž๐Ÿ“›
namestr๐Ÿšš (๐Ÿšฅ license_info โš’). ๐Ÿ›‚ ๐Ÿ“› โš™๏ธ ๐Ÿ› ๏ธ.
urlstr๐Ÿ“› ๐Ÿ›‚ โš™๏ธ ๐Ÿ› ๏ธ. ๐Ÿ”œ ๐Ÿ“ ๐Ÿ“›.

๐Ÿ‘† ๐Ÿ’ช โš’ ๐Ÿ‘ซ โฉ:

from fastapi import FastAPI

description = """
ChimichangApp API helps you do awesome stuff. ๐Ÿš€

## Items

You can **read items**.

## Users

You will be able to:

* **Create users** (_not implemented_).
* **Read users** (_not implemented_).
"""

app = FastAPI(
    title="ChimichangApp",
    description=description,
    summary="Deadpool's favorite app. Nuff said.",
    version="0.0.1",
    terms_of_service="http://example.com/terms/",
    contact={
        "name": "Deadpoolio the Amazing",
        "url": "http://x-force.example.com/contact/",
        "email": "dp@x-force.example.com",
    },
    license_info={
        "name": "Apache 2.0",
        "url": "https://www.apache.org/licenses/LICENSE-2.0.html",
    },
)


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

Tip

๐Ÿ‘† ๐Ÿ’ช โœ โœ description ๐Ÿ‘ & โšซ๏ธ ๐Ÿ”œ โœ ๐Ÿ”ข.

โฎ๏ธ ๐Ÿ‘‰ ๐Ÿ“ณ, ๐Ÿง ๐Ÿ› ๏ธ ๐Ÿฉบ ๐Ÿ”œ ๐Ÿ‘€ ๐Ÿ’–:

๐Ÿ—ƒ ๐Ÿ”–

๐Ÿ‘† ๐Ÿ’ช ๐Ÿšฎ ๐ŸŒ– ๐Ÿ—ƒ ๐ŸŽ ๐Ÿ”– โš™๏ธ ๐Ÿ‘ช ๐Ÿ‘† โžก ๐Ÿ› ๏ธ โฎ๏ธ ๐Ÿ”ข openapi_tags.

โšซ๏ธ โœŠ ๐Ÿ“‡ โš— 1๏ธโƒฃ ๐Ÿ“– ๐Ÿ”  ๐Ÿ”–.

๐Ÿ”  ๐Ÿ“– ๐Ÿ’ช ๐Ÿ”Œ:

  • name (โœ”): str โฎ๏ธ ๐ŸŽ ๐Ÿ“› ๐Ÿ‘† โš™๏ธ tags ๐Ÿ”ข ๐Ÿ‘† โžก ๐Ÿ› ๏ธ & APIRouterโ“‚.
  • description: str โฎ๏ธ ๐Ÿ“ ๐Ÿ“› ๐Ÿ”–. โšซ๏ธ ๐Ÿ’ช โœ”๏ธ โœ & ๐Ÿ”œ ๐ŸŽฆ ๐Ÿฉบ ๐ŸŽš.
  • externalDocs: dict ๐Ÿ”ฌ ๐Ÿ”ข ๐Ÿงพ โฎ๏ธ:
    • description: str โฎ๏ธ ๐Ÿ“ ๐Ÿ“› ๐Ÿ”ข ๐Ÿฉบ.
    • url (โœ”): str โฎ๏ธ ๐Ÿ“› ๐Ÿ”ข ๐Ÿงพ.

โœ ๐Ÿ—ƒ ๐Ÿ”–

โžก๏ธ ๐Ÿ”„ ๐Ÿ‘ˆ ๐Ÿ–ผ โฎ๏ธ ๐Ÿ”– users & items.

โœ ๐Ÿ—ƒ ๐Ÿ‘† ๐Ÿ”– & ๐Ÿšถโ€โ™€๏ธ โšซ๏ธ openapi_tags ๐Ÿ”ข:

from fastapi import FastAPI

tags_metadata = [
    {
        "name": "users",
        "description": "Operations with users. The **login** logic is also here.",
    },
    {
        "name": "items",
        "description": "Manage items. So _fancy_ they have their own docs.",
        "externalDocs": {
            "description": "Items external docs",
            "url": "https://fastapi.tiangolo.com/",
        },
    },
]

app = FastAPI(openapi_tags=tags_metadata)


@app.get("/users/", tags=["users"])
async def get_users():
    return [{"name": "Harry"}, {"name": "Ron"}]


@app.get("/items/", tags=["items"])
async def get_items():
    return [{"name": "wand"}, {"name": "flying broom"}]

๐Ÿ‘€ ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ โœ ๐Ÿ”˜ ๐Ÿ“›, ๐Ÿ–ผ "๐Ÿ’ณ" ๐Ÿ”œ ๐ŸŽฆ ๐Ÿฆ (๐Ÿ’ณ) & "๐ŸŽ€" ๐Ÿ”œ ๐ŸŽฆ โ• (๐ŸŽ€).

Tip

๐Ÿ‘† ๐Ÿšซ โœ”๏ธ ๐Ÿšฎ ๐Ÿ—ƒ ๐ŸŒ ๐Ÿ”– ๐Ÿ‘ˆ ๐Ÿ‘† โš™๏ธ.

โš™๏ธ ๐Ÿ‘† ๐Ÿ”–

โš™๏ธ tags ๐Ÿ”ข โฎ๏ธ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ (& APIRouterโ“‚) ๐Ÿ› ๏ธ ๐Ÿ‘ซ ๐ŸŽ ๐Ÿ”–:

from fastapi import FastAPI

tags_metadata = [
    {
        "name": "users",
        "description": "Operations with users. The **login** logic is also here.",
    },
    {
        "name": "items",
        "description": "Manage items. So _fancy_ they have their own docs.",
        "externalDocs": {
            "description": "Items external docs",
            "url": "https://fastapi.tiangolo.com/",
        },
    },
]

app = FastAPI(openapi_tags=tags_metadata)


@app.get("/users/", tags=["users"])
async def get_users():
    return [{"name": "Harry"}, {"name": "Ron"}]


@app.get("/items/", tags=["items"])
async def get_items():
    return [{"name": "wand"}, {"name": "flying broom"}]

Info

โœ ๐ŸŒ… ๐Ÿ”ƒ ๐Ÿ”– โžก ๐Ÿ› ๏ธ ๐Ÿ“ณ.

โœ… ๐Ÿฉบ

๐Ÿ”œ, ๐Ÿšฅ ๐Ÿ‘† โœ… ๐Ÿฉบ, ๐Ÿ‘ซ ๐Ÿ”œ ๐ŸŽฆ ๐ŸŒ ๐ŸŒ– ๐Ÿ—ƒ:

โœ” ๐Ÿ”–

โœ” ๐Ÿ”  ๐Ÿ”– ๐Ÿ—ƒ ๐Ÿ“– ๐Ÿ”ฌ โœ” ๐ŸŽฆ ๐Ÿฉบ ๐ŸŽš.

๐Ÿ–ผ, โœ‹๏ธ users ๐Ÿ”œ ๐Ÿšถ โฎ๏ธ items ๐Ÿ”ค โœ”, โšซ๏ธ ๐ŸŽฆ โญ ๐Ÿ‘ซ, โ†ฉ๏ธ ๐Ÿ‘ฅ ๐Ÿšฎ ๐Ÿ‘ซ ๐Ÿ—ƒ ๐Ÿฅ‡ ๐Ÿ“– ๐Ÿ“‡.

๐Ÿ—„ ๐Ÿ“›

๐Ÿ”ข, ๐Ÿ—„ ๐Ÿ”— ๐Ÿฆ /openapi.json.

โœ‹๏ธ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”— โšซ๏ธ โฎ๏ธ ๐Ÿ”ข openapi_url.

๐Ÿ–ผ, โš’ โšซ๏ธ ๐Ÿฆ /api/v1/openapi.json:

from fastapi import FastAPI

app = FastAPI(openapi_url="/api/v1/openapi.json")


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

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ’š โŽ ๐Ÿ—„ ๐Ÿ”— ๐Ÿ• ๐Ÿ‘† ๐Ÿ’ช โš’ openapi_url=None, ๐Ÿ‘ˆ ๐Ÿ”œ โŽ ๐Ÿงพ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ”ข ๐Ÿ‘ˆ โš™๏ธ โšซ๏ธ.

๐Ÿฉบ ๐Ÿ“›

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”— 2๏ธโƒฃ ๐Ÿงพ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ”ข ๐Ÿ”Œ:

  • ๐Ÿฆ ๐ŸŽš: ๐Ÿฆ /docs.
    • ๐Ÿ‘† ๐Ÿ’ช โš’ ๐Ÿšฎ ๐Ÿ“› โฎ๏ธ ๐Ÿ”ข docs_url.
    • ๐Ÿ‘† ๐Ÿ’ช โŽ โšซ๏ธ โš’ docs_url=None.
  • ๐Ÿ“„: ๐Ÿฆ /redoc.
    • ๐Ÿ‘† ๐Ÿ’ช โš’ ๐Ÿšฎ ๐Ÿ“› โฎ๏ธ ๐Ÿ”ข redoc_url.
    • ๐Ÿ‘† ๐Ÿ’ช โŽ โšซ๏ธ โš’ redoc_url=None.

๐Ÿ–ผ, โš’ ๐Ÿฆ ๐ŸŽš ๐Ÿฆ /documentation & โŽ ๐Ÿ“„:

from fastapi import FastAPI

app = FastAPI(docs_url="/documentation", redoc_url=None)


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