microsoft / vscode-apimanagement

VS Code extension for Azure API Management.
MIT License
59 stars 38 forks source link

What does {{azure-api-management-subscription-key}} mean? #355

Open jim-doble opened 1 month ago

jim-doble commented 1 month ago

I am new to Visual Studio Code, new to the Visual Studio Code API Management Extension, and new to Azure API Management, so I am really new.

When using the Visual Studio Code API Management Extension in Visual Studio Code, when I select Test Operation to test one of the operations in my API, a window displays showing the request to be sent, including a header named Ocp-Apim-Subscription-Key set to the value: {{azure-api-management-subscription-key}}.

I know what a subscription key is, and I have seen video tutorials which show copying the subscription key from the APIM and pasting it over the {{azure-api-management-subscription-key}} value, but I am wondering if there is a more convenient way. For example, can I define some sort of environment variable named "azure-api-management-subscription-key" which contains the API key, and if so, would this value be retrieved by the test command every time, without me needing to do that copy/paste each time?

I notice that in the test command window the string "azure-api-management-subscription-key" is underlined in red, and there is this error message: "azure-api-management-subscription-key is not found"

This seems to imply that the API Management Extension is looking for "azure-api-management-subscription-key" somewhere, but where is it looking? Is there somewhere I could set it, and it would be found? The implication is that the double curlies syntax (i.e. {{}}) has some sort of meaning, but I cannot for the life of me find an explanation of that what meaning actually is.

I also notice this text in the test window:

/A subscription key is required to call this API. //You can get the all-access subscription key by right clicking on your service and choose "Copy Subscription Key". //You can also set an environment variable, //see https://code.visualstudio.com/docs/editor/variables-reference#_environment-variables

I am wondering if this is a hint that I could store the subscription key in an environment variable. However, when I looked at the provided link on environment variables, there was all manner of confusing detail beyond anything I thought I was looking for, and I couldn't tell if I has heading in the right direction or had gone down a rabbit hole. I also notice in the environment variables documentation that the syntax for referencing an environment variable seems to be ${environment-variable-name} rather than {{environment-variable-name}} so I am still not sure what the double curlies syntax is about.

Is the {{}} syntax some sort of Visual Studio Code (or Visual Studio Code API Management Extension) syntax for referencing an environment variable? If so, if I wanted to set an environment variable named "azure-api-management-subscription-key" how would I actually do that?

DilaneR commented 1 month ago

@jim-doble It is used to reference naming values in your API Management Instance.

To answer your question, there is many ways to create a "environment variable". But regarding this subject. You can simply create in your API Management Instance a new naming value called "azure-api-management-subscription-key" and then reference it in the operation policies with the {{azure-api-management-subscription-key}}.

https://learn.microsoft.com/en-us/azure/api-management/api-management-howto-properties?tabs=azure-portal

jim-doble commented 1 month ago

In the case I described, I am not trying to reference {{azure-api-management-subscription-key}} from within a policy. Rather, it is being referenced from a Test Operation in the Visual Studio Code API Management Extension, like this:

`GET https://eastus2oc1dev.azure-api.net/echo/resource?param1=sample HTTP/1.1 Ocp-Apim-Subscription-Key: {{azure-api-management-subscription-key}} Ocp-Apim-Trace: true

//A subscription key is required to call this API. //You can get the all-access subscription key by right clicking on your service and choose "Copy Subscription Key". //You can also set an environment variable, //see https://code.visualstudio.com/docs/editor/variables-reference#_environment-variables`

I have tried creating a named value called "azure-api-management-subsciption-key" with the required value, but that does not appear to work. The Visual Studio Code API Management Extension still reports that it cannot find "azure-api-management-subsciption-key", and if I try the commend I still get a 401.

The fact that the Visual Studio Code API Management Extension is reporting that it cannot find "azure-api-management-subsciption-key" leads me to believe that the double curlies syntax means something to it, and that it is looking for that value somewhere, but it does not appear to be looking among the named values.