Best Practice

Leveraging API Apps in Azure

Introduction

Azure offers developers the ability to build and host their APIs in the cloud. As Mobile phones take over PC sales, it has become more important for developers to build and deploy REST API services which both Mobile Phones and Tablets can connect to. Azure App Services provides the Paas environment to accomplish this.

Why use APIS

If you need a scalable, secure platform to build your API’s then Azure API Apps will meet your requirements. You can develop your API’s using multiple languages such as Python, C# or even PHP. You can select from a selection of web frameworks such as Node JS, ASP.net Web API and more. If you develop using Visual Studio you get integration through project templates and deployment wizards. Azure API Apps can also be used for integration scenarios to leverage workflows using Azure Logic Apps.

Shared Core features

Azure API Apps are hosted using Azure App Services offering. This means they share common features that are offered for Web Apps. These include:

  • Auto scaling. The ability to Scale out instances according to changes in CPU and other metrics.
  • Deployment slots. Minimise production application downtime by deploying to staging slots.
  • Authentication. Harness Azure AD as an Identity Provider to federate user authentication using popular SSO protocols such as OAuth.
  • Host your API Apps in an App Hosting plan (Free, Shared, Basic, Standard, Premium) to suit your workload.

Documenting your API using Swagger

Swagger is an API documentation framework. With swagger API developers can build documentation using metadata. If you are building an Azure Api App in Visual Studio, then the API App project template will add the Swagger Nuget references to the project. Once you deploy your API app to Azure you can access the Swagger UI using the URL: https://websitedomainname/swagger.

Continuous Integration & deployment

Azure API Apps also supports continuous deployment from your source control system. You can then use Azure App Service deployment slots to deploy your changes to a staging area. This allows developers to pre-qualify changes before they are promoted to production.

Authentication

If you wish to federate your authentication, then API Apps can be configured to use the following as Identity Providers:

  • Azure AD
  • Google
  • Facebook
  • Twitter
  • Microsoft

By default, your API app will be configured with no authentication method. Azure Active Directory allows you to use OAuth to protect your API.

Building an API App with Visual Studio

Visual Studio 2017 provides an API project template. From Visual Studio File Menu select New Project

  1. Select “ASP.NET Web Application (.NET Framework)” and click OK

Leveraging API Apps in Azure 

  1. Select “Azure API App” and click on OK. This will create a standard Web Api project. It will add Swagger nugget references and also decorate the default ValueController with Swagger Attribute for documentation metadata.

Leveraging API Apps in Azure 2 

  1. Right click on the Project and select Publish. Click on Create New Profile:

Leveraging API Apps in Azure 3 

  1. Pick a publish target. Select Microsoft Azure App Service. Select Create New. Click OK

Leveraging API Apps in Azure 4 

Leveraging API Apps in Azure 5 

  1. Fill in the form:
  • App Name: Provide a unique name
  • Subscription: Select an Azure Subscription
  • Resource Group: Select a Resource Group to host your App
  • App Service Plan: Select an App Service Plan
  • Click on Create to create API App. Azure will now provision the application.

Checking the Swagger endpoint

Once your application has been deployed you can visit it at the URL: https://{appname}.azurewebsites.net ({appname} is what you named your app in the deployment wizard)

As mentioned earlier Visual Studio adds the necessary code and references to enable Swagger. But to be sure check if the option to enable the Swagger API UI is enabled. You will find this in the file SwaggerConfig.cs This is automatically created as part of the project template. Make sure the line:.EnableSwaggerUi(c => Is not commented.

If you wish to interrogate your Swagger documentation you can go to the url: https://{appname}.azurewebsites.net/swagger/

Leveraging API Apps in Azure 6 

Leveraging API Apps in Azure 7 

Integrating with Logic App Workflows

You can start integrating your API App with a Logic App. For example, you could have a scenario where you have a workflow which requires data from your API.

Before you can do this you have to configure the following via the Azure Portal:

  1. Specify a API swagger metadata endpoint in your API App. For my sample API app I used the URL: https://romapiapp.azurewebsites.net/swagger/docs/v1
  2. Specify CORS. Under CORS section in API in the API App enter * as “Allowed Origins” access policy.

See the following screenshots:

Leveraging API Apps in Azure 8 

Leveraging API Apps in Azure 9 

Once you have done these steps you can start integrating your Logic App with your API!

Conclusion

Azure API App Services provides an easy and configurable solution for building and deploying API Apps into the Cloud. It provides developers flexibility in terms of programming languages and Web frameworks. You can also use the core features of App Services such as Auto scaling, deployment slots and continuous integration and deployment. For security you can configure Azure AD, or federate with a Social Media Identity provider. if you are building workflows using Azure Logic Apps you can now use Swagger endpoint to integrate your Api App with your workflow.