Date:

Share:

How to create an API Gateway using Azure API Management

Related Articles

If you’re building an application that exposes multiple services, you might not want to expose them on different hosts. Consumers will struggle to configure their app with all the different hostnames, and you’ll be forced to keep the same URLs even if you need to move to other platforms or, for example, want to turn a REST endpoint into an Azure function. .

In this case, you need to mask the real endpoints under a Appearance: Maybe… aPI gate? 🙂

In this article we will learn how to configure Azure API management (from now on: APIM) service to create an API gateway and “hide” our real services.

Demo: Publish .NET API services and locate the OpenAPI definition

For the purpose of this article, we will work with 2 API services: BooksService and video service.

Both are .NET 6 APIs, deployed in Azure using GitHub Actions (using the steps I described in a previous article).

Both services expose their Swagger pages and a bunch of endpoints that we hide behind the Azure APIM.


How to create an Azure API Management (APIM) service from the Azure portal

Now, we want to hide their real endpoints. Then the clients will only know about the existence of the API GatewayAnd not from the two separate API services:


An API gateway hides native endpoints from clients

Time to create our APIM resource.👷‍♂️

turned to the Azure portal, and create a new API Management instance. I suggest reading the brief overview of the functionality provided by Azure API Management services as shown in the screenshot below.


Description of API management in Azure Portal

The wizard will ask you for some information, such as the name of the resource, the region and an email used to send communications (honestly, I still don’t understand why they ask for your email).

Fill in all the fields, choose your preferred pricing tier (mine is Developer: it has no SLA and is quite cheap), then proceed with creating the service.

After a few minutes (it took 50 minutes – fifty!💢 – to recruit my show)You will have the instance ready to use.


API management dashboard

We are now ready to add our APIs and expose them to our customers.

How to add Azure API Management APIs using Swagger Setup (OpenAPI)

As we saw in a previous article, Swagger creates a JSON file that describes the operations available in your APIs, as well as the object structures received as input and returned as output.

Let me use the Books API as an example: once this API project is deployed to the cloud (it’s not mandatory to use Azure: it will work the same using other cloud providers), you’ll see the Swagger UI and the related JSON definition.


Swagger's UI for BooksAPI

We have 3 endpoints, /, /echoand /books; These endpoints are described in swagger.json File linked on Swagger page; Put this link aside: we will use it soon.

Finally, we can add the Books APIs to our Azure Management API service! Go to the resource in Azure, locate the APIs A menu item in the left panel, f Create a new API definition using OpenAPI (which is the standard Swagger uses to create its UI).


API import from the OpenAPI specification

You will see a form that allows you to create new resources from OpenAPI specifications.

paste here the link to swagger.json file you located earlier, fill in the required fields and, if desired, add a prefix to identify these endpoints: I choose my books.


Wizard to import APIs from OpenAPI

You will then see your APIs appear in the panel shown below. It consists of different parts:

  • The list of exposed services. In the screenshot below, BooksAPI, Echo API and VideosAPI;
  • The list of exposed endpoints for each service: Here, BooksAPI exposes the endpoints in /, /echoand /books;
  • A list of policies applied to the incoming requests before hitting the actual endpoint;
  • The actual endpoint used when calling the frontend exposed by APIM;
  • A list of policies applied to the outgoing requests after the origin has processed the requests.


API privacy panel

For now, we’ll ignore inbound and outbound processing, as they will be the subject of a future article.

Consume exposed APIs in API Gateway

We are ready to go! Return to the Azure API Management service dashboard and Locate the API Gateway URL under Custom Domains > Gateway URL.


Where to find the gateway URL

This will be the root address our clients will use.

We can then access the Books API and the Videos API at both the source and the gateway (we’re only doing this to demonstrate that things work; clients will only use the APIs exposed by the API gateway).

The Videos API maintains the exact same structure, mapping the endpoints as defined in Origin.


Videos API on Origin and API Gateway

On the contrary, to access the books’ APIs we must access the /mybooks path (because we defined it a few steps ago when we imported BooksAPI from the OpenAPI definition: this API URL extension field), as shown below:


Books API on Origin and API Gateway

Additional readings

As usual, a bunch of interesting reads 📚

In this article we have only scratched the surface of Azure API Management. There’s a lot – and you can read about it on the Microsoft Docs site:

🔗 What is Azure API Management? | Microsoft docs

To integrate Azure APIM, we used two simple dotNET 6 web interfaces deployed in Azure. If you want to know how to configure GitHub Actions to build and deploy dotNET APIs, I recently published an article on this topic.

🔗 How to deploy .NET APIs to Azure using GitHub Actions | Code4IT

Finally, since we were talking about Swagger, here is an article where I analyzed how Swagger can be integrated into dotNET Core applications:

🔗 Understanding Swagger Integration in .NET Core | Code4IT

This article first appeared on Code4IT 🐧

finishing

This could be just the beginning of a long journey; APIM allows you to customize your API gateway by defining API access by user role, creating API documentation using custom templates and themes, and many different things.

We will return to this topic soon.

Happy coding!

🐧

.

Source

Popular Articles