> ## Documentation Index
> Fetch the complete documentation index at: /developers/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentication

> Connect a clinician and get an access token for API requests.

Use your application API key to obtain a **Partner JWT** after the clinician
connects their Cortexa account. Use that JWT for clinical API requests.

## Before you begin

Ask your Cortexa integration contact for an application ID, API key, Cortexa app
URL, and registered HTTPS callback. Keep the API key on your backend.

The examples use the sandbox API:

```bash theme={null}
export CORTEXA_API_BASE='https://sandbox.api.cortexanote.com/v2/scribe/open-api'
```

## 1. Connect the clinician

Open `/scribe/authorize` on your assigned **Cortexa app URL**, with these query
parameters. Your application supplies the values automatically.

| Parameter | Required | Description |
| - | - | - |
| `application_id` | Yes | Your partner application ID, issued by Cortexa |
| `external_user_id` | Yes | The clinician's user ID in your system; 1–255 characters. Use the same ID when they reconnect. |
| `redirect_uri` | Yes | Your registered HTTPS callback URI, matched exactly and URL-encoded |
| `state` | Yes | A random value of 16–512 characters. Save it for the current sign-in and verify it on callback. |
| `scope` | Yes | Requested permissions, separated by spaces and URL-encoded; see [scopes](#scopes) |

Example path using synthetic values:

```text theme={null}
/scribe/authorize?application_id=app_example&external_user_id=doctor_123&redirect_uri=https%3A%2F%2Fehr.example%2Fcortexa%2Fcallback&state=sample-random-state-7f2c9a46&scope=scribe%3Aread%20scribe%3Awrite%20scribe%3Agenerate
```

Prepend your assigned Cortexa app URL. The clinician signs in, selects a workspace,
and allows access. Cortexa then redirects to your callback with `code` and `state`.
Verify `state` and exchange the code within **60 seconds**. Each code works once.

> [!NOTE]
>
> `external_user_id` comes from your system; the clinician does not enter it.
> If it is missing or empty, the authorization page shows an incomplete-link
> error and cannot continue. A direct authorization request returns `422`.

## 2. Exchange the code

`POST /jwt` — call this from your backend with the `Cortexa-Api-Key` header.
Load `CORTEXA_API_KEY` from your secret store and `CORTEXA_AUTHORIZATION_CODE` from
the verified callback.

```bash theme={null}
curl --fail-with-body --request POST "$CORTEXA_API_BASE/jwt" \
  --header "Cortexa-Api-Key: $CORTEXA_API_KEY" \
  --header 'Content-Type: application/json' \
  --data "{\"authorization_code\":\"$CORTEXA_AUTHORIZATION_CODE\"}"
```

Example response:

```json theme={null}
{
  "access_token": "JWT_RETURNED_BY_CORTEXA",
  "token_type": "Bearer",
  "expires_in": 900,
  "grant_id": "grant_example",
  "scope": "scribe:generate scribe:read scribe:write"
}
```

| Field | Description |
| - | - |
| `access_token` | Partner JWT for API requests; set this as `CORTEXA_JWT` |
| `expires_in` | Token lifetime in seconds; use the returned value |
| `grant_id` | Saved authorization for this clinician and workspace; keep it for token renewal |
| `scope` | Permissions granted to this token |

Save the grant ID with the clinician's record in your system. You only send
`external_user_id` during authorization; later requests use the code, grant ID,
or JWT as shown here.

## 3. Call the API

Send the Partner JWT in the `Authorization` header:

```bash theme={null}
curl --fail-with-body "$CORTEXA_API_BASE/teams" \
  --header "Authorization: Bearer $CORTEXA_JWT"
```

This returns the authorized workspace. Use the same grant throughout a
consultation: patients and sessions are accessible only through their own grant.

## 4. Renew the token

Set `CORTEXA_GRANT_ID` to the saved grant ID. Before the JWT expires, send the
grant ID and your API key to obtain another token:

```bash theme={null}
curl --fail-with-body --request POST "$CORTEXA_API_BASE/jwt" \
  --header "Cortexa-Api-Key: $CORTEXA_API_KEY" \
  --header 'Content-Type: application/json' \
  --data "{\"grant_id\":\"$CORTEXA_GRANT_ID\"}"
```

Send exactly one of `authorization_code` or `grant_id`. The clinician does not
need to sign in again while the grant remains valid. This flow uses the saved
grant ID rather than a refresh token.

If access is revoked or the clinician loses workspace access, reconnect the
clinician. Key rotation or application disablement can also invalidate tokens.

## Scopes

| Scope | Access |
| - | - |
| `scribe:read` | Read clinical resources, jobs, and the authorized workspace directory |
| `scribe:write` | Create or update patients, sessions, documents, transcripts, audio, and context |
| `scribe:generate` | Generate notes, documents, answers, coding suggestions, and E/M assessments |

Request scopes enabled for your application. Workspace permissions, template
access, entitlements, and quotas also apply. Coding and E/M GET requests require
`scribe:generate` because they can start processing.

## Which credential should I use?

| Operation | Credential |
| - | - |
| Token exchange; connector registration, listing, and updates | Application API key |
| Clinical requests and clinician connector connections | Partner JWT |
| [Usage queries](/developers/usage.md) | API key for application-wide usage; JWT for one grant |
| Cortexa sign-in and consent | Handled by the Cortexa app |

A clinician's normal Cortexa login token cannot replace the Partner JWT.

> [accordion] Verify a JWT
>
> JWTs use RS256. Public keys are available at `GET /.well-known/jwks.json` without
> authentication. Match `kid`, enforce RS256, and verify the issuer, audience,
> and expiry for your environment. Cortexa also checks current access on each request.

## Troubleshooting

| Error | Action |
| - | - |
| Incomplete authorization link | Supply all required query parameters, including a nonempty `external_user_id` |
| `401 INVALID_API_KEY` | Check the key, environment, and application's enabled state |
| `401 INVALID_AUTHORIZATION_CODE` | Restart authorization; the code may have expired or already been used |
| `401 INVALID_GRANT` | Check the saved grant and reconnect if access was revoked |
| `401 INVALID_ACCESS_TOKEN` | Renew the Partner JWT and check the grant remains valid |
| `403 INSUFFICIENT_SCOPE` | Obtain the scopes required by the operation |
| `409 EXTERNAL_USER_ALREADY_LINKED` | This external clinician ID is linked to a different Cortexa user in the selected workspace |
| `422` on token exchange | Send exactly one of `authorization_code` or `grant_id` |

Continue with [your first note](/developers/quickstart.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.