# Errors

Source: https://developers.newjune.co/errors

> Every error has the same JSON shape, a stable code, and a link back to this page.

When a request fails, the API answers with an HTTP status from the 4xx or 5xx range and a JSON body with one `error` object. Branch on `error.code`, not on the message: codes are stable, messages may be reworded.

```json title="401 Unauthorized"
{
  "error": {
    "type": "authentication_error",
    "code": "invalid_api_key",
    "message": "The API key is not valid.",
    "param": null,
    "request_id": "req_6f1c2b8e3d4a4f5b9c6d7e8f",
    "doc_url": "https://developers.newjune.co/errors#invalid_api_key"
  }
}
```

| Field | Description |
| --- | --- |
| `type` | The broad category. See [error types](#error-types). |
| `code` | A stable, specific code. Every code is listed below. |
| `message` | A sentence for people. Safe to log, not meant for program logic. |
| `param` | The query parameter at fault, when there is one. Otherwise null. |
| `request_id` | The id of this request, also sent as the `Request-Id` header. Include it when you contact support. |
| `doc_url` | A link to this code's entry on this page. |

## Handling errors

Retry only what can succeed on retry: `429` after waiting for `Retry-After`, and `500` or `503` with exponential backoff. Every `400`, `401`, `403`, and `404` needs a change to the request or the key first.

curl:

```bash
curl -s "https://data.newjune.co/v1/rounds?limit=500" \
  -H "Authorization: Bearer $NEW_JUNE_API_KEY"
# {"error":{"type":"invalid_request_error","code":"invalid_query_parameter","param":"limit",...}}
```

TypeScript:

```ts
const response = await fetch('https://data.newjune.co/v1/rounds?limit=25', {
  headers: { Authorization: `Bearer ${process.env.NEW_JUNE_API_KEY}` },
});

if (!response.ok) {
  const { error } = await response.json();
  switch (error.type) {
    case 'rate_limit_error':
      // Wait for Retry-After seconds, then retry.
      break;
    case 'api_error':
      // Retry with exponential backoff.
      break;
    default:
      // Fix the request or the key; retrying will fail the same way.
      throw new Error(`${error.code}: ${error.message} (${error.request_id})`);
  }
}
```

Python:

```python
import os
import requests

response = requests.get(
    "https://data.newjune.co/v1/rounds",
    params={"limit": 25},
    headers={"Authorization": f"Bearer {os.environ['NEW_JUNE_API_KEY']}"},
    timeout=30,
)

if not response.ok:
    error = response.json()["error"]
    if error["type"] == "rate_limit_error":
        pass  # Wait for Retry-After seconds, then retry.
    elif error["type"] == "api_error":
        pass  # Retry with exponential backoff.
    else:
        raise RuntimeError(f"{error['code']}: {error['message']} ({error['request_id']})")
```

A few rules hold for every endpoint:

- An id that belongs to another account returns the same `404 resource_not_found` as an id that does not exist, so ids reveal nothing about other players.
- Unknown paths under `/v1` return `401` until you send a valid key, then `404 route_not_found`.
- Methods other than `GET`, `HEAD`, and `OPTIONS` return `405 method_not_allowed` with an `Allow` header.
- Internal details never appear in a response. A `500` carries only a `request_id`.

## Error types

| Type | Meaning |
| --- | --- |
| `invalid_request_error` | The request is wrong in a way you can fix before retrying. |
| `authentication_error` | The API key is missing or cannot be used. |
| `permission_error` | The key is valid but may not do this. |
| `not_found_error` | Nothing exists at this id or path for your account. |
| `rate_limit_error` | You sent too many requests. Wait, then retry. |
| `api_error` | Something failed on our side. Retry with backoff. |

## Error codes

Every code the API can return. Codes are stable: new ones may be added, existing ones are never renamed or reused.

### invalid_query_parameter

HTTP 400, type `invalid_request_error`.

**Message:** A query parameter has an invalid value.

**What to do:** Check `param` for the parameter name and fix its value. Timestamps need a Z or an offset; ids are UUIDs; `limit` is 1 to 100.

### unknown_query_parameter

HTTP 400, type `invalid_request_error`.

**Message:** This endpoint does not accept that query parameter.

**What to do:** Remove the parameter or fix its spelling. Each endpoint accepts only the parameters listed on its reference page.

### invalid_cursor

HTTP 400, type `invalid_request_error`.

**Message:** The cursor is not valid. Use the next_cursor value from a previous response.

**What to do:** Pass `next_cursor` from the previous page exactly as returned, with the same filters. Do not build or edit cursors.

### method_not_allowed

HTTP 405, type `invalid_request_error`.

**Message:** This API is read only. Use GET.

**What to do:** Use GET. The API is read only.

### missing_api_key

HTTP 401, type `authentication_error`.

**Message:** No API key was provided. Send it as "Authorization: Bearer &lt;key&gt;".

**What to do:** Send the key in the `Authorization` header as `Bearer &lt;key&gt;`.

### invalid_api_key

HTTP 401, type `authentication_error`.

**Message:** The API key is not valid.

**What to do:** Check that you copied the whole key, including the `nj_live_` prefix. If it is lost, create a new key.

### api_key_revoked

HTTP 401, type `authentication_error`.

**Message:** This API key has been revoked. Create a new key on the Developer page at newjune.co/golf/developer.

**What to do:** Create a new key on the Developer page and update your secret store.

### api_key_expired

HTTP 401, type `authentication_error`.

**Message:** This API key has expired. Create a new key on the Developer page at newjune.co/golf/developer.

**What to do:** Create a new key on the Developer page and update your secret store.

### account_suspended

HTTP 403, type `permission_error`.

**Message:** API access is suspended for this account.

**What to do:** Contact New June support. Retrying will not help.

### insufficient_scope

HTTP 403, type `permission_error`.

**Message:** This API key does not have the scope this endpoint needs.

**What to do:** Create a key that includes the scope listed on the endpoint’s reference page. Scopes cannot be added to an existing key.

### resource_not_found

HTTP 404, type `not_found_error`.

**Message:** No such resource.

**What to do:** Check the id. An id that belongs to another account returns this same error, by design.

### no_current_focus_set

HTTP 404, type `not_found_error`.

**Message:** There is no current focus set for this account.

**What to do:** There is no focus set in use right now. List focus sets to see past ones.

### route_not_found

HTTP 404, type `not_found_error`.

**Message:** No such endpoint.

**What to do:** Check the path against the API reference. Paths start with `/v1/`.

### rate_limit_exceeded

HTTP 429, type `rate_limit_error`.

**Message:** Too many requests. Wait until the time in Retry-After, then try again.

**What to do:** Wait for the number of seconds in `Retry-After`, then retry. Spread requests out to stay under your per-minute limit.

### daily_limit_exceeded

HTTP 429, type `rate_limit_error`.

**Message:** This account has reached its daily request limit. It resets at midnight UTC.

**What to do:** Stop until midnight UTC, when the daily count resets. `Retry-After` gives the seconds until then.

### too_many_failed_attempts

HTTP 429, type `rate_limit_error`.

**Message:** Too many requests with a missing or invalid API key. Try again shortly.

**What to do:** Fix the key you are sending, then wait for `Retry-After` before trying again.

### internal_error

HTTP 500, type `api_error`.

**Message:** Something went wrong on our side. Try again later.

**What to do:** Retry with exponential backoff. If it persists, contact support with the `request_id`.

### service_unavailable

HTTP 503, type `api_error`.

**Message:** The API is temporarily unavailable. Try again later.

**What to do:** Retry with exponential backoff. The API is down or paused for a short time.
