Azure API Management
Azure API Management is a managed platform for exposing and governing APIs across Azure, on-premises, hybrid, and multicloud environments.
Overview
Azure API Management (APIM) is a managed API gateway and governance platform whose runtime gateway is separate from its management plane and developer experience. A production design must cover API import and lifecycle, versions and revisions, products and subscriptions, caller identity, backend identity, policy scope and inheritance, request limits, transformations, caching, observability, networking, certificates, tier capabilities, zones or regions, capacity, recovery, and configuration delivery. APIM can enforce consistent edge policy, but the backend remains responsible for resource authorization, business validation, idempotency, and resilience. Classic, v2, Consumption, workspace, and self-hosted gateways do not have identical capabilities, so architects must validate the current feature matrix rather than assume policy or topology parity.
What you will learn
- Explain APIM planes, resources, gateway variants, and policy execution.
- Design caller and backend identity without moving domain authorization to the gateway.
- Manage contracts with versions, revisions, products, and subscriptions.
- Build safe traffic, transformation, cache, and failure policies.
- Choose tier, networking, zones, regions, and capacity from requirements.
- Test, deploy, observe, recover, and govern APIM configuration as code.
Prerequisites
- HTTP, REST/OpenAPI, OAuth 2.0, OpenID Connect, JWT, TLS, and DNS.
- ASP.NET Core authentication, authorization, resilience, and observability.
- Azure identity, RBAC, Key Vault, networking, Bicep, and deployment pipelines.
When to use Azure API Management
Good fit
- Multiple APIs need consistent exposure, policy, analytics, onboarding, and lifecycle governance.
- External or partner APIs need a managed gateway and developer experience.
- Hybrid backends need controlled centralized configuration.
Choose another approach when
- A single private service only needs simple ingress routing.
- Required gateway capability is absent from the selected tier or gateway.
- The team expects APIM to replace backend authorization or service reliability.
Key concepts
The gateway processes traffic; the management plane configures APIs, operations, backends, products, subscriptions, users, policies, revisions, versions, workspaces, and diagnostics.
Inbound, backend, outbound, and on-error sections execute at global, workspace, product, API, or operation scope with explicit base inheritance.
Validate caller tokens at the gateway, authorize domain resources in the backend, and use managed identity or certificates from APIM to protected backends.
Products package APIs for audiences and can use subscriptions for access and metering; subscription keys are not a replacement for user identity.
Versions represent breaking public contracts, while revisions support controlled nonbreaking configuration changes and staged release.
Quotas, rate limits, caching, routing, validation, and transformations protect dependencies but must preserve correct semantics and useful error contracts.
Classic, v2, Consumption, self-hosted, and workspace gateways differ in networking, scale, policy, cache, region, and operational responsibility.
Tier, units, zones, regions, backend failover, DNS, certificates, capacity, telemetry sampling, and configuration recovery determine end-to-end availability.
Architecture
Consumers call an APIM gateway that applies identity and traffic policies, routes to protected backend APIs, and exports telemetry while the management plane controls configuration.
Conceptual architecture · Adapt to your system context
Implementation walkthrough
Build the critical path deliberately, then verify its observable behavior.
- 01Inventory consumers and APIs
Record contracts, identities, traffic, sensitivity, owners, SLOs, regions, and dependencies.
- 02Choose topology and tier
Validate gateway features, capacity, networking, zones, regions, portal, and recovery requirements.
- 03Define API lifecycle
Import OpenAPI, create stable operation IDs, products, subscriptions, versions, revisions, and ownership.
- 04Secure both hops
Validate caller tokens, configure backend managed identity, restrict networking, and retain backend authorization.
- 05Compose policies
Add correlation, validation, limits, routing, transforms, caching, and safe errors in deliberate scopes.
- 06Automate and test
Deploy with Bicep or Terraform, test policy branches and backends, and promote a revision.
- 07Operate and recover
Load test, alert on capacity and failures, rotate certificates, export configuration, and rehearse recovery.
Code example
An inbound policy validates an Entra-issued JWT, limits calls per authenticated subject, and adds a correlation identifier before forwarding the request.
<policies>
<inbound>
<base />
<validate-jwt header-name="Authorization" failed-validation-httpcode="401">
<openid-config url="{{entra-openid-config}}" />
<audiences><audience>{{orders-api-audience}}</audience></audiences>
</validate-jwt>
<rate-limit-by-key calls="120" renewal-period="60"
counter-key="@(context.Principal?.Identity?.Name ?? context.Request.IpAddress)" />
<set-header name="x-correlation-id" exists-action="skip">
<value>@(context.RequestId.ToString())</value>
</set-header>
</inbound>
<backend><base /></backend>
<outbound><base /></outbound>
<on-error><base /></on-error>
</policies>Testing strategy
Test policies and backends independently, then prove their combined behavior on a deployed gateway.
- Unit test backend authorization and business rules.
- Validate policy XML, expressions, named values, inheritance, and forbidden secrets in CI.
- Run gateway tests for missing, invalid, expired, wrong-audience, wrong-scope, and valid tokens.
- Verify rate, quota, cache, transformation, timeout, retry, and on-error branches.
- Load test gateway capacity and backend saturation with representative payloads.
- Inject backend, DNS, certificate, regional, and telemetry failures and rehearse recovery.
Security considerations
Treat APIM as one trust boundary in a defense-in-depth path.
- Disable anonymous exposure unless deliberate.
- Validate tokens strictly and authorize resources in the backend.
- Use managed identity and least-privilege roles for backends and Key Vault.
- Restrict inbound and outbound network paths and validate certificates.
- Redact credentials, tokens, PII, and bodies from logs and traces.
- Protect management access with least privilege, change review, and audit.
Performance and scalability
Measure gateway and backend together because policy cost and dependency capacity determine useful throughput.
- Track capacity, request rate, percentiles, policy duration, backend duration, errors, and throttles.
- Use caching only for safe responses with correct vary keys and invalidation.
- Bound body transforms, validation size, retries, and timeouts.
- Scale before sustained saturation and retest after policy changes.
- Avoid expensive policy expressions and repeated remote calls.
- Model Consumption cost or provisioned units against realistic peaks.
Deployment and operations
Promote declarative configuration and immutable revisions through environments.
- Pin API versions and infrastructure API versions.
- Keep environment values separate from reusable API and policy definitions.
- Use federated deployment identity and narrowly scoped management permissions.
- Run policy and contract smoke tests before making a revision current.
- Roll back by switching revision or forward-fixing compatible configuration.
- Retarget DNS and backends only through tested recovery procedures.
Common problems and fixes
Requests return 401 or 403 unexpectedly
Likely cause: Issuer, audience, claim, token location, subscription, or backend permission differs.
Fix: Trace one safe request, inspect policy scope and JWT metadata, and separate gateway rejection from backend authorization.
Latency rises at the gateway
Likely cause: Capacity saturation, expensive policy work, remote validation, retries, or a slow backend.
Fix: Split gateway and backend duration, inspect capacity and policy traces, remove retry amplification, and load test the bottleneck.
Policy works at one scope but not another
Likely cause: Base inheritance is missing or a broader policy overrides behavior.
Fix: Inspect the effective policy, add deliberate base elements, and test global through operation scopes.
Private backend is unreachable
Likely cause: Tier capability, VNet integration, DNS, route, firewall, identity, or TLS trust is incomplete.
Fix: Verify the selected tier path end to end from gateway subnet and identity, including name resolution and certificate chain.
Configuration drift appears
Likely cause: Portal edits bypass the declarative delivery path.
Fix: Restrict manual changes, export and compare configuration, restore source control as authority, and redeploy.
Best practices
- Choose tier and gateway type only after mapping required policies, networking, zones, regions, scale, developer portal, and recovery capabilities.
- Keep policies small, scoped, source-controlled, reviewed, linted, tested, and promoted through immutable API revisions.
- Validate issuer, audience, signature, expiry, and required claims at the gateway, then enforce resource authorization again in the backend.
- Use APIM managed identity for Key Vault and backends; keep secrets in Key Vault-backed named values and never log them.
- Apply rate limits, quotas, payload limits, timeouts, retry, and circuit behavior from measured backend capacity and idempotency.
- Preserve correlation and distributed trace context while redacting sensitive headers and bodies from diagnostics.
- Automate OpenAPI import, named values, backends, policies, products, diagnostics, and tests with infrastructure and configuration as code.
- Monitor capacity, gateway and backend latency, 4xx and 5xx rates, policy errors, cache behavior, certificates, quotas, and regional health.
Production checklist
- Tier and gateway capabilities match policy, network, availability, region, and recovery needs.
- Caller and backend identities use strict validation, least privilege, and no embedded secrets.
- Backends retain domain authorization, validation, idempotency, and resilience.
- Policies are scoped, inherited, versioned, tested, bounded, and observable.
- Versions, revisions, products, subscriptions, owners, and deprecation are governed.
- Capacity, latency, errors, certificates, quotas, and regional health have alerts and runbooks.
- Infrastructure, configuration, smoke tests, rollback, and disaster recovery are rehearsed.
Common interview questions
Gateway versus management plane?
The gateway is the request data plane. The management plane defines APIs, products, subscriptions, policies, users, revisions, versions, diagnostics, and operational configuration.
Why must a backend authorize after APIM validates a JWT?
Gateway validation establishes an acceptable identity and coarse access, but only the backend understands resource ownership, tenant boundaries, and business permissions.
Version versus revision?
A version exposes a distinct contract, commonly for breaking changes. A revision stages a compatible implementation or policy change before making it current.
Rate limit versus quota?
A rate limit bounds calls in a short renewal window to protect capacity; a quota limits usage over a longer period for entitlement or cost control.
How do you make APIM highly available?
Select a supported production tier, distribute suitable capacity across zones or regions where supported, make backends and DNS resilient, automate configuration recovery, and test dependency failures.
When use a self-hosted gateway?
Use it when traffic must remain near on-premises or multicloud backends while management stays in Azure, accepting responsibility for its hosting, scale, connectivity, upgrades, and availability.
Dedicated GitHub sample
Clone, run, test, and modify the focused implementation created for this guide.
APIM Governed Orders
A .NET 10 orders API with APIM Bicep and a source-controlled policy for Entra JWT validation, rate limiting, correlation, bounded forwarding, and safe errors.
- Run
dotnet run --project src/ApimGovernedOrders- Test
dotnet test ApimGovernedOrders.slnx