You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在FastAPI的OpenAPI/Swagger UI中默认启用「Try it out」?

让FastAPI的Swagger UI默认启用「Try it out」功能

当然可以实现,FastAPI支持通过配置Swagger UI的初始化参数来默认开启「Try it out」,不用每次手动点击。

只需要在创建FastAPI实例时,传入swagger_ui_parameters参数,设置tryItOutEnabled为True即可:

from fastapi import FastAPI

# 初始化FastAPI时配置Swagger UI默认开启Try it out
app = FastAPI(swagger_ui_parameters={"tryItOutEnabled": True})

# 示例接口
@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str | None = None):
    return {"item_id": item_id, "q": q}

启动服务后访问/docs页面,所有接口会默认处于「Try it out」激活状态,直接就能输入参数并点击「Execute」测试,不用再手动开启。

这个配置是Swagger UI官方提供的功能,FastAPI通过swagger_ui_parameters参数允许传递各类Swagger UI的自定义配置,满足不同的界面需求。

内容的提问来源于stack exchange,提问作者PythonForEver

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 22:45:26