> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apinizer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# API Based Throttling Application - Fixed Time Intervals

In this scenario, the application of the **API Based Throttling** policy to a Mock API created on Apinizer will be tested.

Using **throttling** for services is a method of limiting the number of requests by preventing overload.

In the scenario, we can ensure that only 10 requests are accepted every 5 minutes by dividing incoming requests into equal 5-minute time intervals.

Let's create a Mock API example and add the policy.

<img src="https://mintcdn.com/apinizer/Ttu-s3OL9b1z3iwB/images/tutorials/api-based1.png?fit=max&auto=format&n=Ttu-s3OL9b1z3iwB&q=85&s=0cddc738aa468ba36c91e18b19f4c221" alt="Generate JWK Screen" width="1000" data-path="images/tutorials/api-based1.png" />

We add **API Based Throttling** as a policy.

<img src="https://mintcdn.com/apinizer/abuBX6Yq8rI5YqRN/images/tutorials/api-based2.png?fit=max&auto=format&n=abuBX6Yq8rI5YqRN&q=85&s=e1fff763dea7c294839f4091b0ce9b36" alt="Generate JWK Screen" width="1000" data-path="images/tutorials/api-based2.png" />

In the scenario, let's limit incoming requests to **10 requests in 5 minutes** using the **Fixed Window** (Fixed Window) method.

* If the period is set to 5 minutes and for example the policy was saved at **12:00:00**:
* Requests between **12:00:00** and **12:04:59** are counted in the same window.
* At **12:05:00**, a new window starts and the counter resets.

<img src="https://mintcdn.com/apinizer/abuBX6Yq8rI5YqRN/images/tutorials/api-based3.png?fit=max&auto=format&n=abuBX6Yq8rI5YqRN&q=85&s=60169bba61322238bbac92ec8f4fb6b2" alt="Generate JWK Screen" width="1000" data-path="images/tutorials/api-based3.png" />

| Field                                         | Description                                                                                                                                                                        |
| --------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Show Rate Limit Statistics in Response Header | If activated, shows rate limit statistics in the response header.                                                                                                                  |
| Permitted Message Count                       | The number of requests allowed. We set this to 10 in this scenario.                                                                                                                |
| Interval Time Amount                          | The period determined according to the selected time unit. We set this to 5 in this scenario.                                                                                      |
| Interval Time Unit                            | Time unit. We set this to minute in this scenario.                                                                                                                                 |
| Target-Specific Throttling Rules              | Special throttling limits are defined for each target.                                                                                                                             |
| Interval Window Type                          | Method used to apply the time interval. 5-minute time periods in Fixed type.                                                                                                       |
| Cache Connection Timeout (Second)             | Specify the connection timeout value for the connection between the policy and cache.                                                                                              |
| Action for Cache Connection Error             | If there is a connection problem with cache, it is determined whether to return an error (Fail is selected) or continue without applying the request limit (Continue is selected). |

When we send a sample request from the test console, the message was blocked because the restriction limit was reached after the 10th request within the time range between 14:00:00 and 14:04:59 at the current time.

<img src="https://mintcdn.com/apinizer/abuBX6Yq8rI5YqRN/images/tutorials/api-based4.png?fit=max&auto=format&n=abuBX6Yq8rI5YqRN&q=85&s=c1e37957baa968c27281779ffefb340d" alt="Generate JWK Screen" width="1000" data-path="images/tutorials/api-based4.png" />

## Target-Specific Throttling Rules (Target-Specific Throttling Rules)

Thanks to this feature, you can define special throttling limitations for specific users or targets.

We add **API Based Throttling** as a policy.

<img src="https://mintcdn.com/apinizer/abuBX6Yq8rI5YqRN/images/tutorials/api-based5.png?fit=max&auto=format&n=abuBX6Yq8rI5YqRN&q=85&s=830b329c1533f3413181109272cf2003" alt="Generate JWK Screen" width="1000" data-path="images/tutorials/api-based5.png" />

* Using Target-Specific Throttling Rules (Target-Specific Throttling Rules), special quota limits can be determined based on users (based on header, body, or another field). While limiting incoming requests to **100 requests for 5 minutes** using the **Fixed Window** (Fixed Window) method in the general policy, a limit of **maximum 5 requests in 5 minutes** can be defined for the **user1** user at the same time.
* In the Fixed Window method, if the period is set to 5 and minute is selected:
* For example, if the policy was saved at 12:00:00, this policy will be valid between **12:00:00** and **12:04:59** within the current day, and 100 requests can be made as a general rule during this period.
* For the **user1** user we defined with Target-Specific Quota Rules, a minute-based limit has been determined. If a 5-minute period starts at **12:00:00**, this covers the period between **12:00:00** and **12:04:59**, and the **user1** user can make a maximum of 5 requests in this time range. In the next 5-minute period (12:05:00), the counter resets.
* In the next 5-minute period (for example between **12:05:00** and **12:09:59**), while a new 100 request right is defined for the general policy, 5 request rights will be redefined for the **user1** user as well.

<img src="https://mintcdn.com/apinizer/abuBX6Yq8rI5YqRN/images/tutorials/api-based6.png?fit=max&auto=format&n=abuBX6Yq8rI5YqRN&q=85&s=0bf0054f6a794aeb14d5ed8c670fde5d" alt="Generate JWK Screen" width="1000" data-path="images/tutorials/api-based6.png" />

When we send a sample request from the test console, when we send a request with the user1 user from the username Header, the message was blocked because the restriction limit was reached after the 5th request within the time range between **12:00:00** and **12:04:59** at the current hour.

<img src="https://mintcdn.com/apinizer/abuBX6Yq8rI5YqRN/images/tutorials/api-based7.png?fit=max&auto=format&n=abuBX6Yq8rI5YqRN&q=85&s=4c9df5587d259257da7f525dfc4d044f" alt="Generate JWK Screen" width="1000" data-path="images/tutorials/api-based7.png" />
