Search code examples
azureazure-api-managementazure-bicepapim

Azure API-M APIs and Operations with different base URL


In Azure API- Management, If we have a single API set and 2 API operations does the same thing but the newer version now point to a whole new endpoint.

For example below is the BICEP code:

// The APIs sets

resource regApi 'Microsoft.ApiManagement/service/apis@2022-08-01' = {
  name: 'register-api-transaction'
  parent: apimService
  properties: {
    displayName: 'Register APIs'
    apiRevision: '1'
    subscriptionRequired: true
    serviceUrl: registerServiceUrl
    path: 'internal/register/search'
    protocols: [
      'http'
      'https'
    ]
    authenticationSettings: {}
    subscriptionKeyParameterNames: {
      header: 'Ocp-Apim-Subscription-Key'
      query: 'subscription-key'
    }
    isCurrent: true
  }
}

The operations:

resource regGetApi 'Microsoft.ApiManagement/service/apis/operations@2021-08-01' = {
  name: 'name-register-get'
  parent: regApi 
  properties: {
    urlTemplate: '/nameRegister'
    method: 'GET'
    displayName: 'Name register'
    responses: []
  }
}

resource searchGetApi 'Microsoft.ApiManagement/service/apis/operations@2021-08-01' = {
  name: 'name-search-get'
  parent: regApi 
  properties: {
    urlTemplate: '/nameSearch'
    method: 'GET'
    displayName: 'Name search'
    responses: []
  }
}

Question:

What if i want to point one if the operation to a different serviceUrl ? both are pointing to the same parent.

  • I do not want to create a new API set
  • Can I use versioning in this situation? where the new version will have an altogether new base URL ?

Solution

  • Yes you can use versioning in this case as versioning is used to make API changes.

    You can publish multiple API versions simultaneously and use a path, query string, or header to distinguish between versions.

    To redirect it to different service URL, you can also use the set-backend-service policy as detailed in the given MS Doc.

    Once changes are made, you can also add Versioning scheme which is an identifier represents a new version for the existing API.

    I tried below code in my environment which meets your requirement, and the deployment was successful as below.

    param apiexists string = 'xxxx'
    resource apiservice 'Microsoft.ApiManagement/service@2023-03-01-preview' existing = {
      name: apiexists
    }
    resource regApi 'Microsoft.ApiManagement/service/apis@2022-08-01' = {
      name: 'register-api-transaction'
      parent: apiservice
      properties: {
        displayName: 'Register APIs'
        apiRevision: '2' 
        subscriptionRequired: true
        serviceUrl: '' // Modify this for the new version
        path: 'internal/register/search'
        protocols: [
          'http'
          'https'
        ]
        authenticationSettings: {}
        subscriptionKeyParameterNames: {
          header: 'Ocp-Apim-Subscription-Key'
          query: 'subscription-key'
        }
        isCurrent: true
      }
    }
    //Operations
    resource regGetApiV2 'Microsoft.ApiManagement/service/apis/operations@2021-08-01' = {
      name: 'name-register-get'
      parent: regApi 
      properties: {
        urlTemplate: '/nameRegister'
        method: 'GET'
        displayName: 'xxx'
        responses: []
      }
    }
    
    resource searchGetApiV2 'Microsoft.ApiManagement/service/apis/operations@2021-08-01' = {
      name: 'name-search-get'
      parent: regApi 
      properties: {
        urlTemplate: '/nameSearch'
        method: 'GET'
        displayName: 'xxx'
        responses: []
      }
    }
    

    In the above code, I have created a new version of the API by modifying the apiRevision: '2' and the serviceUrl to newServiceUrl.

    If you want to use the path-based versioning scheme, add the version number (Eg: "/v2")to the path of the API to indicate as version.

    Output:

    enter image description here

    enter image description here