🔥 Help shape the future of Azure Tips and Tricks by telling what you'd like for us to write about here (opens new window).

💡 Learn more : API Management overview (opens new window).

📺 Watch the video : How to use Azure API Management from VS Code (opens new window).

# How to use Azure API Management from VS Code

# Expose APIs on your terms

When you expose an API that other people use, you want full control over who uses it and how they use it. You can use Azure API Management (opens new window) for that. You import API definitions into Azure API Management and create a facade API that users communicate with. This allows you to create products (opens new window) for your API, and monitor and manage API usage and access with policies (opens new window). API Management also enables you to manage versions (opens new window) of your API. It also allows you to design the API methods (opens new window) and documentation that you expose, so that it works for your users, regardless of what the original API looks like. And you can run API Management in several usage tiers (opens new window), including a consumption tier, which is billed per execution.

You typically manage Azure API Management through the Azure portal. And now, there is a new Visual Studio Code Extension for Azure API Management (opens new window) that enables you to do everything with API Management, straight from your development environment. In this post, we'll explore the Azure API Management extension for VS Code.

# Prerequisites

If you want to follow along, you'll need the following:

# Use Azure API Management from Visual Studio Code

Let's try out the Azure API Management extension for VS Code.

  1. Open VS Code
  2. Navigate to the Extensions menu
  3. Search for the "Azure API Management Extension" and select the result
  4. Click install to install the extension

(Azure API Management extension for VS Code)

  1. With the extension installed, you'll see an Azure menu item in VS Code. Navigate to it. If you aren't using any other Azure extensions yet, you might have to sign into Azure
  2. You can create an Azure API Management instance from VS Code. By default, the creation process will only ask for a name. And you can also change that to customize the creation. Let's do that. Go to the Settings menu of VS Code
    1. In Settings > Extensions > Azure API Management, check Azure API Management: Advanced Creation
    2. Go back to the Azure menu
  3. In the API Management Extension, right-click on your Azure subscription and select Create API Management in Azure
    1. Fill in a Name for the API Management instance
    2. Pick a usage tier. Consumption is fine
    3. Select a Resource Group
    4. Pick a Location
    5. Hit Enter to start creating the API Management instance

(Create API Management in VS Code)

When the API Management instance is created, we can import an API definition into it. This will create a facade that users can consume.

  1. Expand the API Management instance to see the APIs node
  2. Right-click on APIs and select Import from Azure Functions. You can also import an API from a URL like this sample API: https://conferenceapi.azurewebsites.net/?format=json
    1. Select your Azure Subscription
    2. Pick the Azure Function you'd like to use
    3. Select the API methods that you want to import
    4. And give the API facade a Name
    5. Hit Enter to import the API

When the API is imported, you can edit its facade definition. You can, for instance, change it to accept HTTP and HTTPS requests. And you can change its policy, or change the global policy that applies to all APIS. Let's change the API policy.

  1. Expand the node of the imported API. You'll see an Operations and Policy node
  2. Select the Policy node. This opens a cshtml file that describes the policy. Here, you can create an advanced policy that can do anything from changing HTTP headers to restricting the amount of API calls
  3. Change the outbound code for the policy into the code below. This adds an HTTP header called "Custom"
    <set-header name="Custom" exists-action="override">
            <value>"My custom value"</value>
    <base />
  1. Save the policy file. VS Code will prompt you to upload the changes to API Management. Click Upload to do that
  2. Let's try it out. Expand the Operations node
  3. Right-click on the Operation that you want to test and select Test Operation. This opens a test file

(Test API operation in VS Code)

  1. As we are going to call an operation of the API Management facade, we need to add the API Management subscription key to the request. Right-click on the API Management instance and select Copy Subscription Key
  2. Paste the subscription key over the value NaN
  3. Click Send request to test the operation. You'll get a response back, which includes the HTTP header called "Custom"

(API response in VS Code)

# Conclusion

Azure API Management (opens new window) is a powerful service that enables you to expose and manage APIs to customers. And now you can manage it without leaving VS Code (opens new window). Go and check it out!