API Server and Base Path
All API endpoints are relative to the base URL. For example, assuming the base URL of https://api.example.com/v1
, the /users
endpoint refers to https://api.example.com/v1/users
.
In OpenAPI 3.0, you use the servers
array to specify one or more base URLs for your API. servers
replaces the host
, basePath
and schemes
keywords used in OpenAPI 2.0. Each server has an url
and an optional Markdown-formatted description
.
You can also have multiple servers, for example, production and sandbox:
Server URL Format
Server URL format follows RFC 3986 and usually looks like this:
The host can be a name or IP address (IPv4 or IPv6). WebSocket schemes ws:// and wss:// from OpenAPI 2.0 are also supported in OpenAPI 3.0. Examples of valid server URLs:
If the server URL is relative, it is resolved against the server where the given OpenAPI definition file is hosted (more on that below). Note: Server URL must not include query string parameters. For example, this is invalid:
If the servers
array is not provided or is empty, the server URL defaults to /
:
Server Templating
Any part of the server URL – scheme, host name or its parts, port, subpath – can be parameterized using variables. Variables are indicated by {curly braces} in the server url, like so:
Unlike path parameters, server variables do not use a schema
. Instead, they are assumed to be strings. Variables can have arbitrary values, or may be restricted to an enum
. In any case, a default
value is required, which will be used if the client does not supply a value. Variable description
is optional, but useful to have and supports Markdown (CommonMark) for rich text formatting. Common use cases for server templating:
- Specifying multiple protocols (such as HTTP vs HTTPS).
- SaaS (hosted) applications where each customer has their own subdomain.
- Regional servers in different geographical regions (example: Amazon Web Services).
- Single API definition for SaaS and on-premise APIs.
Examples
HTTPS and HTTP
Or using templating:
Note: These two examples are semantically different. The second example explicitly sets the HTTPS server as default
, whereas the first example does not have a default server.
Production, Development and Staging
SaaS and On-Premise
Regional Endpoints for Different Geographical Areas
Overriding Servers
The global servers
array can be overridden on the path level or operation level. This is handy if some endpoints use a different server or base path than the rest of the API. Common examples are:
- Different base URL for file upload and download operations,
- Deprecated but still functional endpoints.
Relative URLs
The URLs in the servers
array can be relative, such as /v2
. In this case, the URL is resolved against the server that hosts the given OpenAPI definition. This is useful in on-premises installations hosted on your customer’s own servers. For example, if the definition hosted at http://localhost:3001/openapi.yaml
specifies url: /v2
, the url
is resolved to http://localhost:3001/v2
. Relative URL resolution rules follow RFC 3986. Moreover, almost all other URLs in an API definition, including OAuth 2 flow endpoints, termsOfService
, external documentation URL and others, can be specified relative to the server URL.
Note that if using multiple servers, the resources specified by relative URLs are expected to exist on all servers.
References
Did not find what you were looking for? Ask the community Found a mistake? Let us know