⚡ Example: Rate Limiting in Azure API Management

 

Scenario:

You have a public API and want to limit each user to 5 calls per minute to prevent abuse or accidental overload.


πŸ›‘️ What is Rate Limiting?

Rate limiting controls how many requests a client can make to your API within a specific time window.

It helps:

  • Prevent API abuse

  • Protect backend systems

  • Fairly distribute API usage


πŸ’‘ Using the rate-limit-by-key Policy

Azure APIM uses a policy called rate-limit-by-key. It limits requests based on a unique identifier — usually the caller's subscription key or IP address.


πŸ”§ Example Policy (XML)

Here’s how you can add this to your inbound policy:


<inbound> <base /> <rate-limit-by-key calls="5" renewal-period="60" counter-key="@(context.Subscription?.Key)" /> </inbound>

Explanation:

AttributeMeaning
calls="5"Maximum 5 calls allowed
renewal-period="60"Time window of 60 seconds (1 minute)
counter-keyUniquely tracks by subscription key (per user limit)

πŸš€ Where to Apply This

You can apply this policy at:

  • Product level (to all APIs in that product)

  • API level (to a specific API)

  • Operation level (for a specific endpoint)


⚠️ What Happens When the Limit is Hit?

When a caller exceeds the limit, APIM responds with:

  • HTTP 429 (Too Many Requests)

  • Optionally, include a custom error message or retry-after header

Comments

Popular posts from this blog

πŸ€– Copilot vs Microsoft Copilot vs Copilot Studio: What’s the Difference?

Automating Unique Number Generation in Dynamics 365 Using Plugins

In-Process vs Isolated Process Azure Functions: What’s the Difference?