Bookmark this page

Managing API Versions

Objectives

After completing this section, you should be able to create and configure multiple versions of APIs.

Introducing API Versioning

API versioning is a way of releasing backward-incompatible changes to your API without breaking client code.

An example of a backward-incompatible change is removing a field from an API response. The client code that reads the removed field breaks if it tries to read a property that no longer exists.

You use API versioning when there is a need to change the client code before using a new incompatible version.

There are several ways to implement API versioning depending on where you place the version number, such as:

  • URL versioning specifies the version number as part of the URL path or in a URL query parameter.

  • Request header versioning specifies the version number in an HTTP header. This can be the accept header or a custom header such as x-api-version.

To implement URL API versioning in Red Hat 3scale API Management, create a backend per version of your application. Then, add them all to a single product, but prefix each with a unique path based on the version string.

3scale API Management also offers the Routing policy, which provides request header versioning.

URL Versioning

With URL versioning, requests are routed based on the version value taken from the request URL.

Figure 2.16: URL versioning request routing

URL versioning makes the version clear because the version is apparent from the URL. However, using URL versioning can break the one-to-one relationship between a resource and its REST URL. Namely, a resource that has no changes gets another URL for the new version.

Preparing the New Version

The design of the public-facing API must accept a version number in the URL path. Doing so ensures that client applications of the API supply the URL version during development.

For example, clients for the first API version use a URL such as https://example.com/v1/resource-name, where the v1 indicates the version of the API the client wants to consume.

To release a breaking change, you must change the URL to use a new version.

Following the previous example, incrementing from v1 to v2 changes the URL to https://example.com/v2/resource-name, representing the new version.

The different versions in the request path route the request to the correct endpoint in the target API.

Using Products and Backends for URL Versioning

For URL versioning, create a backend for each of the versions and set the private base URL of the backend to point to the desired API version.

Having each backend point to a different path creates a logical separation for each version.

BackendPrivate Base URL
backend_v1 http://microservice-internal-url/api/v1
backend_v2 http://microservice-internal-url/api/v2

For the preceding example, you assign both backends to the product by using a public path that represents the version.

For example, the public path for backend version v2 is /v2.

When handling the request, 3scale API Management rewrites the path and eliminates the public path specified in the mapping between the product and the backend.

Table 2.1. 3scale API Management Request Translation.

Public URLPrivate URL
https://example.com/v1/resource-name http://microservice-internal-url/api/v1/resource-name
https://example.com/v2/resource-name http://microservice-internal-url/api/v2/resource-name

Request Header Versioning

Request header versioning lets clients consume different versions of an API preserving the same URL. With this approach, if a REST resource does not change, then there is no need to release a new version for it. The downside is that it requires clients to know which versions of a resource are available before consuming the API.

Using the Routing Policy for Request Header Versioning

To implement request header versioning, use the Routing policy.

This policy routes requests based on the URL, request headers, or JWT claims.

The Routing policy must be placed before the APIcast policy in the policy chain.

Figure 2.17: Product policies

You can configure the Routing policy to route requests by using a custom header, such as x-api-version.

For this, you can configure the Routing policy in the Admin Portal with the following values.

Table 2.2. Routing policy for request header versioning

FieldValue
url http://microservice-internal-url/api/v2
combine_op and
match header
valuev2
value_type Evaluate 'value' as plain text.
op ==
header_name x-api-version

With this configuration, the Routing policy routes requests containing the header x-api-version: v2 to http://microservice-interal-url/api/v2.

After updating the policy configuration, you must click Update Policy Chain in the Policies section. Then, promote the configuration to staging to verify that it routes to the correct version.

You can use the curl utility to test the request header versioning by adding the version header to the request.

[student@workstation ~]$ curl -v -s --header "x-api-version: v2" \
  "https://example.com/resource?user_key=USER_KEY"
...output omitted...
> GET /resource?user_key=b2c9a5d6204ff848052dd4ba6944fbc7 HTTP/1.1
> Host: example.com
> User-Agent: curl/7.61.1
> Accept: /
> x-api-version: v2
>
...output omitted...
< HTTP/1.1 200 OK
...output omitted...

You can also use the 3scale Toolbox CLI to create the policy chain by executing the policies command with the import subcommand and an argument with a file containing the policy list.

[student@workstation ~]$ 3scale policies import \
  TENANT PRODUCT -f policy-chain.yaml

Where policy-chain.yaml contains a policy list with the Routing policy followed by the default 3scale APIcast policy.

- name: routing
  version: builtin
  configuration:
    rules:
    - condition:
        combine_op: and
        operations:
        - value_type: plain
          match: header
          value: v2
          op: matches
          header_name: x-api-version
      url: http://example.com/api/v2
  enabled: true
- name: apicast
  version: builtin
  configuration: {}
  enabled: true

This command creates a new configuration for the product and promotes that configuration to the staging environment.

 

References

For more information, refer to the URL versioning section in the Red Hat 3scale API Management Administering the API Gateway at https://access.redhat.com/documentation/en-us/red_hat_3scale_api_management/2.11/html/administering_the_api_gateway/versioning-3scale#url_versioning

Revision: do240-2.11-40390f6