服务器

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

以 Markdown 格式查看

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

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

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

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

命名您的服务器

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

openapi.yml
1servers:
2 - x-fern-server-name: Production
3 url: https://api.yourcompany.com/
4 - x-fern-server-name: Production_EU
5 url: https://api.eu.yourcompany.com/

单个 API 的多个基础 URL

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

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

generators.yml
1api:
2 default-environment: Production
3 default-url: api
4 environments:
5 Production:
6 api: api.yourcompany.com
7 ai: ai.yourcompany.com
8 specs:
9 - openapi: ./path/to/your/openapi
10 overrides: ./path/to/your/overrides # optional

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

openapi.yml
1paths:
2 /chat:
3 post:
4 x-fern-server-name: ai

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

generators.yml
1api:
2 default-environment: Production
3 default-url: api
4 environments:
5 Production:
6 api: api.yourcompany.com
7 ai: ai.yourcompany.com
8 Staging:
9 api: api.staging.yourcompany.com
10 ai: ai.staging.yourcompany.com
11 Dev:
12 api: api.dev.yourcompany.com
13 ai: ai.dev.yourcompany.com
要查看生产环境中的示例,请查看 Chariot generators.yml