Saturday, December 18, 2021

Bicep Templates for APIM Configuration - Part 2

 In previous article I have explained how to create an API with Bicep. But APIs in APIM goes towards versioning and then endpoints. 

If you do not intend to use versioning, you can create API in the last step of the previous article. Otherwise you need to define a version set first. 

resource adminApi_VersionSet_V1 'Microsoft.ApiManagement/service/apiVersionSets@2021-04-01-preview' = {
  name: 'adminApi_VersionSet_V1'
  parent: apim
  properties: {
    description: 'Admin API'
    displayName: 'Admin API'
    versioningScheme:'Segment'
  }
}

Here I am using the Segment as the versioning scheme. You can opt to use the Query String or a header as well instead.  

resource adminApi 'Microsoft.ApiManagement/service/apis@2021-04-01-preview' = {
  name: '${apimName}/AdminApi'
  properties: {
    apiVersion:'v1'
    apiVersionSetId: adminApi_VersionSet_V1.id
    displayName: 'Admin API'
    description: 'Includes admin operations.'
    serviceUrl: '${hostUrl_AdminApp}/config/v1/'
    path: 'admin'
    protocols: [
      'https'
    ]
  }
}


If you are using Azure Functions, you can map the routing to /config/v1 as shown above.  

You can customize the API policies at API level or at an endpoint level as shown below.

resource adminApiPolicy 'Microsoft.ApiManagement/service/apis/policies@2021-04-01-preview' = {
  name: '${adminApi.name}/policy'
  properties: {
    format: 'rawxml'
    value: '<policies><inbound><base /></inbound><backend><base /></backend><outbound><base /></outbound>
<on-error><base /></on-error></policies>'
  }
}

Now you can add endpoints as required. An example of adding an endpoint with a template parameter is shown below. 

resource Test_GET 'Microsoft.ApiManagement/service/apis/operations@2020-06-01-preview' = {
  name: '${apiName}/Test_GET'
  properties: {
    displayName: 'Test'
    method: 'GET'
    urlTemplate: '/test/{inputText}'
    description: 'Returns a response text.'
    templateParameters:[
      {
        name:'inputText'
        type:'string'
        required:true
      }
    ]
  }
}

No comments:

Post a Comment