Skip to content
Visual explainer 2h 30m to learn 5 steps

Put API Management in front of an API

Publish an API through API Management with backend identity, rate limits, versions and logging.

By Invicly Tech, , in Microsoft Azure Cloud Engineering. About 12 minutes to read.

Topic 19 of 33

In short

Azure API Management (APIM) is a gateway that sits between API callers and your backend. It gives one public front door where you apply policies for authentication, rate limits, transformation and logging, and where you publish versions without changing the backend. Which features you get, such as virtual network support or a self-hosted gateway, depends on the pricing tier.

Think of it like this: APIM is the reception desk of an office: it checks who you are, how often you have visited, and sends you to the right room.

Explained simply

An API is how programs talk to your service. As soon as other teams or customers use it, you need answers to new questions. Who is calling? How often may they call? What happens when you change the API? Where do you see errors?

Azure API Management, often called APIM, answers those questions in one place. Callers talk to the APIM gateway instead of your service. The gateway checks who they are, counts their calls, forwards the request to your real service, and records what happened.

The rules the gateway follows are called policies. A policy might say each customer can make 100 calls a minute, or add a header, or reject requests without a valid token. You change policies without touching your backend code.

APIM comes in several tiers. The cheaper ones are great for learning and small apps. Features such as private networking, multiple regions or running the gateway in your own data centre need the bigger tiers. Check the tier before you promise a design.

Press Play on a diagram to walk through it one step at a time.

SequenceOne call through API Management
Client
APIM gateway
Microsoft Entra ID
Backend API
  1. 1GET /orders/v2/42 with subscription key
    Client to APIM gateway: GET /orders/v2/42 with subscription key
  2. 2Key and rate limit pass; request token with managed identity
    APIM gateway to Microsoft Entra ID: Key and rate limit pass; request token with managed identity
  3. 3Access token for the backend
    Microsoft Entra ID to APIM gateway: Access token for the backend
  4. 4Forward request with bearer token
    APIM gateway to Backend API: Forward request with bearer token
  5. 5200 response
    Backend API to APIM gateway: 200 response
  6. 6Outbound policies, then reply; log the call
    APIM gateway to Client: Outbound policies, then reply; log the call
FlowPolicy sections in the order they run
  1. Step 1
    inbound

    Authenticate, rate limit, rewrite the request

  2. Step 2
    backend

    Forward to the backend service

  3. Step 3
    outbound

    Transform or filter the response

  4. Step 4
    on-error

    Runs only if any step fails, shapes the error reply

In depth

Parts of an APIM instance

An instance has three parts. The gateway is the data plane: it receives every API call at a host name such as apim-contoso.azure-api.net, runs policies and forwards to the backend. The management plane is where you define APIs, operations, products, policies and subscriptions, through the portal, the CLI, Bicep or the REST API. The developer portal is an optional website where API consumers read documentation, try calls and get subscription keys. You usually import an API from an OpenAPI file, which creates its operations automatically, then set the backend URL, the URL suffix (for example orders), and the products it belongs to. A product groups APIs and controls who may subscribe and whether approval is needed.

LayersWhere a policy can be attached
  1. 1
    Global (all APIs)

    Company-wide rules such as CORS or logging headers

  2. 2
    Product

    Limits for a group of subscribers, such as a free plan

  3. 3
    API

    Backend authentication for the orders API

  4. 4
    Operation

    Caching for GET /orders/{id} only

Securing the caller side and the backend side

There are two separate trust problems. On the caller side, a subscription key identifies which subscription is calling, which is useful for limits and billing, but it is a shared secret, not proof of a user. For real authentication add validate-jwt or validate-azure-ad-token in the inbound section to check an OAuth 2.0 access token from Microsoft Entra ID. On the backend side, the gateway must prove itself to your API. Turn on a system-assigned or user-assigned managed identity for APIM and use authentication-managed-identity to get a token for the backend's app ID. Then make sure callers cannot skip the gateway: require that token in the backend, and restrict the backend's network access to the APIM instance where the tier allows it.

  • Subscription key: who is calling, for limits
  • validate-jwt: is the caller's token valid
  • Managed identity: gateway to backend, no secrets
  • Close the backend so APIM is the only way in

Rate limits and quotas

Rate limits protect your backend from short bursts; quotas cap total use over a longer period. rate-limit counts calls per subscription in a renewal period, such as 100 calls in 60 seconds. rate-limit-by-key counts per any key you choose, such as the caller's IP address or a claim from the token. When a caller goes over, APIM returns 429 Too Many Requests with a Retry-After header and the backend never sees the call. quota and quota-by-key work the same way over hours, days or months, and can also limit bandwidth. Counters are kept per gateway, so in multi-unit or multi-region setups the limit is approximate rather than exact. Not every policy is supported in every tier, and the policy reference lists supported gateways for each one.

Versions, revisions and change

A version is a separate contract. Use one for breaking changes, such as renaming a field. Versions belong to a version set, and callers pick one by path (/orders/v2), header (Api-Version: v2) or query string (?api-version=v2). Old versions keep working until you retire them, which gives consumers time to move. A revision is a non-breaking change to the same version, such as adding an optional field or a policy. You create a revision, test it through its own URL with ;rev=2 after the API path, and then make it current with a change log note. If it misbehaves, make the previous revision current again. This lets you change and roll back gateway behaviour without touching callers.

CompareVersion or revision
Version
  • For breaking changes
  • Callers choose it explicitly
  • Path, header or query scheme
  • Old and new run side by side
Revision
  • For non-breaking changes
  • Tested via ;rev=N before release
  • Made current in one step
  • Roll back by making the old one current

Logs and tier-dependent features

For evidence, connect APIM to Application Insights for request traces and failures, and add a diagnostic setting that sends gateway logs to a Log Analytics workspace, where each call becomes a row you can query with KQL. Be careful with sampling and with logging bodies, which may contain personal data. Tiers differ a lot. Consumption is serverless and billed per call, with a free monthly allowance, but has a smaller feature set. Developer is cheap and full-featured for testing but has no SLA and must not run production. Basic, Standard and Premium, and the newer v2 tiers, add capacity and features; Premium adds multi-region deployment, availability zones and the self-hosted gateway. Networking options also vary by tier, so check the current feature comparison page before choosing.

  • Consumption: pay per call, fast to create
  • Developer: testing only, no SLA
  • Premium: multi-region, zones, self-hosted gateway
  • Check networking support per tier on the comparison page

Learn it in 5 short steps

About 30m each. Track them with a timer in your plan.

  1. 1
    Import an API~30m
  2. 2
    Authenticate to the backend~30m
  3. 3
    Add a rate-limit policy~30m
  4. 4
    Version the API~30m
  5. 5
    Check tier-dependent features~30m

Key points

  • Policies are XML statements in inbound, backend, outbound and on-error sections.
  • Policies apply at global, product, API and operation scope; base pulls in the parent scope.
  • A managed identity lets the gateway call the backend without stored secrets.
  • Versions are for breaking changes callers choose; revisions are for safe, non-breaking edits.
  • Tiers differ in SLA, scale, networking and gateway options; check before you design.

Key terms

Tap a card to flip it. 0/6 known

Examples

xml
<policies>
  <inbound>
    <base />
    <rate-limit-by-key calls="100" renewal-period="60"
        counter-key="@(context.Subscription.Id)" />
    <authentication-managed-identity resource="api://<backend-app-id>" />
  </inbound>
  <backend><base /></backend>
  <outbound><base /></outbound>
  <on-error><base /></on-error>
</policies>

Limit each subscription to 100 calls a minute and call the backend with the gateway's managed identity.

Publish a test API on the Consumption tier

Prerequisites: an Azure subscription and the Azure CLI. Consumption is billed per call with a free monthly allowance, so a short lab costs little; a design-only alternative is to write the policy files and review them without deploying. Step 1: create the instance; Consumption is ready in minutes, while classic tiers can take much longer. Step 2: import a public sample OpenAPI file with the path orders. Step 3: copy a subscription key from the built-in all-access subscription in the portal. Step 4: call the API with curl. Evidence: a 200 response and, after a few minutes, a row in the gateway logs. Cleanup: delete the resource group.

bash
az group create -n rg-apim-lab -l westeurope
az apim create -n apim-contoso-lab -g rg-apim-lab \
  --publisher-name Contoso --publisher-email admin@contoso.com \
  --sku-name Consumption
az apim api import -g rg-apim-lab --service-name apim-contoso-lab \
  --api-id orders --path orders --specification-format OpenApi \
  --specification-url https://example.com/openapi.json
curl -i -H "Ocp-Apim-Subscription-Key: <key>" \
  https://apim-contoso-lab.azure-api.net/orders/42
az group delete -n rg-apim-lab --yes

Adding a rate limit and watching it work

Step 1: in the orders API, open the inbound policy and add rate-limit-by-key with calls set to 5, renewal-period 60, and the subscription ID as the counter key. Step 2: call the API ten times in a loop. Evidence: the first five calls return 200 and the rest return 429 with a Retry-After header. Step 3: wait a minute and call again; it succeeds. Step 4: in a real system, put the limit on the product, so a free plan and a paid plan get different numbers. Rollback: remove the policy line, or, if you made the change in a new revision, make the old revision current again.

bash
for i in $(seq 1 10); do
  curl -s -o /dev/null -w "%{http_code}\n" \
    -H "Ocp-Apim-Subscription-Key: <key>" \
    https://apim-contoso-lab.azure-api.net/orders/42
done
# expected: 200 x5 then 429 x5

Quick reference

  • Policy sections run inbound, backend, outbound; on-error runs only on failure
  • Keep base in each section unless you mean to drop parent-scope policies
  • Caller key header: Ocp-Apim-Subscription-Key
  • Backend without secrets: managed identity plus authentication-managed-identity
  • Over the limit: 429 Too Many Requests with Retry-After
  • Breaking change: new version; safe change: new revision tested with ;rev=N
  • Developer tier has no SLA; never use it for production traffic
  • Lock the backend down so callers cannot bypass the gateway

Common mistakes

  • Leaving the backend reachable directly from the internet, so callers bypass every policy.
  • Removing base from a policy section and silently dropping the global policies.
  • Designing around virtual network injection or a self-hosted gateway on a tier that does not offer it.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1Which part of APIM receives API calls and runs policies?

Question 2

2You add an optional field to a response. How should you release it in APIM?

Question 3

3What does a subscription key prove?

Question 4

4Your design needs the gateway to run in your own data centre. What must you check?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    List the four policy sections in the order they apply, and say which one should hold a rate limit and which one should strip an internal response header.

  2. Exercise 2medium

    Write an inbound policy that allows 50 calls per minute per client IP address and adds a header X-Request-Source with the value apim.

  3. Exercise 3hard

    Design the change from /orders/v1 to /orders/v2, which renames customerId to accountId. Include versioning scheme, how v1 keeps working, how the backend is protected, and how you will know when v1 can be retired.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Do I need API Management for a single small API?
Not always. If you have one internal consumer and your platform already handles authentication and limits, APIM may add cost and delay. It pays off when you have several APIs, external consumers, or need central policies and analytics.
Does API Management add latency?
Yes, a little, because each call takes an extra hop and runs policies. Placing APIM in the same region as the backend and keeping policies simple keeps the overhead small.
Is the Developer tier fine for a small production app?
No. It has no SLA and is meant for evaluation and testing. Use Consumption or a Basic or v2 tier for small production workloads.

Go deeper

ShareXLinkedInWhatsApp