Thursday, 5 November 2026

What is Azure API Management (APIM), and why do we use it?

Azure API Management (APIM)

Azure API Management is a managed service that sits in front of your APIs as a gateway. Clients call APIM, and APIM applies policies (authentication, rate limiting, transformation, caching) before forwarding requests to your backends. The backends can be App Service, Functions, AKS, Container Apps, on-premises systems, or even other clouds.

The three components

  • Gateway: the runtime proxy that receives API calls, enforces policies, and routes to backends. This is the data plane.
  • Management plane: the Azure portal, CLI, Bicep, or REST API, where you define APIs, products, policies, and users.
  • Developer portal: an auto-generated, customizable website where API consumers browse docs, try calls, and get subscription keys.

Self-hosted gateway: you can also run the gateway in containers on-premises or in other clouds, managed from the same APIM instance (Developer and Premium tiers).

Core concepts

  • API: a set of operations (e.g., GET /orders/{id}), imported from OpenAPI, WSDL, an App Service, a Function App, or created manually.
  • Product: a bundle of APIs with its own terms, quotas, and visibility (e.g., "Free" vs. "Premium" plans).
  • Subscription: a key a consumer uses to access a product or API (sent in the Ocp-Apim-Subscription-Key header by default).
  • Policy: XML statements that run on the request or response pipeline at global, product, API, or operation scope.
  • Named values: configuration and secrets used inside policies (can be backed by Key Vault).
  • Backend: a reusable definition of a downstream service, with URL, credentials, and circuit breaker settings.

Why use it

1. Security at the edge

  • Validate JWTs from Entra ID before requests reach your backend
  • Enforce subscription keys, client certificates, and IP filtering
  • Authenticate to backends with managed identity, so backends never expose public credentials
  • Centralize security policies instead of re-implementing them in every service

2. Traffic control

  • Rate limiting and quotas per client, per key, or per IP
  • Protection against abusive callers and backend overload
  • Response caching to reduce backend load and latency
  • Retry and circuit breaker behavior for flaky backends

3. A single, consistent front door

  • One public URL and domain across many backend services
  • Backends can move or be replaced without breaking consumers
  • Consistent URL structure, headers, and error formats

4. Versioning and lifecycle

  • Run multiple versions (/v1, /v2) side by side
  • Revisions let you make non-breaking changes and test them before making them current

5. Transformation and mediation

  • Rewrite URLs, add or strip headers, convert XML to JSON, mask fields in responses
  • Expose a clean API over a messy legacy backend without changing it

6. Observability

  • Built-in analytics, plus diagnostic logs and traces to Application Insights and Azure Monitor
  • Correlation IDs, per-API and per-consumer usage and latency

7. Developer experience and monetization

  • Self-service portal with documentation and a test console
  • Products and subscriptions let you onboard partners, issue keys, and apply different limits per tier

Example policies

Validate an Entra ID token, rate limit, and forward using managed identity:

<policies>
  <inbound>
    <base />
    <validate-jwt header-name="Authorization" failed-validation-httpcode="401"
                  failed-validation-error-message="Unauthorized">
      <openid-config url="https://login.microsoftonline.com/<tenant-id>/v2.0/.well-known/openid-configuration" />
      <audiences>
        <audience>api://<api-client-id></audience>
      </audiences>
      <required-claims>
        <claim name="roles" match="any">
          <value>Orders.Read</value>
        </claim>
      </required-claims>
    </validate-jwt>

    <rate-limit-by-key calls="100" renewal-period="60"
                       counter-key="@(context.Request.IpAddress)" />

    <authentication-managed-identity resource="api://<backend-client-id>" />
  </inbound>
  <backend>
    <base />
  </backend>
  <outbound>
    <base />
    <set-header name="X-Powered-By" exists-action="delete" />
  </outbound>
  <on-error>
    <base />
  </on-error>
</policies>

Response caching:

<inbound>
  <base />
  <cache-lookup vary-by-developer="false" vary-by-developer-groups="false" />
</inbound>
<outbound>
  <base />
  <cache-store duration="60" />
</outbound>

Tiers

Tier Typical use
Consumption Serverless, pay per call, scale to zero; good for low or spiky traffic (no VNet injection, limited features)
Developer Non-production, for evaluation and testing (no SLA)
Basic / Standard Small to mid-sized production workloads (classic tiers)
Premium Multi-region, availability zones, VNet injection, self-hosted gateway, highest scale
v2 tiers (Basic v2, Standard v2, Premium v2) Newer generation with faster provisioning and scaling, and simpler VNet integration

Tier names and feature sets change over time, so check the current pricing and feature comparison before choosing.

Typical architecture

Client ─► Front Door / App Gateway (WAF) ─► APIM ─► Backends
                                              │       ├─ App Service (Orders API)
                                              │       ├─ Functions (Webhooks)
                                              │       └─ AKS / Container Apps (Inventory)
                                              └─► Application Insights

Lock backends down so they only accept traffic from APIM (access restrictions, private endpoints, or a shared header or token check). Otherwise callers can bypass your policies by hitting the backend directly.

When APIM is a good fit

  • You expose APIs to external partners or the public and need keys, quotas, and a developer portal
  • You have multiple backend services and want one consistent entry point
  • You need centralized auth, throttling, and logging without changing each service
  • You're modernizing legacy services and want to hide their quirks behind a clean facade
  • You need to publish APIs across regions or hybrid environments

When it may be overkill

  • A single internal API with one consumer, where app-level auth and Easy Auth suffice
  • Very low-budget or prototype projects (the non-Consumption tiers carry a meaningful fixed monthly cost)
  • Scenarios needing only WAF and routing, where Front Door or Application Gateway is simpler
  • Latency-critical hot paths, where every extra hop matters (APIM adds some overhead, usually small)

APIM vs. related services

APIM Front Door / App Gateway Easy Auth
Focus API lifecycle, policy, consumer management Global routing, WAF, load balancing Simple built-in authentication
Understands API operations and products Yes No No
Rate limiting per consumer/key Yes Basic (WAF rate rules) No
Developer portal Yes No No

They're complementary: Front Door provides global entry and WAF, and APIM provides API governance behind it.

Good practices:

  • Define APIM as code (Bicep or Terraform, with policies in versioned files) and deploy through CI/CD. Use the APIOps approach to promote API definitions between environments.
  • Use managed identity for backend authentication, and Key Vault-backed named values for secrets.
  • Validate tokens in APIM and also in the backend (defense in depth), and restrict backends to APIM traffic only.
  • Keep policies simple and reusable with fragments, because complex policy logic is hard to test and debug.
  • Send diagnostics to Application Insights and watch for gateway versus backend latency and failures.
  • Use revisions and versions deliberately: revisions for non-breaking changes, versions for breaking ones.
  • Plan capacity and region placement: choose a tier that supports the VNet and availability needs you actually have, because some of these can't be changed later without redeploying.


No comments:

Post a Comment

Note: only a member of this blog may post a comment.