Skip to Content
openapi-proxy

Environments and servers

How to serve the same API for production, a sandbox and staging, and for audiences that should see different servers.

An API usually runs in more than one place: production, a sandbox, staging. Its operations are the same everywhere. What differs is where requests go, and OpenAPI says that in one list at the top of the document, servers. Documentation tools show that list as a menu, and SDK generators use the first entry unless someone picks another.

So an environment needs no setting of its own here. It is a matter of what a variant says in servers.

Title and servers of a variant

By default a variant says what your document says: its title, its description and its servers. In a project with several documents, those come from the main one.

In the variant editor, Title and servers lets a variant state its own instead:

  • Servers. The list this audience sees, in the order tools use it. The first is the default.
  • Title and Description. For an audience that knows the API by another name.

The preview shows the result before anything is published. A field left empty keeps what the document says.

Three ways to handle environments

One variant, every server listed. Give the variant all the servers its readers may use. They choose in their tool. This is the least work, and it is what most documentation and SDK tools expect.

servers:
  - url: https://api.example.com
    description: Production
  - url: https://sandbox.example.com
    description: Sandbox

One variant per environment. When each environment has its own docs site or its own build, make a variant for each and put that environment's server first, or list only that one. Duplicate, in a variant's menu, copies its selection and settings into a new variant, so the two start out the same.

One project per environment. When staging has operations that production does not have yet, it is a different document, served from a different URL of yours. Connect it as a project of its own.

What readers should not see

A document that is generated from your code often lists servers that are not for everyone: http://localhost:8080, an internal hostname, a staging address. A variant for the public should state its own servers, and then only those are served. The source is not changed, and other variants of the project still see what it says.

From a script

overrides on a variant holds the same three things. With an API key:

curl -X PATCH https://proxy.example.com/api/variants/$VARIANT_ID \
  -H "Authorization: Bearer $OPENAPI_PROXY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"overrides": {"servers": [{"url": "https://staging.example.com", "description": "Staging"}]}}'

"overrides": null goes back to what the document says.

On this page