Add multiple API versions

URI Path

The most common way to version an API is in the URI path and is often done with the prefix “v”. This method employs URI routing to direct requests to a specific version of the API.

Query parameters

In this method, the version number is included in the URI, but as a query parameter instead of in the path.

  1. http://apisix.org/hello?version=2

Custom request Header

You can also set the version number using custom headers in requests and responses. This leaves the URI of your resources unchanged.

  1. http://apisix.org/hello -H 'Version: 1'
  2. http://apisix.org/hello -H 'Version: 2'

The primary goal of versioning is to provide users of an API with the most functionality possible while causing minimal inconvenience. Keeping this goal in mind, let’s have a look in this tutorial at how to publish and manage multiple versions of your API with Apache APISIX.

In this tutorial, you learn how to:

  • Create a route and upstream for our sample API.
  • Add a new version to the existing API.
  • Use proxy-rewrite plugin to rewrite the path in a plugin configuration.

For the demo case, we will leverage the sample repository Evolve APIs on GitHub built on the Spring boot that demonstrates our API. You can see the complete source code there.

To execute and customize the example project per your need shown in this tutorial, here are the minimum requirements you need to install in your system:

  • - you need Docker installed locally to complete this tutorial. It is available for or macOS.

Also, complete the following steps to run the sample project with Docker.

Use to clone the repository:

  1. git clone 'https://github.com/nfrankel/evolve-apis'
  1. cd evolve-apis

Now we can start our application by running docker compose up command from the root folder of the project:

  1. docker compose up -d

You first need to your HTTP requests from the gateway to an Upstream (your API). With APISIX, you can create a route by sending an HTTP request to the gateway.

At this stage, we do not have yet any version and you can query the gateway as below:

  1. curl http://apisix.org/hello

output

  1. Hello world
  1. curl http://apisix.org/hello/Joe

output

  1. Hello Joe

In the previous step, we created a route that wrapped an upstream inside its configuration. Also, APISIX allows us to create an upstream with a dedicated ID to reuse it across several routes.

Let’s create the shared upstream by running below curl cmd:

  1. curl http://apisix:9080/apisix/admin/upstreams/1 -H 'X-API-KEY: xyz' -X PUT -d '
  2. {
  3. "name": "Old API",
  4. "type": "roundrobin",
  5. "oldapi:8081": 1
  6. }
  7. }'

Add a new version

In the scope of this tutorial, we will use URI path-based versioning because it’s the most widespread. We are going to add v1 version for our existing oldapi in this section.

We can now create the second versioned route that references the existing upstream and plugin config.

  1. curl http://apisix:9080/apisix/admin/routes/2 -H 'X-API-KEY: xyz' -X PUT -d '
  2. "name": "Versioned Route to Old API",
  3. "methods": ["GET"],
  4. "uris": ["/v1/hello", "/v1/hello/", "/v1/hello/*"],
  5. "upstream_id": 1,
  6. "plugin_config_id": 1
  7. }'

At this stage, we have configured two routes, one versioned and the other non-versioned:

output

  1. Hello world
  1. curl http://apisix.org/v1/hello

output

  1. Hello world

We have versioned our API, but our API consumers probably still use the legacy non-versioned API. We want them to migrate, but we cannot just delete the legacy route as our users are unaware of it. Fortunately, the 301 HTTP status code is our friend: we can let users know that the resource has moved from http://apisix.org/hello to http://apisix.org/v1/hello. It requires configuring the on the initial route:

Apache APISIX Multiple API versions with two routes

Now when we try to request the first non-versioned API endpoint, you will get an expected output:

  1. curl http://apisix.org/hello
  2. <html>
  3. <head><title>301 Moved Permanently</title></head>
  4. <body>
  5. <center><h1>301 Moved Permanently</h1></center>
  6. <hr><center>openresty</center>
  7. </html>

Either API consumers will transparently use the new endpoint because they will follow, or their integration breaks and they will notice the 301 status and the new API location to use.

Learn more about how to API consumers and protect your APIs.