# Authentication

> Sign in with SMART Backend Services: a JWT signed with your private key, exchanged for a 5-minute access token.

Apps sign in with the [SMART Backend Services](https://hl7.org/fhir/smart-app-launch/backend-services.html) profile of OAuth 2.0: the `client_credentials` grant with a `private_key_jwt` client assertion. There are no passwords or shared secrets. Your app proves who it is by signing a short-lived JWT with its private key.

## 1. Create a key pair

Use RSA (2048 bits or more, signed with **RS384**) or an EC P-384 key (signed with **ES384**). Give the practice the public key only, as a PEM `PUBLIC KEY` or as a JWKS. Soma refuses private keys.

*RSA key pair*

```bash
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out private.pem
openssl pkey -in private.pem -pubout -out public.pem   # give public.pem to the practice
```

## 2. Build the client assertion

Sign a JWT with these claims. Put your key's `kid` in the header if the practice has more than one of your keys.

| Claim | Value |
| --- | --- |
| `iss` | Your client ID. |
| `sub` | Your client ID (the same value). |
| `aud` | The practice's token URL: `https://dev3.somahealth.com/api/fhir/r4/your-practice/auth/token`. |
| `exp` | No more than 5 minutes from now. |
| `jti` | A unique value. A jti can be used only once. |
| `iat`, `nbf` | Optional. `nbf` must not be in the future. |

*Header and claims*

```json
{
  "alg": "RS384",
  "typ": "JWT",
  "kid": "key-2026-12"
}
.
{
  "iss": "soma-7c1e9f2a",
  "sub": "soma-7c1e9f2a",
  "aud": "https://dev3.somahealth.com/api/fhir/r4/your-practice/auth/token",
  "exp": 1765746300,
  "jti": "5b0d5c6e-0a1f-4f7e-9b1c-3e9f2d7a8c41"
}
```

## 3. Request a token

*Request*

```bash
curl -X POST https://dev3.somahealth.com/api/fhir/r4/your-practice/auth/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  --data-urlencode "grant_type=client_credentials" \
  --data-urlencode "scope=system/Appointment.rs system/Patient.rs" \
  --data-urlencode "client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer" \
  --data-urlencode "client_assertion=$ASSERTION"
```

*Response*

```json
{
  "access_token": "q2ZkY3Vt…",
  "token_type": "bearer",
  "expires_in": 300,
  "scope": "system/Appointment.rs system/Patient.rs"
}
```

Tokens are opaque and last **5 minutes**. There are no refresh tokens: when a token expires, sign a new assertion and ask again. Send the token on every request as `Authorization: Bearer <token>`.

## Scopes

Ask for system scopes, one per resource. The token's `scope` lists what you were given: only resources the practice allowed for your app. If none of the scopes you asked for is allowed, the request fails with `invalid_scope`.

| Scope | Grants |
| --- | --- |
| `system/Patient.rs` | Read and search Patient (SMART v2). `.r` or `.s` alone grant just one of them. |
| `system/Patient.read` | Read and search Patient (SMART v1). |
| `system/*.rs`, `system/*.read` | Every resource the practice allowed for your app. |

Supported resources: `Patient`, `Appointment`, `Schedule`, `Slot`, `Practitioner`, `Location`, `Organization`, `Coverage`, `AllergyIntolerance`, `CarePlan`, `CareTeam`, `Condition`, `Device`, `DiagnosticReport`, `DocumentReference`, `Encounter`, `Goal`, `Immunization`, `Medication`, `MedicationRequest`, `Observation`, `PractitionerRole`, `Procedure`, `Provenance`, `QuestionnaireResponse`, `RelatedPerson`, `ServiceRequest`, `Specimen`. Write scopes (`c`, `u`, `d`, `.write`) aren't granted; `patient/` and `user/` scopes aren't supported.

## Location and provider limits

A practice can limit your app to some of its locations or providers. You then see only appointments, schedules and slots at those locations and with those providers, only those locations and providers, and only patients (and their coverage) who have appointments there.

## Discovery

The practice's [SMART configuration](/reference/smart-configuration.md) lists the token URL, signing algorithms and scopes, and the [capability statement](/reference/metadata.md) names the token URL too.

---
Last updated 2026-12-04 · Soma Health FHIR API
