# App Launch (Authorization Code + PKCE)

Use this flow when a **user** (clinician or patient) is present: SMART App Launch from
an EHR, a standalone SMART app, or plain OpenID Connect sign-in. No client secret is
required for public clients; PKCE (S256) protects the code exchange.

> For system-to-system access with no user present, see
> [Backend Services (Client Credentials)](/guides/smart-backend-services) instead.

## Environments

| Environment | Base URL | Default |
|---|---|---|
| Production (AU) | `https://au.platform.innowell.org` | Yes |
| Staging (AU) | `https://au.platform-stg.innowell.dev` | |

Switch environments with the server selector at the top of the API Reference. CA-region
hosts are planned.

## The four steps

1. **Register** (out of band). Innowell issues you a `client_id`, registers your
   `redirect_uri`(s), and grants your scopes. Confidential clients also receive a
   `client_secret`.
2. **Discover.** `GET /.well-known/smart-configuration` publishes the authorize/token
   endpoints and supported scopes.
3. **Obtain a token.** Redirect the user to `/oauth/authorize` with PKCE, receive a
   `code` on your `redirect_uri`, then exchange it at `/oauth/token`.
4. **Call the FHIR API** with the resulting bearer token.

## Flow

```mermaid
sequenceDiagram
    participant App
    participant Browser
    participant Auth as Innowell Auth Server
    App->>Browser: Redirect to /oauth/authorize with code_challenge
    Browser->>Auth: Authenticate and consent
    Auth->>Browser: Redirect to redirect_uri with code
    Browser->>App: Deliver code
    App->>Auth: POST /oauth/token with code and code_verifier
    Auth->>App: access_token plus id_token
```

## EHR launch addendum

For an EHR launch, first mint a launch context, then pass its id to `/oauth/authorize`:

1. `POST /oauth/smart/launch` with `patient_id` (the Innowell **role id**, not the FHIR id),
   authenticated with a bearer token. Returns `launch_id`. Only clinicians and lead
   clinicians can mint a launch context; other roles receive `403 insufficient_role`.
2. Add `launch={launch_id}` and `aud={fhir_base}` to the `/oauth/authorize` request.
3. The resulting token carries a `patient` value (FHIR patient id) in the token response.

## Token request (curl)

```bash
curl -X POST "$BASE_URL/oauth/token" \
  -d grant_type=authorization_code \
  -d client_id="$CLIENT_ID" \
  -d redirect_uri="$REDIRECT_URI" \
  -d code="$CODE" \
  -d code_verifier="$CODE_VERIFIER"
```

Replace `$BASE_URL` with your chosen environment. Tokens are RS256 JWTs; validate them
against `GET /oauth/discovery/keys`.
