How to resolve server URLs containing variables in OpenAPI 3.x definitions?
Tags: openapi, javascript, api, opensource
Published: 2023-04-22 16:33:50

The [OpenAPI specification](https://spec.openapis.org/oas/v3.1.0) is widely used to define RESTful APIs. One of the critical components of an OpenAPI specification is the [server](https://spec.openapis.org/oas/v3.1.0#server-object) section, which provides a list of URLs where the API can be accessed. However, working with the server section can be complicated, especially when the server URLs contain [variables](https://spec.openapis.org/oas/v3.1.0#serverVariableObject) and other dynamic components. That's where **[openapi-url-resolver](https://www.npmjs.com/package/openapi-url-resolver)** comes in.

Below is a server object example,

```javascript
{
  "servers": [
    {
      "url": "https://{username}.gigantic-server.com:{port}/{basePath}",
      "description": "The production API server",
      "variables": {
        "username": {
          "default": "demo",
          "description": "this value is assigned by the service provider, in this example `gigantic-server.com`"
        },
        "port": {
          "enum": [
            "8443",
            "443"
          ],
          "default": "8443"
        },
        "basePath": {
          "default": "v2"
        }
      }
    }
  ]
}
```


## Resolve server URLs or hosts from an OpenAPI 3.x definitions

**openapi-url-resolver** is a lightweight NPM package that simplifies the process of resolving server URLs from OpenAPI specifications. With `openapi-url-resolver`, you can easily extract server information and remove protocols from resolved URLs, making it easier to work with APIs that conform to the OpenAPI specification.

[![npm openapi-url-resolver](https://img.shields.io/npm/v/openapi-url-resolver?label=openapi-url-resolver&logo=npm)](https://www.npmjs.com/package/openapi-url-resolver)

Here are some of the key features of openapi-url-resolver:

- 📦 **Lightweight** module with only 965 bytes in size
- 🚀 **Zero dependencies**, making it easy to install and use in your projects
- 🎯 Efficient and simple way to resolve URLs from OpenAPI specifications

You can install `openapi-url-resolver` via NPM:

```bash
npm install openapi-url-resolver
```

Using [openapi-url-resolver](https://github.com/vinitshahdeo/openapi-url-resolver) is simple. All you need to do is pass an OpenAPI 3.x specification object to the `resolve()` function, and it will return an array of resolved server URLs. You can also pass a second parameter to the `resolve()` function to get the server URLs with protocols.

Here's an example of how to use openapi-url-resolver:

{% embed https://gist.github.com/vinitshahdeo/ae76fae2350dfbd543cca66af42f1022 %}

It's a great tool for developers who need to extract server information from OpenAPI specifications. **It's lightweight, easy to use, and doesn't have any dependencies, making it an excellent addition to any project that uses OpenAPI**.

If you're interested in learning more about **openapi-url-resolver**, check out the [GitHub repository](https://github.com/vinitshahdeo/openapi-url-resolver ) and give it a try in your projects!

Thank you.