|
1 | 1 | ---
|
2 |
| -title: Debug Azure API Management policies in Visual Studio Code | Microsoft Docs |
3 |
| -description: Learn how to debug Azure API Management Policies using the Azure API Management Visual Studio Code extension |
| 2 | +title: Create and debug Azure API Management policies in VS Code |
| 3 | +description: Learn how to create, understand, and debug Azure API Management Policies with Copilot assistance using the Azure API Management Visual Studio Code extension. |
4 | 4 | author: dlepow
|
5 | 5 | ms.service: azure-api-management
|
6 | 6 | ms.topic: how-to
|
7 |
| -ms.date: 09/22/2020 |
| 7 | +ms.date: 03/27/2025 |
8 | 8 | ms.author: danlep
|
| 9 | +ms.collection: ce-skilling-ai-copilot |
9 | 10 | ---
|
10 | 11 |
|
11 |
| -# Debug Azure API Management policies in Visual Studio Code |
| 12 | +# Create, understand, and debug Azure API Management policies in Visual Studio Code |
12 | 13 |
|
13 |
| -[!INCLUDE [api-management-availability-developer](../../includes/api-management-availability-developer.md)] |
| 14 | +[!INCLUDE [api-management-availability-all-tiers](../../includes/api-management-availability-all-tiers.md)] |
14 | 15 |
|
15 |
| -[Policies](api-management-policies.md) 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. |
| 16 | +This article explains how to use the [Azure API Management Extension for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-apimanagement) to create, understand, and debug Azure API Management policies with AI assistance from [GitHub Copilot for Azure](/azure/developer/github-copilot-azure/introduction). |
16 | 17 |
|
17 |
| -This article describes how to debug API Management policies using the [Azure API Management Extension for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-apimanagement). |
| 18 | +Azure API Management [policies](api-management-howto-policies.md) are a sequence of statements that address cross-cutting concerns like authentication, authorization, throttling, caching, and transformation, applied to API requests or responses. |
18 | 19 |
|
19 | 20 | ## Prerequisites
|
20 | 21 |
|
21 |
| -* Create an API Management Developer-tier instance by following this [quickstart](get-started-create-service-instance.md) first. |
| 22 | +* [Visual Studio Code](https://code.visualstudio.com/) and the latest version of [Azure API Management Extension for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-apimanagement). |
22 | 23 |
|
23 |
| -* Install [Visual Studio Code](https://code.visualstudio.com/) and the latest version of [Azure API Management Extension for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-apimanagement). |
| 24 | +* The following extensions are needed for the specified scenarios: |
| 25 | + |
| 26 | + * [GitHub Copilot for Azure extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azure-github-copilot) - to create and explain policies |
| 27 | + * [REST Client extension](https://marketplace.visualstudio.com/items?itemName=humao.rest-client) - for policy debugging |
| 28 | + |
| 29 | +* An API Management instance. If you need to create one, see this [quickstart](vscode-create-service-instance.md). |
24 | 30 |
|
25 | 31 | * 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](visual-studio-code-tutorial.md).
|
26 | 32 |
|
27 | 33 | ## Restrictions and limitations
|
28 | 34 |
|
29 |
| -* This feature uses the built-in (service-level) all-access subscription (display name "Built-in all-access subscription") for debugging. |
| 35 | +* Policy debugging is currently supported only in the API Management Developer tier. |
| 36 | + |
| 37 | +* Policy debugging uses the built-in (service-level) all-access subscription (display name "Built-in all-access subscription"). You must be a service owner to use this subscription. |
| 38 | + |
| 39 | +## Create a policy using GitHub Copilot for Azure |
| 40 | + |
| 41 | +The Azure API Management extension integrates with GitHub Copilot for Azure to help you create a draft of a policy. With AI assistance, create and edit policies in a user-friendly way. |
| 42 | + |
| 43 | +To create a policy: |
| 44 | + |
| 45 | +1. Open the API Management extension in Visual Studio Code. |
| 46 | +1. Select the API Management instance you want to work with. |
| 47 | +1. Select the policy scope that you want to work with. |
| 48 | + * To create a policy that applies to all APIs in the instance, select **Global Policy**. |
| 49 | + * To create a policy that applies to a specific API, under **APIs**, select the API, and then select **Policy**. |
| 50 | + * To create a policy that applies to a specific API operation, select the operation, and then select **Policy**. |
| 51 | + |
| 52 | +1. In the policy editor window that appears, right-click and select **Copilot** > **Draft APIM Policies**. The Copilot Chat window appears. |
| 53 | +1. In the Copilot Chat window, enter a prompt in natural language to create a policy. Examples: |
| 54 | + |
| 55 | + ```copilot-prompt |
| 56 | + @azure I want to limit requests to 100 calls per minute |
| 57 | + ``` |
| 58 | +
|
| 59 | + ```copilot-prompt |
| 60 | + @azure Create a policy that sets the backend URL to https://mybackend.contoso.com and adds a custom header to the request |
| 61 | + ``` |
| 62 | +1. Copilot generates a policy draft in the chat window. You can refine the prompt if you want Copilot to change the output. |
| 63 | +
|
| 64 | +1. When you're ready, select the **Copy** button to copy the policy to the clipboard, or select **Apply in Editor** to insert the policy in the editor window. If you are satisfied with the change in the policy XML, select **Keep**. |
| 65 | +
|
| 66 | +:::image type="content" source="media/api-management-debug-policies/draft-policy.gif" alt-text="Animation showing drafting of an API Management policy in VS Code."::: |
| 67 | +
|
| 68 | +> [!TIP] |
| 69 | +> Check in the **Problems** window for explanation of any issues in the policy XML. |
| 70 | +
|
| 71 | +## Explain a policy using GitHub Copilot for Azure |
30 | 72 |
|
31 |
| -[!INCLUDE [api-management-tracing-alert](../../includes/api-management-tracing-alert.md)] |
| 73 | +Copilot can help explain an existing policy in your API Management instance. |
32 | 74 |
|
33 |
| -## Initiate a debugging session |
| 75 | +To receive an explanation: |
34 | 76 |
|
35 |
| -1. Launch Visual Studio Code |
36 |
| -2. Navigate to the API Management extension under Azure extensions |
37 |
| -3. Find the API Management instance to debug |
38 |
| -4. Find the API and operation to debug |
39 |
| -5. Right click on the operation and select **Start policy debugging** |
| 77 | +1. Open the API Management extension in Visual Studio Code. |
| 78 | +1. Select the API Management instance you want to work with. |
| 79 | +1. Select the policy scope that you want to work with. |
| 80 | + * To explain a policy that applies to all APIs in the instance, select **Global Policy**. |
| 81 | + * To explain a policy that applies to a specific API, under **APIs**, select the API, and then select **Policy**. |
| 82 | + * To explain a policy that applies to a specific API operation, select the operation, and then select **Policy**. |
40 | 83 |
|
41 |
| -At this point, the extension will try to initiate and establish a debugging session with the API Management gateway. |
| 84 | +1. In the policy editor window that appears, right-click and select **Copilot** > **Explain APIM Policies**. |
| 85 | +1. An overview and explanation of policies in the policy XML document are generated in the Copilot Chat window. |
42 | 86 |
|
43 |
| - |
| 87 | +:::image type="content" source="media/api-management-debug-policies/draft-policy.gif" alt-text="Animation showing explanation of an API Management policy in VS Code."::: |
44 | 88 |
|
45 |
| -## Send a test request |
46 |
| -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](https://marketplace.visualstudio.com/items?itemName=humao.rest-client). |
| 89 | +## Debug policies using the VS Code extension |
47 | 90 |
|
48 |
| -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. |
| 91 | +The Azure API Management extension for Visual Studio Code provides a debugging experience that allows you to step through the policy pipeline or set a breakpoint when you send a request to the gateway. |
49 | 92 |
|
50 |
| -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. |
| 93 | +### Initiate a debugging session |
51 | 94 |
|
52 |
| - |
| 95 | +1. Open the API Management extension in Visual Studio Code. |
| 96 | +3. Select the API Management instance to debug. |
| 97 | +4. Select the API and operation to debug. |
| 98 | +5. Right click on the operation and select **Start Policy Debugging**. |
53 | 99 |
|
54 |
| -## Debug policies |
55 |
| -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. |
| 100 | +At this point, the extension starts to establish a debugging session with the API Management gateway. Check the progress in the **Debug Console** window. |
56 | 101 |
|
57 |
| - |
| 102 | +:::image type="content" source="media/api-management-debug-policies/initiate-debugging-session.png" alt-text="Screenshot if initiating a policy debugging session in Visual Studio Code."::: |
| 103 | +
|
| 104 | +> [!NOTE] |
| 105 | +> Starting a debugging session also enables request tracing for the duration of the session, to help you diagnose and solve issues. |
| 106 | +
|
| 107 | +### Send a test request |
| 108 | +
|
| 109 | +When the debugging session is established, the REST Client extension opens a new editor that allows you to create and send a test HTTP request to the gateway. |
| 110 | +
|
| 111 | +The **Ocp-Apim-Debug** header is added automatically 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. |
| 112 | +
|
| 113 | +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. |
| 114 | +
|
| 115 | +
|
| 116 | +:::image type="content" source="media/api-management-debug-policies/rest-client.png" alt-text="Screenshot of sending a test request in Visual Studio Code."::: |
| 117 | +
|
| 118 | +### Debug policies |
| 119 | +
|
| 120 | +After the test HTTP request is sent, the extension opens the debugging window, which shows the effective policies of this operation and stops at the first effective policy. |
| 121 | +
|
| 122 | +:::image type="content" source="media/api-management-debug-policies/main-window.png" alt-text="Screenshot of the debugging window in Visual Studio Code."::: |
58 | 123 |
|
59 | 124 | 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.
|
60 | 125 |
|
61 |
| -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. |
| 126 | +* In the **Variables** panel, inspect values of system-created and user-created variables. |
| 127 | +* In the **Breakpoints** panel, view the list of all breakpoints that have been set. |
| 128 | +* In the **Call Stack** panel, view the current effective policy scope. |
62 | 129 |
|
63 | 130 | If there is an error during policy execution, you will see the details of the error at the policy where it happened.
|
64 | 131 |
|
65 |
| - |
| 132 | +Exit the debugging session by clicking the **Stop** button when you are finished. |
66 | 133 |
|
67 |
| -> [!TIP] |
68 |
| -> Remember to exit the debug session by clicking the **Stop** button when you are finished. |
| 134 | +:::image type="content" source="media/api-management-debug-policies/exception.png" alt-text="Screenshot of a policy exception in Visual Studio Code."::: |
69 | 135 |
|
| 136 | +> [!TIP] |
| 137 | +> To review the request trace, select the value of the `Ocp-Apim-Trace-Location` header that is provided in the HTTP response. |
70 | 138 |
|
71 | 139 | ## Related content
|
72 | 140 |
|
|
0 commit comments