Good afternoon.
I am trying to rate limit a specific API following the steps below:
- Create an API.
Api Definition
{
"name": "Config-Api-Products",
"api_id": "Config-Api-Products",
"org_id": "1",
"auth_configs": {
"authToken": {
"auth_header_name": "X-Api-Key"
}
},
"definition": {
"location": "header",
"key": "X-Api-Version"
},
"version_data": {
"not_versioned": true,
"versions": {
"Default": {
"name": "Default",
"use_extended_paths": true
}
}
},
"proxy": {
"listen_path": "/product/list",
"target_url": "https://my-server/products",
"strip_listen_path": true
},
"active": true
}
- Create a Key with access rights to this API. In this access token, I don’t set any Rate Limits, since I want to retrieve it from the policy instead:
Sample API Gateway call
curl --location --request POST 'localhost:7070/tyk/keys/create' \
--header 'X-Tyk-Authorization: my_tyk_token' \
--header 'Content-Type: application/json' \
--data-raw '{
"org_id": "1",
"access_rights": {
"Config-Api-Products": {
"api_id": "Config-Api-Products",
"api_name": "Config-Api-Products",
"versions": ["Default"]
}
}
}'
Sample API Gateway Response:
{
"key": "generated_tyk_token_key",
"status": "ok",
"action": "added",
"key_hash": "e042e6ef"
}
- Create a policy. For that I am mapping the
policies directory
on tyk using compose file.
docker-compose.yml
version: '3.3'
services:
tyk-gateway:
image: docker.tyk.io/tyk-gateway/tyk-gateway:v3.2.1
ports:
- 7070:7070
networks:
- tyk
volumes:
- $PWD/tyk.standalone.conf:/opt/tyk-gateway/tyk.conf
- $PWD/apps:/opt/tyk-gateway/apps
- $PWD/middleware:/opt/tyk-gateway/middleware
- $PWD/certs:/opt/tyk-gateway/certs
- $PWD/policies:/opt/tyk-gateway/policies
depends_on:
- tyk-redis
tyk-redis:
image: redis:latest
command: redis-server --appendonly yes
networks:
- tyk
ports:
- 7171:6379
volumes:
- $PWD/redis-data:/data
networks:
tyk:
policies.json
{
"Config-Api-Products-Policy": {
"org_id": 1,
"rate": 1,
"per": 5,
"active": true,
"name": "Config-Api-Products-Policy",
"quota_max": 30,
"quota_renewal_rate": 60,
"state": "active",
"access_rights": {
"Config-Api-Products": {
"api_id": "Config-Api-Products",
"api_name": "Config-Api-Products",
"versions": ["Default"]
}
}
}
}
File base policy management enablement
"policies": {
"policy_source": "file",
"policy_record_name": "/opt/tyk-gateway/policies/policies.json"
}
Call to API that should be rate-limited
curl --location --request GET 'localhost:7070/product/list' \
--header 'X-Api-Key: generated_tyk_token_key' \
--header 'Authorization: Bearer my_bearer_backend_token'
With that configuration, I was expecting that the API would not be able to be called once for every 5 seconds, but I can do multiple API calls and Tyk doesn’t block it. Also, I called the API much more than 30 times in 60 seconds and Tyk is not blocking those calls either.
What am I missing here? Can anyone please help me with that?
Thank you.