In the OpenAPI 3.0 Specification, the root OpenAPI Object has the servers
property which is an array of Server Objects. And the Path Item Object also allows an optional servers
property.
The description given in the Specification does not give a clear idea of how servers
can be helpful.
What is the significance of the servers
property? Do we have any example which explains the use cases of servers
both as a direct property of the root OpenAPI object and also as a property of a path item?
Multiple hosts are supported in OpenAPI 3.0. 2.0 supports only one host per API specification (or two if you count HTTP and HTTPS as different hosts). A possible way to target multiple hosts is to omit the host and schema from your specification and serve it from each host.
The OpenAPI Specification (OAS) defines a standard, language-agnostic interface to HTTP APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection.
servers replaces the host , basePath and schemes keywords used in OpenAPI 2.0. Each server has an url and an optional Markdown-formatted description .
Basic authentication. API key (as a header or query parameter) OAuth 2 common flows (implicit, password, application and access code)
servers
specifies one or more target servers for the API, in other words, the base URL for API calls. The endpoint paths (e.g. /users/{id}
) are defined relative to these servers. Some APIs have a single target server; others may offer several servers, e.g. sandbox vs. production, or regional servers for different geographical areas (example: AWS).
By default, all operations in an OpenAPI definition use the globally defined servers
, but servers
may also be overridden for specific paths and operations. This is useful for APIs where some operations use a different server than the rest of the operations. This way you can document all operations in a single API definition instead of splitting it into multiple definitions, one per server.
Example: Dropbox API
api.dropboxapi.com
domain.content.dropboxapi.com
. notify.dropboxapi.com
.www.dropbox.com
.The Dropbox API definition might look like this:
openapi: 3.0.0
info:
title: Dropbox API
version: 1.0.0
servers:
- url: 'https://api.dropboxapi.com/2'
paths:
# These endpoints are on api.dropboxapi.com (use global `servers`)
/file_requests/list:
...
/users/get_account:
...
/files/upload:
# File upload/download uses another target server
servers:
- url: 'https://content.dropboxapi.com/2'
...
/files/list_folder/longpoll:
# Longpolling uses another target server
servers:
- url: 'https://notify.dropboxapi.com/2'
...
Check out the API Host and Base Path guide for more details and examples.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With