Tyk Classic API versioning
Last updated: 9 minutes read.
API versioning is a crucial practice in API development and management that allows you to evolve your API over time while maintaining backward compatibility for existing clients. As your API grows and changes, versioning provides a structured way to introduce new features, modify existing functionality, or deprecate outdated elements without breaking integrations for users who rely on previous versions.
When working with Tyk Classic APIs, all versions of an API are configured from a single API definition. This means that they share many features with a subset available to be configured differently between versions.
API versioning is configured in the Tyk Classic API Definition. You can do this via the Tyk Dashboard API or in the API Designer.
If you’re using the newer Tyk OAS APIs, then check out the Tyk OAS page.
If you’re using Tyk Operator then check out the configuring API versioning in Tyk Operator section below.
Controlling access to Tyk Classic API versions
You can explicitly grant access to specific version(s) of an API by specifying only those version(s) in the key (also known as an authorization token, bearer token, access token, API token or token session object - see here).
Configuring API versioning in the Tyk Classic API Definition
The configuration for a new version of a Tyk Classic API is contained in the version_data
section within the API definition.
This has the following configuration:
not_versioned
: set tofalse
to treat this as a versioned APIdefault_version
: this must contain thename
of the version that shall be treated asdefault
(for access control and default fallback)versions
: a list of objects that describe the versions of the API; there must be at least one (default) version defined for any API (even non-versioned APIs)
To add an API version, you must add a new entry in the versions
list:
name
: an identifier for this version of the API, for exampledefault
orv1
expires
: an optional expiry date for the API after which Tyk will reject any access request; accepted format is2006-01-02 15:04
paths
: location for configuration of endpoint ignore, allow and block listsuse_extended_paths
: set totrue
to enable theextended_paths
configextended_paths
: location for configuration of additional endpoint-level middlewareglobal_*
: configuration of API-level middleware. The wildcard can be replaced by any of the API-level settings e.g.global_size_limit
override_target
: alternative upstream (target) URL that should be used for this version, overriding thetarget_url
configured in theproxy
section of the API definition; this can be used to redirect to a different hostname or domain if required
There is also some API-level configuration for versioning, which is located in the definition
section of the Tyk Classic API definition:
The definition
section has the following fields:
location
: used to configure where the versioning identifier should be provided:header
,url
,url-param
key
: the versioning identifier key used iflocation
is set toheader
orurl-param
strip_versioning_data
: set this totrue
to remove the versioning identifier prior to creating the upstream (target) URL)fallback_to_default
: set this totrue
to invoke the default version if an invalid version is requestedurl_versioning_pattern
: if you are usingstrip_versioning_data
andfallback_to_default
withlocation=url
with Tyk 5.5.0 or later you can configure this with a regex that matches the format that you use for the versioning identifier (versions.name
)
The following fields in definition
are either deprecated or otherwise not used for Tyk Classic API versioning and should be left with their default values:
enabled
: defaults tofalse
default
: defaults to an empty string""
name
: defaults to an empty string""
strip_path
: deprecated field; defaults tofalse
versions
: defaults to an empty array{}
When you first create an API, it will not be “versioned” (i.e. not_versioned
will be set to true
) and there will be a single Default
version created in the version_data
section of the API definition.
Here’s an example of the minimal configuration that would need to be added to the API definition for an API with two versions:
|
|
In this example, there are two versions of the API
- the version identifier is provided in a request header
x-api-version
- the versions are named
v1
andv2
- the only difference between
v1
andv2
is thatv2
will proxy the request to a different upstream via the configuredoverride_target
- the default version (
default_version
) isv1
- if the request header contains an invalid version named (e.g.
v3
), it will be directed to the default (fallback_to_default:true
)
Configuring API versioning in the API Designer
When you first create a Tyk Classic API, it will not be “versioned” (i.e. not_versioned
will be set to true
) and there will be a single Default
version created in the version_data
section of the API definition (for explanation of these fields, please see above). The statement above holds only when designing an API “from scratch” as when importing an OAS API the versioning functionality is enabled by default.
Creating a versioned API
You can use the API Designer in the Tyk Dashboard to add versions for your API by following these steps.
Step 1: enable versioning
In the API Designer, navigate to the Versions tab.
Deselect the Do not use versioning checkbox to enable versioning and display the options.
Step 2: configure the versioning identifier
Choose from the drop-down where the version identifier will be located and, if applicable, provide the key name (for query parameter or request header locations).
Note
The Tyk Classic API Designer does not have support to configure url_versioning_pattern
from this screen, however it is easy to add in the Raw Definition editor.
Step 3: add a new version
You will see the existing (Default
) version of your API in the Versions List. You can add a new version by providing a version name (which will be the value your clients will need to provide in the version location when calling the API).
You can optionally configure an Override target host that will replace the target path that was set in the base configuration for the version. Note that this is not compatible with Service Discovery or Load Balanced settings.
Select Add to create this new version for your API.
Step 4: set the default version
You can choose any of your API versions to act as the default.
Select Update to save the changes to your API.
Configuring an API version
As explained much of the Tyk Classic API definition is shared between the different versions, but some middleware can be configured differently.
From the Endpoint Designer tab, you can select the version that you wish to configure from the Edit Version dropdown.
Select Update to save the changes to your API.
Configuring API versioning in Tyk Operator
The process for configuring API versioning is similar to that defined in section Configuring API versioning in the Tyk Classic API Definition.
We can see in the example below that one version is configured for the API within spec.version_data
:
- the version name is
v1
- the default version (
default_version
) isv1
- the
definition
configuration block contains alocation
field set toheader
and has an accompanyingkey
field set tox-api-version
. Subsequently, the version identifier for the API will be retrieved from thex-api-version
header. The comments provide examples for how to configure the version identifier to be retrieved from URL or a named URL parameter - an allow list, black list and ignore authentication middleware have been configured for version
v1
- an alternative upstream URL (
override_target
) is configured forv1
to send requests tohttp://test.org
|
|