> ## 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 Quota Application - Fixed Time Intervals

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

The Quota policy limits the total number of requests that can be made in a certain time period. This policy is generally used to provide longer-term usage control (**for example**, **giving a specific user permission for 1000 requests within one day or one month**).

In the scenario, we will limit incoming requests to 100 requests in a 1-day time period.

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

<img src="https://mintcdn.com/apinizer/LkpVQ4wpcX8FupXk/images/tutorials/mock-api-olusturma.png?fit=max&auto=format&n=LkpVQ4wpcX8FupXk&q=85&s=68ea06e4f63776cbdeb2696402f43475" width="1000" data-path="images/tutorials/mock-api-olusturma.png" />

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

<img src="https://mintcdn.com/apinizer/eoMNV6dY9Oprmz1f/images/tutorials/api-based-quota-politika-ekleme.png?fit=max&auto=format&n=eoMNV6dY9Oprmz1f&q=85&s=efc0ee8d9188301401f73fc236cfc150" width="1000" data-path="images/tutorials/api-based-quota-politika-ekleme.png" />

In the scenario, let's limit incoming requests to **100 requests for 1 day** using the **Fixed Window** (Fixed Window) method.

* If the period is set to 1 and day is selected:
* For example, if the policy was saved at 12:00:00, this policy will be valid between **00:00:00** and **23:59:59** within the current day.
* At **00:00:00** the next day, a new window starts and the counter resets.

<img src="https://mintcdn.com/apinizer/AsiSk-hPeQ0V1lqb/images/tutorials/fixed-window-ayarlari.png?fit=max&auto=format&n=AsiSk-hPeQ0V1lqb&q=85&s=e67745ff41c0b392af706248bfb0cde8" width="1000" data-path="images/tutorials/fixed-window-ayarlari.png" />

| Field                             | Description                                                                                                                                                                        |
| --------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Permitted Message Count           | The number of requests allowed. We set this to 100 in this scenario.                                                                                                               |
| Interval Time Amount              | The period determined according to the selected time unit. We set this to 1 in this scenario.                                                                                      |
| Interval Time Unit                | Time unit. We set this to day in this scenario.                                                                                                                                    |
| Target-Specific Quota Rules       | Special quota limits are defined for each target.                                                                                                                                  |
| Interval Window Type              | Method used to apply the time interval. 1-day 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 100th request within the time range between 00:00:00 and 23:59:59 on the current day.

<img src="https://mintcdn.com/apinizer/U-KT1-UboZ-b9Uet/images/tutorials/test-konsolu-quota-siniri.png?fit=max&auto=format&n=U-KT1-UboZ-b9Uet&q=85&s=2887698b575aba5c90a2e384ce95035a" width="1000" data-path="images/tutorials/test-konsolu-quota-siniri.png" />

## Target-Specific Quota Rules (Target-Specific Quota Rules)

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

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

<img src="https://mintcdn.com/apinizer/eoMNV6dY9Oprmz1f/images/tutorials/api-based-quota-politika-ekleme.png?fit=max&auto=format&n=eoMNV6dY9Oprmz1f&q=85&s=efc0ee8d9188301401f73fc236cfc150" width="1000" data-path="images/tutorials/api-based-quota-politika-ekleme.png" />

* Using Target-Specific Quota Rules (Target-Specific Quota 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 1 day** using the **Fixed Window** (Fixed Window) method in the general policy, a limit of **maximum 5 requests per hour** can be defined for the **user1** user at the same time.
* In the Fixed Window method, if the period is set to 1 and day is selected:
* For example, if the policy was saved at 12:00:00, this policy will be valid between **00:00:00** and **23:59:59** within the current day, and 100 requests can be made as a general rule during this period.
* At **00:00:00** the next day, a new window starts and the counter resets.
* For the **user1** user we defined with Target-Specific Quota Rules, an hourly limit has been determined. If an hour period starts at **00:00:00**, this covers the period between **00:00:00** and **00:59:59**, and the **user1** user can make a maximum of 5 requests in this time range. In the next hour period (01:00:00), the counter resets.

<img src="https://mintcdn.com/apinizer/U-KT1-UboZ-b9Uet/images/tutorials/target-specific-quota-rules-ayarlari.png?fit=max&auto=format&n=U-KT1-UboZ-b9Uet&q=85&s=d7657c6a6b662f5c9afe16992c90c4d0" width="1000" data-path="images/tutorials/target-specific-quota-rules-ayarlari.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 **00:00:00** and **00:59:59** at the current hour.

<img src="https://mintcdn.com/apinizer/U-KT1-UboZ-b9Uet/images/tutorials/test-konsolu-user1-quota-siniri.png?fit=max&auto=format&n=U-KT1-UboZ-b9Uet&q=85&s=2cb921925bfc12edf49e4ea58c672bf5" width="1000" data-path="images/tutorials/test-konsolu-user1-quota-siniri.png" />
