# idpc doctor

Check the environment, the credentials in use and whether the API key works. `doctor` is the first command to run when a key fails. It spends no lookup.

## Usage

```text
idpc doctor
```

No flags beyond the global ones. It checks the key that `--api-key`, `IDPC_API_KEY` or the credentials file resolves to.

## Output

```json
{
  "cli": { "name": "@ideal-postcodes/cli", "version": "1.3.1" },
  "runtime": { "node": "v24.13.1", "platform": "linux", "arch": "x64" },
  "config": { "path": "/home/me/.config/ideal-postcodes/credentials.json" },
  "credentials": { "api_key": { "source": "env" }, "user_token": null },
  "connectivity": { "reachable": true },
  "key": { "available": true, "diagnosis": [], "warnings": [] }
}
```

| Field                                           | Meaning                                                                                                                                                                               |
| ----------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `credentials.api_key`, `credentials.user_token` | Where each credential came from: `flag`, `env` or `config`. `null` when not set                                                                                                       |
| `connectivity.reachable`                        | `true` when the API answered, `false` when the request failed in transit (`connectivity.error` says why), `null` when no API key is set and nothing was checked                       |
| `key.available`                                 | `true` when the key can make lookups, `false` when the API refused it, `null` when the key was not checked: the API was not reached, or it answered with a rate limit or server error |
| `key.lookups_remaining`                         | The key's balance. Present only with a user token                                                                                                                                     |
| `key.diagnosis`                                 | Why the key is unusable, one sentence per cause                                                                                                                                       |
| `key.warnings`                                  | Problems that do not stop the key working, such as a low balance or no user token                                                                                                     |

## Exit code

`doctor` exits `1` when the API is unreachable, the key is unusable or the key check did not complete, and `0` otherwise. The report prints either way, so read `key.diagnosis` before the exit code.

## Diagnoses

| `key.diagnosis` starts with                    | Cause                                                                                                                                                                                     | Fix                                                                                                                        |
| ---------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
| `Key not found`                                | The API has no key with this value                                                                                                                                                        | Check the key against <https://ideal-postcodes.co.uk/account/> and which source supplied it (`credentials.api_key.source`) |
| `No lookups remaining`                         | The balance is 0                                                                                                                                                                          | Top up the key                                                                                                             |
| `Daily limit reached`, `Monthly limit reached` | The key hit a limit set on it                                                                                                                                                             | Wait for the limit to reset, or raise it with `idpc keys update`                                                           |
| `Allowed URLs are set`                         | The key is restricted to named URLs. The CLI sends no `Origin`, so the key reports unusable here and still works from those URLs                                                          | Use an unrestricted key for the CLI                                                                                        |
| `Key check failed [<code>]`                    | The API answered the check with another error. `rate_limited` and `server_error` leave `key.available` as `null`, since they say nothing about the key; any other code sets it to `false` | Retry after a rate limit or server error. Otherwise act on the error code                                                  |
| `Key reports unusable`                         | The key is unusable and its balance, limits and allowed URLs do not explain it                                                                                                            | Contact support                                                                                                            |
| `Could not read key details`                   | The user token was rejected                                                                                                                                                               | Check `IDPC_USER_TOKEN`                                                                                                    |

Balance and limit diagnoses need a user token. Without one, `doctor` reports only whether the key is usable and adds a warning.

## A failing key

```bash
idpc doctor | jq '{reachable: .connectivity.reachable, key: .key}'
```

* `reachable: false`: a network fault between you and the API. The key was not checked.
* `available: false`: the API answered and refused the key. `diagnosis` holds the cause.
* `reachable: true` and `available: null`: the API was rate limiting or failing. Run `doctor` again before judging the key.
* `available: true` and a command still fails: the key works, so read that command's error code. [Error codes](https://docs.ideal-postcodes.co.uk/docs/cli#error-codes) lists them.
