ZE7EN
ZE7EN

Reputation: 81

Default version for API Management

I have enabled versioning for Azure API Management (APIM). The versioning is based on querystring parameters (e.g. ?api-version=1.0). The intent for using a querystring is so that if the user doesn't provide the parameter, it'll default to the latest version. So:

/api/operation?api-version=1.0 <- Goes to 1.0
/api/operation?api-version=1.1 <- Goes to 1.1
/api/operation                 <- Goes to 1.1 (assuming that is latest)

However, it appears as though APIM uses that parameter as an indexer and if it's not provided, APIM doesn't know which version to use. Is there a way to tell APIM that, if the parameter is missing, automatically go to a specific version?

Upvotes: 4

Views: 2869

Answers (3)

Rich Lewis
Rich Lewis

Reputation: 151

I have found that there may be a workaround for this. If you create an API initially without versioning, and then add versioning to it, you get the new version (say, v2) and, alongside that, Azure essentially copies your original API as a 'version' that it names original. This 'original' API version doesn't require the query parameter (or in my case, header) to activate. So essentially, if you pass no header, traffic gets sent to this 'original' API, but if you do pass a header, then it will go to the appropriate version.

As for your requirement for the header-less (or parameter-less) version to always go to the latest version, I think that would have to be done manually, by updating the destination of the 'original' version appropriately.

This could definitely be improved, but maybe there's a fudge if you really need it!

Upvotes: 0

Vitaliy Kurokhtin
Vitaliy Kurokhtin

Reputation: 7810

Not possible in Azure APIM. API version is required to be passed for every request.

Upvotes: 3

Joey Cai
Joey Cai

Reputation: 20067

Once you opt into API versioning, all API routes are explicitly versioned. This means a client cannot request a resource without explicitly providing an API version.

In order to make your scenario work, you need to allow match a default API version when a client doesn't specify anything. You could set up like below:

service.AddApiVersioning( options => options.AssumeDefaultVersionWhenUnspecified = true );

The AssumeDefaultVersionWhenUnspecified option enables support for clients to make requests with implicit API versioning. This option is disabled by default, which means that all clients must send requests with an explicit API version. Services will respond to client requests that do not specify an API version with either HTTP status code 400 (Bad Request) or HTTP status code 404 (Not Found), depending whether the requested route exists.

Also, you could use DefaultApiVersion option which defines what the default ApiVersion will be for a service without explicit API version information. The default configured value is 1.0.

services.AddApiVersioning(
    o => o.DefaultApiVersion =
        new ApiVersion( new DateTime( 2016, 7, 1 ) );

For more details, you could refer to this article.

Upvotes: 1

Related Questions