# Authentication

> Authenticate with Jump EHR API using API keys. Learn about scopes, tokens, and secure authentication practices.

Source: https://docs.usejump.co.uk/getting-started/authentication

The Jump EHR API uses API keys to authenticate requests. You can create and manage your API keys in the Jump EHR dashboard.

## API Keys

All API requests require a valid API key passed in the `Authorization` header:

```bash
Authorization: Bearer pk_live_your_api_key
```

### Key Types

Jump EHR provides two types of API keys:

| Type     | Prefix     | Purpose                                                                                                                   |
| -------- | ---------- | ------------------------------------------------------------------------------------------------------------------------- |
| **Test** | `pk_test_` | Works only against your practice's sandbox: a copy of its setup with fictional patients. Created from inside the sandbox. |
| **Live** | `pk_live_` | Works only against your practice. Accesses real patient data.                                                             |

The key's mode is set by where you create it, and a key used in the wrong place is refused: a test key against a practice with `403 TEST_KEY_OUTSIDE_SANDBOX`, a live key against a sandbox with `403 LIVE_KEY_IN_SANDBOX`. See [Testing your integration](https://docs.usejump.co.uk/getting-started/testing).

<Callout type="warning">
  Live API keys provide access to real patient data. Only use them in secure server-side environments.
</Callout>

### Creating API Keys

1. Log in to your Jump EHR dashboard
2. Navigate to **Settings** > **API Keys**
3. Click **Create API Key**
4. Enter a descriptive name (e.g., "Production Backend", "Development")
5. Select the mode (Test or Live)
6. Choose the required scopes
7. Click **Create**

Your API key will only be displayed once. Copy it immediately and store it securely.

### Revoking Keys

You can revoke an API key at any time from the dashboard. Revoked keys will immediately stop working for all API requests.

## Authentication Examples

```bash
curl -X GET "https://api.usejump.co.uk/v1/patients" \
  -H "Authorization: Bearer pk_live_your_api_key"
```

### JavaScript

```javascript
const response = await fetch(
  'https://api.usejump.co.uk/v1/patients',
  {
    headers: {
      'Authorization': 'Bearer pk_live_your_api_key',
      'Content-Type': 'application/json'
    }
  }
);
```

### Python

```python
import requests

response = requests.get(
    'https://api.usejump.co.uk/v1/patients',
    headers={
        'Authorization': 'Bearer pk_live_your_api_key',
        'Content-Type': 'application/json'
    }
)
```

## Scopes

API keys are assigned scopes that control which resources they can access. Request only the scopes your application needs.

### Core Resource Scopes

| Scope                  | Description                                                     |
| ---------------------- | --------------------------------------------------------------- |
| `read_patients`        | View patient records and demographics                           |
| `write_patients`       | Create and update patient records                               |
| `read_appointments`    | View appointment records                                        |
| `write_appointments`   | Create, update, and cancel appointments; hold and release slots |
| `read_episodes`        | View episodes and episode events                                |
| `write_episodes`       | Create episodes, add events, close/reopen/assign                |
| `read_questionnaires`  | View questionnaire templates and responses                      |
| `write_questionnaires` | Create and update questionnaire responses                       |

### Configuration Scopes

| Scope                         | Description                                  |
| ----------------------------- | -------------------------------------------- |
| `read_appointment_templates`  | View appointment type configuration          |
| `write_appointment_templates` | Create and update appointment templates      |
| `read_pathways`               | View pathway configs and rules               |
| `write_pathways`              | Update pathway configs, manage pathway rules |

### Read-Only Resource Scopes

| Scope                     | Description                                     |
| ------------------------- | ----------------------------------------------- |
| `read_consultations`      | View consultation records and clinical sections |
| `read_locations`          | View practice locations                         |
| `read_clinician_profiles` | View clinician profiles and booking status      |
| `read_availability`       | Query booking slot availability                 |

### Webhook Scopes

| Scope             | Description                                        |
| ----------------- | -------------------------------------------------- |
| `manage_webhooks` | Create, update, delete, and test webhook endpoints |

<Callout type="info">
  Some scopes cover related resources. `read_episodes` / `write_episodes` grants access to both `/episodes` and `/episode-events`. `read_questionnaires` / `write_questionnaires` covers both templates and responses. `read_pathways` / `write_pathways` covers both configs and rules.
</Callout>

<Callout type="info">
  API keys default to `['read_patients']` if no scopes are specified during creation. Request only the scopes your application actually needs.
</Callout>

## Authentication Errors

| Status Code | Error Code     | Description                          |
| ----------- | -------------- | ------------------------------------ |
| `401`       | `UNAUTHORISED` | Missing or invalid API key           |
| `403`       | `FORBIDDEN`    | Valid key but missing required scope |

### Error Response

```json
{
  "data": null,
  "meta": { "request_id": "req_abc123" },
  "error": {
    "code": "FORBIDDEN",
    "message": "Missing required scope: 'read_episodes'. Your API key has scopes: [read_patients]."
  }
}
```

## Security Best Practices

<Callout type="info">
  Following these practices helps protect your API keys and patient data.
</Callout>

1. **Never expose keys in client-side code** - API keys should only be used in server-side applications
2. **Use environment variables** - Store keys in environment variables, not in source code
3. **Rotate keys regularly** - Create new keys periodically and revoke old ones
4. **Use minimal scopes** - Only request the scopes your application actually needs
5. **Monitor usage** - Review API logs for unexpected activity
6. **Use test keys for development** - Only use live keys in production environments

## Next Steps

* Learn about [Rate Limits](https://docs.usejump.co.uk/getting-started/rate-limits) and usage quotas
* Explore the [API Reference](https://docs.usejump.co.uk/api-reference) for endpoint details
