Skip to content

Guides

Authentication

OAuth 2.0 with PKCE on a SunCharts EHR installation; client credentials, API keys and user tokens on Connect.

SunCharts EHR: OAuth 2.0 authorization code with PKCE

Each installation is its own authorization server. Tokens act as the staff member who approved your app and can never do more than that person's role allows. This is plain OAuth 2.0; it is not SMART App Launch or OpenID Connect.

Register your application

Only the practice owner can register an application, from their signed-in browser session. The web app lists registered applications and can disable one, but it has no registration form yet, so the owner sends this request:

POST /api/v1/oauth/clients
{
  "name": "Example App",
  "redirect_uri": "https://app.example.test/callback",
  "scope": "clinical.read events.read offline_access"
}
  • Applications are confidential clients. A secret (sce_cs_…) is always issued and shown once.
  • One redirect address per application. It must be https, or http on a loopback host for development.
  • The scopes you list are the most the application can ever request.

Get and use a token

# 1. Read the installation's OAuth settings
curl "$EHR_URL/.well-known/oauth-authorization-server"

# 2. Make a PKCE verifier, challenge and state
VERIFIER=$(openssl rand -base64 48 | tr '+/' '-_' | tr -d '=\n')
CHALLENGE=$(printf %s "$VERIFIER" | openssl dgst -sha256 -binary | openssl base64 | tr '+/' '-_' | tr -d '=\n')
STATE=$(openssl rand -hex 16)

# 3. Send the staff member's browser to the consent screen
#    $EHR_URL/oauth/authorize?response_type=code&client_id=$CLIENT_ID
#      &redirect_uri=https%3A%2F%2Fapp.example.test%2Fcallback
#      &scope=clinical.read%20offline_access&state=$STATE
#      &code_challenge=$CHALLENGE&code_challenge_method=S256
#    Your callback receives ?code=...&state=...&iss=...
#    Check that state matches and iss equals the issuer from step 1.

# 4. Exchange the code within 60 seconds
curl "$EHR_URL/oauth/token" -u "$CLIENT_ID:$CLIENT_SECRET" \
  -d grant_type=authorization_code -d code="$CODE" \
  -d redirect_uri=https://app.example.test/callback -d code_verifier="$VERIFIER"

# 5. Call the API (the access token lasts 5 minutes)
curl "$EHR_URL/fhir/Patient?_count=10" -H "Authorization: Bearer $ACCESS_TOKEN"

# 6. Refresh. Each refresh token works once.
curl "$EHR_URL/oauth/token" -u "$CLIENT_ID:$CLIENT_SECRET" \
  -d grant_type=refresh_token -d refresh_token="$REFRESH_TOKEN"
  • Authorization request. state must be 16 to 512 characters, and code_challenge_method must be S256. A pending request expires after 10 minutes.
  • Code. Valid for 60 seconds, single use. Using a code twice revokes the grant.
  • Token endpoint. Authenticate with HTTP Basic (client ID and secret). Form-encoded body.
  • Access token. Opaque (sce_at_…), valid for 5 minutes.
  • Refresh token. Issued only when the grant includes offline_access. Valid for 30 days and rotated on every use. Replaying an old refresh token revokes the whole grant.
  • Revocation. POST /oauth/revoke with the token revokes the grant behind it. Staff can also revoke grants from the EHR.

Scopes

Scope Allows
clinical.read Read clinical records, including FHIR
clinical.write Write within the staff member's role
billing.read Read insurance and billing records, including FHIR Coverage
billing.write Update insurance and record billing activity
scheduling.read Read locations, providers, services, availability and webhook subscriptions
scheduling.write Create holds and appointments, reschedule or cancel them, manage scheduling webhooks
events.read Read change notices
migration.manage Owner only: stage, reconcile and import records
offline_access Receive refresh tokens

SunCharts Connect Partners

Your server

  • Client credentials. POST /oauth/token with your client ID and secret returns a pt_… access token valid for 1 hour. There is no refresh token; ask for a new one.
  • API keys. Long-lived keys (sk_test_… in the sandbox, sk_live_… in production) sent as Authorization: Bearer or in the X-API-Key header. A key can be limited to some scopes and given an expiry.
  • Both at once. When a request also carries an end user's token in Authorization, put your partner credential in X-Partner-Authorization: Bearer ….

Never ship a client secret or API key in a browser or mobile app.

Your users

Clinical calls use the end user's own access token. Your backend relays the sign-in endpoints with the users.passthrough scope, so users stay inside your app. Sign-in is passwordless: passkeys, or an email link with a six-digit code.

Scopes

The full list is on the Connect reference. Sandbox apps cannot hold clinical.write.

Production access

Production credentials are issued per partner after review. Sandbox credentials never work in production, and the reverse.