# Rate limits

Source: https://developers.newjune.co/rate-limits

> Three limits, the headers that report them, and how to back off on 429.

## The three limits

| Limit | Default | Applies to | Over the limit |
| --- | --- | --- | --- |
| Per key | 60 requests per minute (sliding window) | Each key on its own | `429` [`rate_limit_exceeded`](https://developers.newjune.co/errors#rate_limit_exceeded) |
| Per account | 1,000 requests per UTC day, all keys together | Your account | `429` [`daily_limit_exceeded`](https://developers.newjune.co/errors#daily_limit_exceeded) |
| Failed authentication | 20 per minute per IP address | Requests with a missing or invalid key | `429` [`too_many_failed_attempts`](https://developers.newjune.co/errors#too_many_failed_attempts) |

The daily count resets at **midnight UTC**. Limits can differ per key or account; `GET /v1/me` returns the ones that apply to your key in `limits.requests_per_minute` and `limits.requests_per_day`. Every request counts, including requests that end in an error.

## Headers

Successful authenticated responses report the per-key budget:

| Header | Meaning |
| --- | --- |
| `RateLimit-Limit` | Requests allowed per minute for this key. |
| `RateLimit-Remaining` | Requests left in the current window. |
| `RateLimit-Reset` | Seconds until the window frees up. |

A `429` carries the same three headers for the limit it hit, plus `Retry-After`: the number of seconds to wait before trying again. For `daily_limit_exceeded`, that is the time until midnight UTC.

```http
HTTP/1.1 429 Too Many Requests
RateLimit-Limit: 60
RateLimit-Remaining: 0
RateLimit-Reset: 17
Retry-After: 17
```

## Handle 429

Wait for `Retry-After`, then retry the same request. If the header is missing, back off exponentially. Stop after a few attempts instead of retrying forever.

curl:

```bash
# curl retries 429 and most 5xx responses on its own and honors Retry-After.
curl --retry 5 --retry-max-time 120 "https://data.newjune.co/v1/rounds?limit=100" \
  -H "Authorization: Bearer $NEW_JUNE_API_KEY"
```

TypeScript:

```ts
// Node 20+. Retries 429 and 5xx; waits for Retry-After when present.
async function getWithRetry(url: string, attempts = 5): Promise<unknown> {
  for (let attempt = 0; ; attempt += 1) {
    const response = await fetch(url, {
      headers: { Authorization: `Bearer ${process.env.NEW_JUNE_API_KEY}` },
    });
    const retryable = response.status === 429 || response.status >= 500;
    if (!retryable || attempt === attempts - 1) {
      if (!response.ok) throw new Error(`${response.status}: ${await response.text()}`);
      return response.json();
    }
    const retryAfter = Number(response.headers.get('Retry-After'));
    const waitSeconds = Number.isFinite(retryAfter) && retryAfter > 0 ? retryAfter : 2 ** attempt;
    await new Promise(resolve => setTimeout(resolve, waitSeconds * 1000));
  }
}

console.log(await getWithRetry('https://data.newjune.co/v1/rounds?limit=100'));
```

Python:

```python
import os
import time
import requests

session = requests.Session()
session.headers["Authorization"] = f"Bearer {os.environ['NEW_JUNE_API_KEY']}"

def get_with_retry(url, params=None, attempts=5):
    """GET that retries 429 and 5xx, waiting for Retry-After when present."""
    for attempt in range(attempts):
        response = session.get(url, params=params, timeout=30)
        retryable = response.status_code == 429 or response.status_code >= 500
        if not retryable or attempt == attempts - 1:
            response.raise_for_status()
            return response.json()
        retry_after = response.headers.get("Retry-After", "")
        time.sleep(int(retry_after) if retry_after.isdigit() else 2 ** attempt)

print(get_with_retry("https://data.newjune.co/v1/rounds", params={"limit": 100}))
```

## Stay under the limits

- **Use `limit=100`** for bulk work. It costs the same as a page of 25.
- **Sync incrementally.** Fetch only rounds that started since your last run, then refresh recent ones. See [Sync to your own database](https://developers.newjune.co/guides/incremental-sync).
- **Pace long walks.** A full export of every round's hole scores and shots can take a few hundred requests. Watch `RateLimit-Remaining` and pause when it gets low.
- **Do not poll faster than you need.** Rounds change only while you play and shortly after. Polling every 15 minutes is plenty for most integrations.
- **Fix bad keys first.** Failed authentication is limited per IP address, and once it trips, requests from that address are refused for a short time even with a good key.
