Debug Azure API Management policies in Visual Studio Code
APPLIES TO: Developer
Policies in Azure API Management provide powerful capabilities that help API publishers address cross-cutting concerns such as authentication, authorization, throttling, caching, and transformation. Policies are a collection of statements that are executed sequentially on the request or response of an API.
This article describes how to debug API Management policies using the Azure API Management Extension for Visual Studio Code.
Prerequisites
Create an API Management Developer-tier instance by following this quickstart first.
Install Visual Studio Code and the latest version of Azure API Management Extension for Visual Studio Code.
Import an API to your API Management instance. For example steps, see Tutorial: Use the API Management Extension for Visual Studio Code to import and manage APIs.
Restrictions and limitations
- This feature uses the built-in (service-level) all-access subscription (display name "Built-in all-access subscription") for debugging.
Important
- API Management request tracing using the Ocp-Apim-Trace header in a request and using the value of the Ocp-Apim-Trace-Location response header is being deprecated.
- To improve security, tracing can now be enabled at the level of an individual API by obtaining a time-limited token using the API Management REST API, and passing the token in a request to the gateway. For details, see Enable tracing of an API.
- Take care when enabling tracing, as it can expose sensitive information in the trace data. Ensure that you have appropriate security measures in place to protect the trace data.
Initiate a debugging session
- Launch Visual Studio Code
- Navigate to the API Management extension under Azure extensions
- Find the API Management instance to debug
- Find the API and operation to debug
- Right click on the operation and select Start policy debugging
At this point, the extension will try to initiate and establish a debugging session with the API Management gateway.
Send a test request
When the debugging session is established, the extension will open a new editor that allows us to create and send a test HTTP request to this operation leveraging the REST Client extension.
You will notice the Ocp-Apim-Debug header has already been added to the request. This header is required and the value must be set to the service-level, all-access subscription key to trigger the debugging functionality in the API Management gateway.
Modify the HTTP request in the editor according to your test scenario. Then click send request to send the test request to the API Management gateway.
Debug policies
After the test HTTP request is sent, the extension will open the debugging window showing the effective policies of this operation and stop at the first effective policy.
To follow the policy pipeline, you can single-step through individual policies or set a breakpoint at a policy and step directly to that policy.
In the Variables panel, you can inspect values of system-created and user-created variables. In the Breakpoints panel, you can see the list of all breakpoints that have been set. In the Call Stack panel, you can see the current effective policy scope.
If there is an error during policy execution, you will see the details of the error at the policy where it happened.
Tip
Remember to exit the debug session by clicking the Stop button when you are finished.
Next steps
- Learn more about the API Management extension for Visual Studio Code.
- Report issues in the GitHub repository