Reliability › Rate limits and errors
Rate limits and errors
Every call can say no. This waits out a rate limit and tries again, and reads the named error for everything else: no key, a plan that does not reach it, a bad parameter.
# Rate limits and errors: httr2 waits out a 429 itself; errors carry the named code
# needs: install.packages("httr2")
library(httr2)
body <- request("https://api.apihorseracing.com/v1/results/latest") |>
req_url_query(limit = 5) |>
req_headers(`X-API-Key` = Sys.getenv("AHR_KEY")) |>
req_retry(max_tries = 5) |>
req_error(body = function(resp) {
e <- resp_body_json(resp)$error
paste(e$code, e$message, sep = ": ")
}) |>
req_perform() |>
resp_body_json()
str(body$data)
What the reply holds
- 401 no_key
- The key is missing or mistyped.
- 403 plan_needed
- Your plan does not reach this; the error names the plan that does.
- 422 invalid_param
- A parameter is wrong; the error says which.
- 429 rate_limited
- Too fast: wait the seconds in Retry-After.
When it says no
401no_key The key is missing or mistyped. Every sample reads it from AHR_KEY.403plan_needed Your plan does not reach this. The reply names the plan that does.429rate_limited Too fast. Wait the seconds in Retry-After, then try again.