# Authentication

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

> Personal API keys, the header they go in, scopes, rotation, expiry, and what 401 and 403 mean.

Every request except `GET /v1/openapi.json` needs a personal API key. A key belongs to one New June account and can only see that account's data.

## Create a key

Any New June account can create keys on the [Developer page](https://newjune.co/golf/developer) of the web portal. When you create one you choose:

- **A name**, 1 to 60 characters, so you can tell keys apart later (for example "Spreadsheet sync").
- **Scopes**, at least one. See [Scopes](#scopes).
- **An expiry date**, optional. Without one, the key works until you revoke it.

The full key is shown once, right after you create it. New June stores only a hash of it and cannot show it again. If you lose it, revoke it and create a new one.

You can have up to **5 active keys** at a time. Revoked and expired keys do not count.

## Send the key

Put the key in the `Authorization` header with the `Bearer` scheme:

```http
Authorization: Bearer nj_live_3f9a...
```

curl:

```bash
curl https://data.newjune.co/v1/me \
  -H "Authorization: Bearer $NEW_JUNE_API_KEY"
```

TypeScript:

```ts
const response = await fetch('https://data.newjune.co/v1/me', {
  headers: { Authorization: `Bearer ${process.env.NEW_JUNE_API_KEY}` },
});
console.log(await response.json());
```

Python:

```python
import os
import requests

response = requests.get(
    "https://data.newjune.co/v1/me",
    headers={"Authorization": f"Bearer {os.environ['NEW_JUNE_API_KEY']}"},
    timeout=30,
)
print(response.json())
```

The API looks for the key only in this header, never in query strings or cookies. Always use HTTPS.

## Key format

A key is `nj_live_` followed by 54 lowercase hexadecimal characters, 62 characters in all. The last 6 characters are a checksum, so the API rejects a mistyped key with `401 invalid_api_key` without looking it up. The first 12 characters (`nj_live_3f9a`) are the key's **prefix**: it appears on the Developer page and in `GET /v1/me` so you can tell keys apart, and it is safe to log.

## Scopes

A key can only call endpoints its scopes allow. Give each key the fewest scopes it needs.

| Scope | Grants | Endpoints |
| --- | --- | --- |
| `rounds:read` | Your rounds, hole-by-hole scores, and shots | [rounds](https://developers.newjune.co/api-reference/rounds), [hole_scores](https://developers.newjune.co/api-reference/hole-scores), [shots](https://developers.newjune.co/api-reference/shots) |
| `reports:read` | The report New June produces after each round | [reports](https://developers.newjune.co/api-reference/reports) |
| `plans:read` | Priorities, practice plans, their evaluations, and focus sets | [priorities](https://developers.newjune.co/api-reference/priorities), [plans](https://developers.newjune.co/api-reference/plans), [evaluations](https://developers.newjune.co/api-reference/evaluations), [focus_sets](https://developers.newjune.co/api-reference/focus-sets) |
| `clubs:read` | The clubs in your bag and their typical distances | [clubs](https://developers.newjune.co/api-reference/clubs) |
| any key | The key itself, its scopes and limits | [me](https://developers.newjune.co/api-reference/me) |

Calling an endpoint without its scope returns `403 insufficient_scope`. Scopes are fixed when the key is created. To change them, create a new key with the scopes you want and revoke the old one.

## 401 or 403

| Status | Meaning | Codes |
| --- | --- | --- |
| `401` | The API could not identify a usable key. Fix the key before retrying. | [`missing_api_key`](https://developers.newjune.co/errors#missing_api_key), [`invalid_api_key`](https://developers.newjune.co/errors#invalid_api_key), [`api_key_revoked`](https://developers.newjune.co/errors#api_key_revoked), [`api_key_expired`](https://developers.newjune.co/errors#api_key_expired) |
| `403` | The key is valid, but it may not do this. | [`insufficient_scope`](https://developers.newjune.co/errors#insufficient_scope), [`account_suspended`](https://developers.newjune.co/errors#account_suspended) |

Repeated requests with a missing or invalid key from one IP address are limited. After too many, the API answers `429 too_many_failed_attempts` for a short time, even for valid keys from that address. See [Rate limits](https://developers.newjune.co/rate-limits).

## Rotate a key

Rotate without downtime:

1. Create a new key with the same scopes.
2. Deploy it to wherever the old key is used.
3. Call `GET /v1/me` with the new key to confirm it works.
4. Revoke the old key on the Developer page.

A revoked key stops working right away and returns `401 api_key_revoked`. Revoking cannot be undone.

## Expiry

If you set an expiry date, the key returns `401 api_key_expired` from that moment on. `GET /v1/me` shows the date in `key.expires_at` (null when the key does not expire). Rotate before that date.

## Keep keys safe

- **Keep keys on servers.** The API sends no CORS headers, so browsers cannot call it, and a key in front-end code is a key anyone can copy.
- **Use environment variables or a secret manager.** Every sample in these docs takes the key from `NEW_JUNE_API_KEY` in the environment.
- **Never commit a key.** Add your `.env` file to `.gitignore`. If a key reaches a repository, a log, or a chat, revoke it.
- **Use one key per integration**, so you can revoke one without breaking the others.
- **Log the prefix, never the key.**
