Setting the API Definition URL and CORS value with ARM

1 minute read

When working with Azure services, you will combine services together. For one of mine integration cases I had to create a LogicApp where in I needed to use one of my own API’s in order to perform some actions.

In order for the Logic apps designer to parse your Swagger and notice your API as connector, it's necessary that you enable CORS and set the APIDefinition properties of the web application you want to use. Using the portal this is very easy to set.

  • Open the settings blade of your Web App
  • Under the API section set the 'API Definition' to the URL of your swagger.json file (this is usually https://{name}.azurewebsites.net/swagger/docs/v1).
  • Add a CORS policy for '*' to allow for requests from the Logic apps Designer.

These settings can also be set when you use a Azure Resource Management template to deploy your resources.

It’s very simple as well you need to add the “apiDefinition” “url” property and the “cors” property to the web config section.

{
  "name": "web",
  "type": "config",
  "apiVersion": "[variables('defaultApiVersion')]",
  "dependsOn": [
    "[concat('Microsoft.Web/sites/', parameters('api_Name'))]"
  ],
  "tags": {
    "displayName": "WebSettings"
  },
  "properties": {
    "cors": {
      "allowedOrigins": [
        "*"
      ]
    },
    "apiDefinition": {
      "url": "[concat('https://', reference(concat('Microsoft.Web/sites/', parameters('api_Name'))).defaultHostName, '/swagger/docs/v1')]"
    }
  }
}

In de code snippet above check the value of the “url” property of the “apiDefinition” it is a concatenation of parameters and created resources within my resource template making the URL property dynamic.