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"}]