Setup API definition and CORS for Azure Web Apps using Azure ARM template

Microsoft Azure App Service can not only be used to host web apps but they can also be used to host API services. Swagger is a framework for describing your API using a common language that everyone can understand. In order for the other softwares 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:

api definition from azure portal

In this blog post, we’ll learn how to achieve the same using Azure ARM template. First, let’s add a CORS setting which is usually set to *. However, this can easily be set to any specific endpoint starting with either http or https. To add CORS setting, we would need to add below code into siteconfig object inside web app properties:

"cors": {
            "allowedOrigins": [

Since we mentioned an array in the above code, we can mention many urls at same time.

Similarly, we can set API definition in the form of below code into siteconfig object:

"apiDefinition": {
             "url": "[concat('https://', reference(concat('Microsoft.Web/sites/', variables('webAppNameVar'))).defaultHostName, '/swagger/docs/v1')]"

In above post, we set apiDefinition by concatenating first https, then Micorosft.Web/Sites followed by webapp/apiapp url and finally /swagger/docs/v1 url. This makes our property dynamic enough.

So that our Azure ARM template looks like below:

A reference for all web properties can be found here:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s