DEV Community

Vinit Shahdeo
Vinit Shahdeo

Posted on

How to resolve server URLs containing variables in OpenAPI 3.x definitions?

The OpenAPI specification is widely used to define RESTful APIs. One of the critical components of an OpenAPI specification is the server 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 and other dynamic components. That's where openapi-url-resolver comes in.

Below is a server object example,

  "servers": [
      "url": "https://{username}{port}/{basePath}",
      "description": "The production API server",
      "variables": {
        "username": {
          "default": "demo",
          "description": "this value is assigned by the service provider, in this example ``"
        "port": {
          "enum": [
          "default": "8443"
        "basePath": {
          "default": "v2"
Enter fullscreen mode Exit fullscreen mode

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

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:

npm install openapi-url-resolver
Enter fullscreen mode Exit fullscreen mode

Using 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:

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 and give it a try in your projects!

Thank you.

Top comments (0)