azure api management,

How To Debug Policies in Azure API Management. A Step-by-Step Guide.

Sven Malvik Sven Malvik Connect Jan 16, 2021 · 5 mins read
How To Debug Policies in Azure API Management. A Step-by-Step Guide.

In this post I want to briefly go through the Azure API Management extension for VSCode and how we can debug policies. It’s one of the questions I get a lot when holding workshops on APIM. How to effectively develop policies in Azure API Management. The post is a collection of screenshots that will explain in detail what you need to do step-by-step.

Install Extensions

Before we start we need 2 VSCode extensions. The C# extension is useful for getting IntelliSense support for the VCCode Azure API Management extension. You get both by following the links under Useful links.

VSCode extension for Azure API ManagementVSCode extension for Azure API Management

VSCode extension C#VSCode extension C#

This is my configuration of the VCCode Azure API Management extension. I disabled Show Save Promp as it irritates me getting asked every time I save Are you sure you want to do this?.

Configuring VSCode extension for Azure API ManagementConfiguring VSCode extension for Azure API Management

Before I do anything I tried the VCCode Azure API Management extension first and look at all the commands that it gives me. This post focuses on policies, so I won’t need much of the rest.

Commands overviewCommands overview

So, I know that the extension is up and running and I check out its capabilities.

Testing Azure API Management Extension

I have already provisioned an instance of Azure API Management with Developer-SKU. The documentation says that debugging policies is supported only in this SKU.

Select the Azure icon on the left side and choose the instance you want to play with. I will copy the master subscription key first because I know I will need it. This master-key is only for testing. Do never give it away since it allows access to all APIs.

Copy Master Subscription KeyCopy Master Subscription Key

Right-click on a GET endpoint of the one Echo API that is available and hit Test Operation. Then replace <Subscription Key> with the master- subscription key from the previous step. Then click on Send Request. It’s not very visible but you should find it :).

Testing APITesting API

The response is presented in a separate VSCode row.

API Response in VSCodeAPI Response in VSCode

Setting Up a new API

I start by importing the public available Conference API with the help of the Azure API Management extension and select Import from OpenAPI link.

Import API from URL into Azure API ManagementImport API from URL into Azure API Management

Follow the steps 1-4 for importing an API from a URL. Step 5 is just to show you how it should look at the end.

Import API from URL into Azure API Management 2Import API from URL into Azure API Management 2

Click on the ConferenceAPI and change the value for subscriptionRequired from true to false. This way it’s easier to test.

Setting Subscription Key requirement to falseSetting Subscription Key requirement to false

Debugging Policy

Now we can finally debug a policy. But first, let’s create/change the policy for the operation getTopics. I use this endpoint because it doesn’t require any parameters when sending a request.

You can just write your policy now. In this example, I started by writing set-va and the extension will show you all the policies that start with this text. Click enter, and vóila, the code for set-variable is automatically inserted. I do the same with set-header.

I set a variable iin the inbound-section that I will read in the outbound-section and set it as the value for the response header.

Changing API Operation Policy in Azure API ManagementChanging API Operation Policy in Azure API Management

I right-click on the operation and hit Test Operation and Send Request as I’ve done previously to make sure it works.

Testing API Operation PolicyTesting API Operation Policy

Now I will again right-click on the operation I hit Start Policy Debugging and then Send Request.

Start Debugging Policy in Azure API ManagementStart Debugging Policy in Azure API Management

What happens in debug-mode now is that the request is being stopped at the top if the inbound-section. On the left side we can see all the information and data of the request. In the top we see the usual commands for debugging.

I set a breakpoint at line 15+7=23.

Hitting Breakpoint when Debugging Policy in Azure API ManagementHitting Breakpoint when Debugging Policy in Azure API Management

As we continue debugging by clicking on the Play-button, the request will stop at the next breakpoint. We can now see in the Variables-window our variable test.

Variables in Debugging Policy in Azure API ManagementVariables in Debugging Policy in Azure API Management

We have now seen how we can debug policies. This is of course a simple example with one policy. You can go on from here and implement your product-, api- and global- policies in a much simpler way than before. I really hop this post helped you a bit.

Join Newsletter
Get the latest updates right in your inbox. I never spam!
Sven Malvik
Written by Sven Malvik

Latest Stories

Understanding Policies in Azure API Management

Policies are the heart of Azure API Management. They let us change the behavior of our APIs in a very flexible manner. Before I dive in t...

Apr 18, 2020

Event-Driven Infrastructure with App Configuration

Azure App Configuration is great for externalizing application configurations. But what if an application is our infrastructure? How coul...

Sep 12, 2020

Logging in Azure API Management

This post is a complete step-by-step guide on how to send logs from Azure API Management to Azure Event Hub with PowerShell. We start by ...

Apr 11, 2020

Introduction to Azure API Management

Azure API Management (APIM) is a way to create consistent and modern API gateways for existing backend services. It provides an interface...

Jan 25, 2021

How to Reference Key Vault Secrets in Azure API Management

In an enterprise, an Azure API Management instance is often shared by many teams and many developers. The developers may all have access ...

Feb 05, 2021

How to Secure Azure Functions App with Azure API Management

How to use an Azure Managed Identity to authenticate against an Azure Functions app that is exposed through Azure API Management. Our Fun...

Feb 02, 2021

How To Manage Azure Virtual Machines

I will go through the first steps for managing Virtual Machines. We will create a Windows VM, start the Internet Information Service IIS,...

Dec 26, 2020

Azure App Configuration Introduction

We build this great application that we configure exactly the way it fits into our environments, and then we realize that changing a conf...

May 09, 2020

Azure API Management with REST

Today, we will provision Azure API Management by using Postman as an API client, sending plain web requests to Azure. We already provisio...

Mar 14, 2020

Azure API Management from the Portal

We will provision Azure API Management. Creating an instance of it is just a click in the Azure Portal. We’ll do it once to show how easy...

Mar 07, 2020

Using Azure API Management APIs with Docker

We use Azure API Management in some cases for calling external services from Azure Kubernetes Service (AKS). Azure API Management acts in...

Jun 27, 2020

Latest Stories

Understanding Policies in Azure API Management

Understanding Policies in Azure API Management

Policies are the heart of Azure API Management. They let us change the behavior of our APIs in a very flexible manner. Before I dive in t...

Apr 18, 2020

Event-Driven Infrastructure with App Configuration

Event-Driven Infrastructure with App Configuration

Azure App Configuration is great for externalizing application configurations. But what if an application is our infrastructure? How coul...

Sep 12, 2020

Logging in Azure API Management

Logging in Azure API Management

This post is a complete step-by-step guide on how to send logs from Azure API Management to Azure Event Hub with PowerShell. We start by ...

Apr 11, 2020

Introduction to Azure API Management

Introduction to Azure API Management

Azure API Management (APIM) is a way to create consistent and modern API gateways for existing backend services. It provides an interface...

Jan 25, 2021

How to Reference Key Vault Secrets in Azure API Management

How to Reference Key Vault Secrets in Azure API Management

In an enterprise, an Azure API Management instance is often shared by many teams and many developers. The developers may all have access ...

Feb 05, 2021

How to Secure Azure Functions App with Azure API Management

How to Secure Azure Functions App with Azure API Management

How to use an Azure Managed Identity to authenticate against an Azure Functions app that is exposed through Azure API Management. Our Fun...

Feb 02, 2021

How To Manage Azure Virtual Machines

How To Manage Azure Virtual Machines

I will go through the first steps for managing Virtual Machines. We will create a Windows VM, start the Internet Information Service IIS,...

Dec 26, 2020

Azure App Configuration Introduction

Azure App Configuration Introduction

We build this great application that we configure exactly the way it fits into our environments, and then we realize that changing a conf...

May 09, 2020

Azure API Management with REST

Azure API Management with REST

Today, we will provision Azure API Management by using Postman as an API client, sending plain web requests to Azure. We already provisio...

Mar 14, 2020

Azure API Management from the Portal

Azure API Management from the Portal

We will provision Azure API Management. Creating an instance of it is just a click in the Azure Portal. We’ll do it once to show how easy...

Mar 07, 2020

Using Azure API Management APIs with Docker

Using Azure API Management APIs with Docker

We use Azure API Management in some cases for calling external services from Azure Kubernetes Service (AKS). Azure API Management acts in...

Jun 27, 2020