Introduction to Azure API Management
This tutorial will guide you through the essential steps of using Azure API Management (APIM) to manage, secure, and publish your APIs. You will learn how to create an APIM instance, import an API, apply policies, and make it accessible to developers.
Prerequisites
Before you begin, ensure you have the following:
- An Azure subscription. If you don't have one, you can sign up for a free trial.
- A sample API that you want to expose. For this tutorial, we'll use the "Pet Store" sample API.
Step 1: Create an Azure API Management Instance
Follow these steps to create a new APIM instance in the Azure portal:
- Navigate to the Azure portal.
- Click on Create a resource.
- Search for "API Management" and select it.
- Click Create.
- Fill in the required details:
- Subscription: Select your Azure subscription.
- Resource group: Create a new one or select an existing one.
- Name: A unique name for your APIM instance.
- Location: Choose a region.
- Operating system: Select your preferred OS.
- Pricing tier: For testing, the Developer tier is sufficient.
- Click Review + create, then Create.
Step 2: Import and Publish Your First API
Once your APIM instance is deployed, you can start adding APIs.
- Go to your API Management resource in the Azure portal.
- In the left-hand menu, under APIs, select APIs.
- Click Add API.
- Choose OpenAPI from the list.
- Configure the following:
- Specification URL or file: Enter the URL for your OpenAPI definition (e.g., https://petstore.swagger.io/v2/swagger.json).
- Display name: "Pet Store API"
- Name: "petstoreapi"
- API URL suffix: "petstore"
- Click Create.
Step 3: Configure Backend Service
By default, APIM uses the service URL from the OpenAPI definition. You can modify this if needed.
- In the APIs list, select the "Pet Store API" you just added.
- Go to the Settings tab.
- Under Gateway URL, you can see the URL where your API is exposed.
- The Backend service URL points to the actual API implementation. For the Swagger Petstore, this might be an internal URL or an external one. Ensure it's accessible from APIM.
Step 4: Add an API Policy
Policies allow you to transform and control the flow of requests and responses.
- With the "Pet Store API" selected, navigate to the Design tab.
- Select the All operations scope from the dropdown.
- In the Inbound processing section, click + Add policy.
- Choose Rate limit by subscription from the policy snippets.
- Review the XML policy code. It will look something like this:
<rate-limit-by-subscription calls="100" renewal-period="60" increment-condition="@(context.Subscription.Key)" /> - Click Save. This policy limits subscribers to 100 calls per minute.
Step 5: Test Your API
The built-in test console allows you to send requests to your API.
- In the "Pet Store API" view, select the Test tab.
- Choose an operation, for example, GET /store/inventory.
- Click Send. You should see the API response.
- Try testing operations that require authentication or parameters to understand how they work.
Step 6: Create and Publish a Product
Products are used to group APIs and offer them to developers.
- In the left-hand menu, under Products, select Products.
- Click + Add Product.
- Configure the product:
- Display name: "Basic Pet Store Access"
- Name: "basicpetstore"
- Description: "Provides access to the Pet Store API with basic rate limits."
- Terms of use: Add your terms.
- Ensure Published is checked.
- Click Create.
- Once the product is created, select it and go to the APIs tab.
- Click + Add API and select the "Pet Store API".
Step 7: Subscribe to a Product
Developers need a subscription key to access APIs grouped within a product.
- In the left-hand menu, under Products, select Products.
- Select the "Basic Pet Store Access" product.
- Go to the Subscriptions tab.
- Click + Add Subscription.
- If you have users, select them. Otherwise, you can create a subscription for yourself as a test.
- Click Create.
- You will see a subscription key. This key is required to call the APIs included in this product.
Step 8: Clean Up Resources
To avoid ongoing charges, it's recommended to clean up resources you no longer need.
- Navigate to your Resource group in the Azure portal.
- Select the resource group.
- Click Delete resource group and confirm the deletion.