Before an API Release: Diagnose OpenAPI Server URLs That Resolve to the Wrong Host

Diagnose OpenAPI requests sent to the wrong host by comparing the specification location, effective server URL, and actual client request.

Published
Reading time
7 min read
Before an API Release: Diagnose OpenAPI Server URLs That Resolve to the Wrong Host
On this page

A documentation page can load successfully while its “Try it” requests go to the wrong host. A common cause is a relative OpenAPI server URL: its base is the location of the OpenAPI document, not necessarily the page displaying it. This guide shows how to trace the base URL, reproduce the resolution, and compare it with a client’s actual request before release.

Key Takeaways

  • In an OpenAPI 3 document, a relative server URL is resolved against the OpenAPI document’s location. A documentation page and its specification may be hosted on different origins.
  • A server URL beginning with / uses the document’s origin, not the directory containing the specification.
  • Check operation-level and path-level server overrides as well as the top-level servers entry.
  • Compare the resolved server and operation path with a request captured from the actual client. A calculation alone cannot confirm how a particular tool behaves.

This article could not consult the current OpenAPI Specification or RFC 3986 in this environment, and its example has not been executed here. Confirm the behavior against those documents and the versions of the documentation and client tools used for your release.

How OpenAPI relative server URL resolution works

Consider this OpenAPI 3 fragment:

openapi: 3.0.0
info:
  title: Example API
  version: 1.0.0
servers:
  - url: /v1
paths:
  /pets:
    get:
      responses:
        '200':
          description: Success

The server value /v1 is a relative reference. Its leading slash makes it relative to the origin of the specification document. For the /pets operation, the resulting request is expected to use the following server and path in each deployment:

Specification document locationResolved serverRequest URL
http://localhost:8080/openapi/openapi.yamlhttp://localhost:8080/v1http://localhost:8080/v1/pets
https://staging-docs.example.com/openapi/openapi.yamlhttps://staging-docs.example.com/v1https://staging-docs.example.com/v1/pets
https://docs.example.com/openapi/openapi.yamlhttps://docs.example.com/v1https://docs.example.com/v1/pets

These examples use fictional hosts. If the production API actually lives at https://api.example.com, the production result above is the wrong host unless the documentation host deliberately proxies /v1 to that API.

The exact relative reference matters. With a specification at https://docs.example.com/openapi/openapi.yaml, ./v1 resolves to https://docs.example.com/openapi/v1, while ../v1 resolves to https://docs.example.com/v1. Changing a leading slash or moving the specification can therefore change the destination.

Advertisement

Find the actual base and effective server

Before changing the specification, collect these values for one affected operation:

  1. Documentation page URL: the address in the browser. This identifies the environment, but it is not necessarily the server URL’s base.
  2. Specification document URL: inspect the browser’s network activity to find the OpenAPI file the page loads. Record its full URL, including its path. If it redirects, record both the configured and final URLs; check which one your client uses as the base.
  3. Effective server entry: inspect the operation, its path item, and the document’s top-level servers. An operation-level entry can override a path-level or top-level entry; a path-level entry can override the top-level entry. Also record the selected server and expanded variable values if the client offers a server selector.
  4. Operation path and actual request URL: use the client’s network or request log. Capture the destination without sharing authorization headers, tokens, or sensitive query parameters.

For example, a page at https://docs.example.com/reference/ might fetch its specification from https://cdn.example.com/openapi/openapi.yaml. In that case, resolving /v1 against the specification location points to https://cdn.example.com/v1, not automatically to either the documentation host or api.example.com. A client may be configured differently, so verify the request it actually generates.

Reproduce the comparison

The following diagnostic handles the simple case above: one already-selected server URL, no server variables, no query or fragment in the server URL, and an operation path without parameters or a query string. It resolves the server against the specification URL, appends the operation path, and compares the result with a captured request URL. The documentation page URL is printed for context; it is not used as the resolution base.

Save this as check-server.mjs. It uses the built-in URL API in Node.js and requires no additional package.

const [pageArg, specArg, serverArg, pathArg, actualArg] = process.argv.slice(2);

if (!pageArg || !specArg || !serverArg || !pathArg || !actualArg) {
  console.error('Usage: node check-server.mjs <page-url> <spec-url> <server-url> <operation-path> <actual-request-url>');
  process.exitCode = 1;
} else {
  try {
    const page = new URL(pageArg);
    const spec = new URL(specArg);
    const server = new URL(serverArg, spec);
    const actual = new URL(actualArg);

    if (!pathArg.startsWith('/') || /[?#{}]/.test(pathArg)) {
      throw new Error('Use an operation path starting with / and without parameters, query, or fragment.');
    }
    if (/[{}]/.test(serverArg) || server.search || server.hash || server.pathname.endsWith('/')) {
      throw new Error('Use an expanded server URL without a query, fragment, or trailing slash.');
    }
    if (!['http:', 'https:'].includes(server.protocol)) {
      throw new Error('This diagnostic only handles HTTP and HTTPS servers.');
    }

    const expected = new URL(server.href + pathArg);
    console.log('Documentation page:', page.href);
    console.log('Specification document:', spec.href);
    console.log('Server entry:', serverArg);
    console.log('Resolved server:', server.href);
    console.log('Calculated request:', expected.href);
    console.log('Captured request:', actual.href);
    console.log('Match:', expected.href === actual.href ? 'yes' : 'no');
    if (expected.href !== actual.href) process.exitCode = 1;
  } catch (error) {
    console.error('Could not compare URLs:', error.message);
    process.exitCode = 1;
  }
}

For a local fixture, run:

node check-server.mjs \
  'http://localhost:8080/reference/' \
  'http://localhost:8080/openapi/openapi.yaml' \
  '/v1' \
  '/pets' \
  'http://localhost:8080/v1/pets'

Replace the arguments with the values collected from your environment. The final argument should be the request URL captured from the client, not an assumed result. For this limited check, select an operation without path parameters or query parameters. A mismatch warrants investigation; a match only confirms this one combination of document, server, operation, and client.

Advertisement

Fix the deployment, then verify again

Choose the fix that matches the intended architecture:

  • The API has a distinct public host: publish an appropriate absolute server URL, such as https://api.example.com/v1, in the production specification. Ensure the listed host is the one intended for public use.
  • The documentation host intentionally proxies API requests: keep a relative server URL only if the corresponding route is configured and verified on every documentation origin that serves the specification.
  • Staging and production need different destinations: generate or configure the appropriate server entries for each published specification, then inspect the published artifacts rather than only the source template.
  • A CDN serves the specification: account for the CDN origin as the relative base, or use a suitable absolute server URL. Do not assume that hosting the documentation page on the API domain changes the specification’s base.

After deployment, reload the published documentation, confirm which specification and server entry it uses, and capture a fresh request. Test at least one representative operation for each server or override that users can select. Use an authorized test account and non-sensitive data where a request reaches a real API. A browser CORS error may prevent a response from being read, but it does not by itself establish that the resolved destination is correct.

Common Mistakes

  • Using the documentation page as the base without checking the specification fetch. The page and specification can be on different hosts or paths.
  • Checking only top-level servers. An operation or path override can send just one group of requests elsewhere.
  • Assuming a relative URL names the API host. /v1 inherits the specification’s origin; it does not discover a separate API domain.
  • Treating the diagnostic as a full client test. Server variables, parameters, queries, redirects, and client-specific configuration require additional checks.
  • Publishing environment secrets. Credentials, tokens, signed URLs, and private staging or internal hostnames can leak through a public specification or documentation bundle.

Best Practices

Keep a release check that records the published documentation URL, fetched specification URL, effective server, and captured request destination for each relevant environment. Review the public specification for private environment URLs and credentials before publishing. Test the deployed artifact through the same documentation interface or generated client your users will use, and repeat the check when moving the specification behind a new host, CDN, or redirect.

Conclusion

To diagnose a wrong-host request, start with the specification’s actual location and the operation’s effective server entry. Calculate the expected destination, then compare it with a request from the deployed client before deciding whether to change the specification or the hosting configuration.

What would your domain sell for?

A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.