コンテンツへスキップ

メタデータとドキュメントURL

FastAPIアプリケーションでは、いくつかのメタデータ設定をカスタマイズできます。

APIのメタデータ

OpenAPI仕様と自動APIドキュメントUIで使用される以下のフィールドを設定できます。

パラメータ タイプ 説明
title str APIのタイトル。
summary str APIの短い概要。 OpenAPI 3.1.0、FastAPI 0.99.0以降で利用可能。
description str APIの短い説明。Markdownを使用できます。
version string APIのバージョン。これは、OpenAPIのバージョンではなく、ご自身のアプリケーションのバージョンです。例: 2.5.0
terms_of_service str APIの利用規約へのURL。提供される場合、URLである必要があります。
contact dict 公開されたAPIの連絡先情報。いくつかのフィールドを含めることができます。
contact フィールド
パラメータタイプ説明
namestr連絡担当者/組織の識別名。
urlstr連絡先情報へのURL。URL形式である必要があります。
emailstr連絡担当者/組織のメールアドレス。メールアドレス形式である必要があります。
license_info dict 公開されたAPIのライセンス情報。いくつかのフィールドを含めることができます。
license_info フィールド
パラメータタイプ説明
namestr必須license_infoが設定されている場合)。APIに使用されるライセンス名。
identifierstrAPIのSPDXライセンス表現。identifierフィールドはurlフィールドと排他的です。 OpenAPI 3.1.0、FastAPI 0.99.0以降で利用可能。
urlstrAPIに使用されるライセンスへのURL。URL形式である必要があります。

以下のように設定できます。

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://apache.dokyumento.jp/licenses/LICENSE-2.0.html",
    },
)


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

ヒント

descriptionフィールドにはMarkdownを記述でき、出力でレンダリングされます。

この設定により、自動APIドキュメントは以下のようになります。

ライセンス識別子

OpenAPI 3.1.0およびFastAPI 0.99.0以降では、urlの代わりにidentifierlicense_infoを設定することもできます。

例えば

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",
        "identifier": "MIT",
    },
)


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

タグのメタデータ

パス操作をグループ化するために使用される異なるタグに対して、openapi_tagsパラメータで追加のメタデータを追加することもできます。

これは、各タグごとに1つの辞書を含むリストを受け取ります。

各辞書には以下を含めることができます。

  • name必須):_パス操作_とAPIRoutertagsパラメータで使用するタグ名と同じstr
  • description:タグの短い説明を含むstr。Markdownを含めることができ、ドキュメントUIに表示されます。
  • externalDocs:外部ドキュメントを記述するdictで、以下を含む。
    • description:外部ドキュメントの短い説明を含むstr
    • url必須):外部ドキュメントのURLを含むstr

タグのメタデータを作成する

usersitemsのタグを持つ例で試してみましょう。

タグのメタデータを作成し、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.dokyumento.jp/",
        },
    },
]

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

説明の中にMarkdownを使用できることに注意してください。たとえば、「login」は太字(login)で、「fancy」は斜体(fancy)で表示されます。

ヒント

使用するすべてのタグにメタデータを追加する必要はありません。

タグを使用する

_パス操作_(およびAPIRouter)で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.dokyumento.jp/",
        },
    },
]

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

情報

パス操作の構成でタグの詳細を読むことができます。

ドキュメントを確認する

これでドキュメントを確認すると、すべての追加メタデータが表示されます。

タグの順序

各タグメタデータ辞書の順序は、ドキュメントUIに表示される順序も定義します。

たとえば、usersはアルファベット順ではitemsの後になりますが、リストの最初の辞書としてメタデータを追加したため、それらの前に表示されます。

OpenAPI URL

デフォルトでは、OpenAPIスキーマは/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スキーマを完全に無効にしたい場合は、openapi_url=Noneを設定できます。これにより、それを使用するドキュメントユーザーインターフェースも無効になります。

ドキュメントURL

含まれている2つのドキュメントユーザーインターフェースを設定できます。

  • Swagger UI/docsで提供されます。
    • docs_urlパラメータでそのURLを設定できます。
    • docs_url=Noneを設定することで無効にできます。
  • ReDoc/redocで提供されます。
    • redoc_urlパラメータでそのURLを設定できます。
    • redoc_url=Noneを設定することで無効にできます。

たとえば、Swagger UIを/documentationで提供し、ReDocを無効にするには、次のようにします。

from fastapi import FastAPI

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


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