Guides Get started

Handle limits and errors

Stay inside your rate limit and react to errors the right way.

3 steps4 min

  1. Watch the headers

    Every response tells you where you stand. X-RateLimit-Reset is the number of seconds until your per-minute allowance refills.

    Headers
    X-RateLimit-Limit: 120
    X-RateLimit-Remaining: 7
    X-RateLimit-Reset: 23
    X-Quota-Remaining: 48210
  2. Back off on 429

    A 429 means you hit the per-minute rate or your monthly quota. Wait for Retry-After seconds. For a used-up monthly quota that is days, so stop and alert rather than sleep. The SDKs retry short waits for you and raise RateLimitError for long ones.

    Python
    from trendwatching import RateLimitError
    
    try:
        rows = tw.innovations.list(query="ai")
    except RateLimitError as e:
        alert(f"TrendWatching quota used up, retry in {e.retry_after} seconds")
  3. Branch on error codes

    Errors share one shape: {"error": "code", "detail": "..."}. Codes are stable, so branch on error: missing_api_key and invalid_api_key (401), subscription_inactive (403), not_found (404), invalid_tag, invalid_cursor, invalid_date and missing_parameter (400), rate_limit_exceeded and quota_exceeded (429).