# Rate limits

Turn on Okta's per-endpoint rate limits, so a request past its bucket's limit returns Okta's 429 E0000047 error.

Check that your client reads Okta's limit headers and waits out a 429 before it retries. Every response the limits cover includes Okta's `X-Rate-Limit-Limit`, `X-Rate-Limit-Remaining` and `X-Rate-Limit-Reset` headers. With no setting nothing is refused, so 53 reads of the applications list in one minute all return 200:

```bash
apps="$RYSTIC_URL/api/v1/apps"
curl -s -H 'Authorization: SSWS placeholder' -w '%{http_code}\n' $(for i in $(seq 53); do echo "-o /dev/null $apps"; done) | sort | uniq -c | awk '{print $2, $1}'
# -> 200 53
curl -s -D - -o /dev/null -H 'Authorization: SSWS placeholder' "$apps" | tr -d '\r' | grep -i '^x-rate-limit-limit'
# -> X-Rate-Limit-Limit: 50
```

[`rate_limits: real`](https://www.rystic.ai/docs/network-profiles.md) enforces the buckets that [Rate limits](https://www.rystic.ai/docs/simulators/okta.md#rate-limits) lists. A bucket's minute starts on the whole second of its first request. Here the limits go on, a reset empties every bucket, one connection sends 53 reads of the applications list, and a read of the users list draws on its own bucket:

```bash
apps="$RYSTIC_URL/api/v1/apps"
curl -s -X POST "$RYSTIC_URL/_rystic/network" -o /dev/null -w '%{http_code}\n' -d '{"rate_limits": "real"}'
# -> 204
curl -s -X POST "$RYSTIC_URL/_rystic/reset" -o /dev/null -w '%{http_code}\n'
# -> 204
curl -s -H 'Authorization: SSWS placeholder' -w '%{http_code}\n' $(for i in $(seq 53); do echo "-o /dev/null $apps"; done) | sort | uniq -c | awk '{print $2, $1}'
# -> 200 50
# -> 429 3
curl -s -D - -o /dev/null -H 'Authorization: SSWS placeholder' "$apps" | tr -d '\r' | grep -i '^x-rate-limit-remaining\|^retry-after'
# -> X-Rate-Limit-Remaining: 0
curl -s -H 'Authorization: SSWS placeholder' "$apps" | jq -r .errorCode
# -> E0000047
curl -s -D - -o /dev/null -H 'Authorization: SSWS placeholder' "$apps" | tr -d '\r' | awk -F': ' -v now="$(date +%s)" 'tolower($1) == "x-rate-limit-reset" && $2 >= now && $2 - now <= 60 {print "resets within a minute"}'
# -> resets within a minute
curl -s -o /dev/null -w '%{http_code}\n' -H 'Authorization: SSWS placeholder' "$RYSTIC_URL/api/v1/users?limit=1"
# -> 200
curl -s -D - -o /dev/null "$RYSTIC_URL/.well-known/okta-organization" | tr -d '\r' | awk 'NR == 1 {print $2} tolower($0) ~ /^x-rate-limit-/ {n++} END {print n + 0, "rate-limit headers"}'
# -> 200
# -> 0 rate-limit headers
curl -s -X POST "$RYSTIC_URL/_rystic/reset" -o /dev/null -w '%{http_code}\n'
# -> 204
curl -s "$RYSTIC_URL/_rystic/network" | jq -r .rate_limits
# -> real
curl -s -D - -o /dev/null -H 'Authorization: SSWS placeholder' "$apps" | tr -d '\r' | grep -i '^x-rate-limit-remaining'
# -> X-Rate-Limit-Remaining: 49
```

The three refused reads got Okta's 429 body with `errorCode` `E0000047` and no `Retry-After`; the time to wait for is `X-Rate-Limit-Reset`, the end of the bucket's minute in Unix seconds. The users read succeeded because the applications bucket being empty leaves the users bucket untouched. The org metadata read is in no bucket, so it succeeded with no limit headers at all. After the second reset, `rate_limits` still reads `real` and the applications bucket is full again.

---

Source: https://www.rystic.ai/docs/simulators/okta/features/rate-limits · Markdown: https://www.rystic.ai/docs/simulators/okta/features/rate-limits.md
