跳到导航

服务器

定义服务器 URL 和环境以帮助用户连接到您的 API。
以 Markdown 格式查看

OpenAPI 允许您在 servers 键下指定一个或多个基础 URL。

openapi.yml
servers:
- url: https://api.yourcompany.com/
- url: https://api.eu.yourcompany.com/

指定服务器对 SDK 和文档都很有价值:

  • 对于 SDK,您的用户无需在客户端实例化时手动指定 baseURL
  • 对于文档,您的 API 调试工具将自动连接到正确的服务器

命名您的服务器

如果您有多个服务器,我们建议指定 x-fern-server-name 来为服务器命名。

openapi.yml
servers:
- x-fern-server-name: Production
url: https://api.yourcompany.com/
- x-fern-server-name: Production_EU
url: https://api.eu.yourcompany.com/

单个 API 的多个基础 URL

如果您采用微服务架构,可能会有不同的端点托管在不同的 URL 上。例如,您的 AI 端点可能托管在 ai.yourcompany.com,而其余端点可能托管在 api.yourcompany.com

要指定这种情况,您需要在 generators.yml 和 OpenAPI 规范中都添加配置。下面的代码片段显示了如何在 generators.yml 中配置具有多个 URL 的环境。

generators.yml
api:
default-environment: Production
default-url: api
environments:
Production:
api: api.yourcompany.com
ai: ai.yourcompany.com
specs:
- openapi: ./path/to/your/openapi
overrides: ./path/to/your/overrides # optional

一旦您在 generators.yml 中指定了环境,就可以使用 x-fern-server-name 扩展来指定操作所属的服务器。

openapi.yml
paths:
/chat:
post:
x-fern-server-name: ai

如果您有多个环境,如开发或测试环境,也可以在 generators.yml 中建模。

generators.yml
api:
default-environment: Production
default-url: api
environments:
Production:
api: api.yourcompany.com
ai: ai.yourcompany.com
Staging:
api: api.staging.yourcompany.com
ai: ai.staging.yourcompany.com
Dev:
api: api.dev.yourcompany.com
ai: ai.dev.yourcompany.com
要查看生产环境中的示例,请查看 Chariot generators.yml