|
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 | +<!-- Confirm SKU availability for this feature. --> |
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 | +[!INCLUDE [api-management-availability-all-tiers](../../includes/api-management-availability-all-tiers.md)] |
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 | +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). |
| 19 | + |
| 20 | +Azure API Management policies are a sequence of statements that address cross-cutting concerns like authentication, authorization, throttling, caching, and transformation, applied to API requests or responses. |
18 | 21 |
|
19 | 22 | ## Prerequisites
|
20 | 23 |
|
21 |
| -* Create an API Management Developer-tier instance by following this [quickstart](get-started-create-service-instance.md) first. |
| 24 | +* [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). |
| 25 | + |
| 26 | +* The following extensions are needed for the specified scenarios: |
| 27 | + |
| 28 | + * [GitHub Copilot for Azure extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azure-github-copilot) - to create and explain policies |
| 29 | + * [REST Client extension](https://marketplace.visualstudio.com/items?itemName=humao.rest-client) - to debug policies |
22 | 30 |
|
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). |
| 31 | +<!-- Assume we need a step to install GH Copilot ext? --> |
| 32 | + |
| 33 | +* An API Management instance. If you need to create one, see this [quickstart](vscode-create-service-instance.md). |
| 34 | + |
| 35 | +<!-- Still can't use extension to create instance in v2 tiers? --> |
24 | 36 |
|
25 | 37 | * 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 | 38 |
|
27 | 39 | ## Restrictions and limitations
|
28 | 40 |
|
29 |
| -* This feature uses the built-in (service-level) all-access subscription (display name "Built-in all-access subscription") for debugging. |
| 41 | +* Policy debugging uses the built-in (service-level) all-access subscription (display name "Built-in all-access subscription") . |
| 42 | + |
| 43 | +[!INCLUDE [api-management-tracing-alert](../../includes/api-manaement-tracing-alert.md)] |
| 44 | + |
| 45 | +## Create polices using GitHub Copilot |
| 46 | +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. |
| 47 | + |
| 48 | +To create a policy: |
| 49 | + |
| 50 | +1. Open the API Management extension in Visual Studio Code. |
| 51 | +1. Select the API Management instance you want to work with. |
| 52 | +1. Select the policy scope that you want to work with. |
| 53 | + * To create a policy that applies to all APIs in the instance, select **Global Policy**. |
| 54 | + * To create a policy that applies to a specific API, under **APIs**, select the API, and then select **Policy**. |
| 55 | +1. In the policy editor window that appears, right-click and select **Copilot** > **Draft APIM Policies**. The Copilot Chat window appears. |
| 56 | +1. In the Copilot Chat window, enter a prompt in natural language to create a policy. Examples: |
| 57 | + |
| 58 | + ```copilot-prompt |
| 59 | + @azure I want to limit requests to 100 calls per minute |
| 60 | + ``` |
| 61 | +
|
| 62 | + ```copilot-prompt |
| 63 | + @azure Create a policy that sets the backend URL to https://mybackend.contoso.com and adds a header to the request |
| 64 | + ``` |
| 65 | +1. Copilot generates a policy draft in the chat window. You can refine the prompt if you want Copilot to change the output. |
| 66 | +
|
| 67 | +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**. |
| 68 | +
|
| 69 | +:::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."::: |
30 | 70 |
|
31 |
| -[!INCLUDE [api-management-tracing-alert](../../includes/api-management-tracing-alert.md)] |
| 71 | +> [!TIP] |
| 72 | +> Check in the **Problems** window for explanation of any issues in the policy XML. |
| 73 | +
|
| 74 | +## Explain a policy using GitHub Copilot |
| 75 | +
|
| 76 | +Copilot can help explain an existing policy in your API Management instance. |
| 77 | +
|
| 78 | +To receive an explanation: |
| 79 | +
|
| 80 | +1. Open the API Management extension in Visual Studio Code. |
| 81 | +1. Select the API Management instance you want to work with. |
| 82 | +1. Select the policy scope that you want to work with. |
| 83 | + * To explain a policy that applies to all APIs in the instance, select **Global Policy**. |
| 84 | + * To explain a policy that applies to a specific API, under **APIs**, select the API, and then select **Policy**. |
| 85 | +1. In the policy editor window that appears, right-click and select **Copilot** > **Explain APIM Policies**. |
| 86 | +1. An overview and explanation of policies in the policy XML document are generated in the Copilot Chat window. |
| 87 | +
|
| 88 | +:::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."::: |
32 | 89 |
|
33 |
| -## Initiate a debugging session |
| 90 | +## Debug policies using the VS Code extension |
34 | 91 |
|
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** |
| 92 | +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. |
40 | 93 |
|
41 |
| -At this point, the extension will try to initiate and establish a debugging session with the API Management gateway. |
| 94 | +> [!IMPORTANT] |
| 95 | +> Policy debugging in the Visual Studio Code extension is only supported in the API Management Developer tier. |
42 | 96 |
|
43 |
| - |
| 97 | +### Initiate a debugging session |
44 | 98 |
|
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). |
| 99 | +1. Open the API Management extension in Visual Studio Code. |
| 100 | +3. Select the API Management instance to debug. |
| 101 | +4. Select the API and operation to debug. |
| 102 | +5. Right click on the operation and select **Start Policy Debugging**. |
| 103 | +
|
| 104 | +At this point, the extension starts to establish a debugging session with the API Management gateway. Check the progress in the **Debug Console** windows. |
| 105 | +
|
| 106 | +:::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."::: |
| 107 | +
|
| 108 | +> [!NOTE] |
| 109 | +> Starting a debugging session also enables request tracing during the session, to help you diagnose and solve issues. |
| 110 | +
|
| 111 | +### Send a test request |
| 112 | +
|
| 113 | +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. |
47 | 114 |
|
48 | 115 | 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.
|
49 | 116 |
|
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. |
| 117 | +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. |
| 118 | +
|
51 | 119 |
|
52 |
| - |
| 120 | +:::image type="content" source="media/api-management-debug-policies/rest-client.png" alt-text="Screenshot of sending a test request in Visual Studio Code."::: |
53 | 121 |
|
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. |
| 122 | +### Debug policies |
| 123 | +After the test HTTP request is sent, the extension opens the debugging window, which shows the effective policies of this operation and stop at the first effective policy. |
56 | 124 |
|
57 |
| - |
| 125 | +:::image type="content" source="media/api-management-debug-policies/main-window.png" alt-text="Screenshot of the debugging window in Visual Studio Code."::: |
58 | 126 |
|
59 | 127 | 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 | 128 |
|
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. |
| 129 | +* In the **Variables** panel, inspect values of system-created and user-created variables. |
| 130 | +* In the **Breakpoints** panel, view the list of all breakpoints that have been set. |
| 131 | +* In the **Call Stack** panel, view the current effective policy scope. |
62 | 132 |
|
63 | 133 | If there is an error during policy execution, you will see the details of the error at the policy where it happened.
|
64 | 134 |
|
65 |
| - |
| 135 | +:::image type="content" source="media/api-management-debug-policies/exception.png" alt-text="Screenshot of a policy exception in Visual Studio Code."::: |
66 | 136 |
|
67 | 137 | > [!TIP]
|
68 |
| -> Remember to exit the debug session by clicking the **Stop** button when you are finished. |
| 138 | +> Exit the debugging session by clicking the **Stop** button when you are finished. |
69 | 139 |
|
70 | 140 |
|
71 | 141 | ## Related content
|
|
0 commit comments