How to implement patient consent with SMART on FHIR
Tutorial: SMART on FHIR with OAuth 2.1 and PKCE step by step — discovery, app registration, consent screen, token exchange, scopes enforced, refresh rotation and revocation — and what Anpheros implements.
SMART on FHIR is the standard way an application gets a person's permission to reach their FHIR record: OAuth 2.1 with PKCE for the authorisation, scopes that say exactly what may be read or written, and a discovery document that tells the application where to go. This tutorial runs the whole flow against Anpheros, step by step with curl: discovery, registering an application, sending the person to the consent screen, exchanging the code, calling the API within the granted scopes, refreshing, and what happens when the person revokes access. Anpheros is the SMART on FHIR backend: it hosts the authorisation server, the consent screen and the FHIR R4 server, so you write no login or consent UI of your own.
Why consent and not just an API key
An API key reaches the patients your project created. A person's existing record (their own data, collected by other apps, clinics and laboratories) is theirs to share. With SMART on FHIR:
- the person sees, in their language, which application asks, what it wants to read or write, for what purpose and for how long;
- your application gets only those resource types and actions, and only for that one record;
- every access is logged and visible to the person, who can revoke it at any time;
- the grant is also stored as a FHIR
Consentresource.
The model behind it is described in Healthcare API with patient consent and Consent and access model.
Step 1: discovery
curl https://platform.anpheros.com/fhir/R4/.well-known/smart-configuration
The response lists the endpoints and what the server supports (abridged):
{"issuer": "https://platform.anpheros.com",
"authorization_endpoint": "https://platform.anpheros.com/oauth/authorize",
"token_endpoint": "https://platform.anpheros.com/oauth/token",
"revocation_endpoint": "https://platform.anpheros.com/oauth/revoke",
"code_challenge_methods_supported": ["S256"],
"scopes_supported": ["patient/*.rs", "patient/Patient.r", "patient/Observation.rs", "patient/Observation.rs?category=laboratory",
"patient/Observation.rs?category=vital-signs", "patient/Observation.c", "patient/Condition.rs",
"patient/MedicationStatement.rs", "patient/DocumentReference.rs", "patient/AllergyIntolerance.rs",
"offline_access", "launch/patient", "openid", "fhirUser"],
"capabilities": ["launch-standalone", "client-public", "client-confidential-symmetric", "permission-patient",
"permission-offline", "context-standalone-patient", "sso-openid-connect"]}
SMART client libraries read this document and need nothing else configured.
Step 2: register your application
In the dashboard, open your project and add an application with its redirect URI. Or through the Admin API:
curl -X POST https://platform.anpheros.com/admin/projects/$PROJECT/applications \
-H "Authorization: Bearer $FIREBASE_ID_TOKEN" -H "Content-Type: application/json" -d '{
"name": "CardioAI", "client_type": "public",
"redirect_uris": ["https://cardio.example/callback"],
"purpose": "cardiology follow-up", "privacy_url": "https://cardio.example/privacy"
}'
{"id": "app_…", "name": "CardioAI", "client_type": "public", "client_id": "client_Hg6CknNkYlMgN190", "client_secret": null,
"redirect_uris": ["https://cardio.example/callback"], "purpose": "cardiology follow-up", "status": "active", …}
Use public for mobile and browser apps, which cannot keep a secret and rely on PKCE. Use confidential for a server; its client_secret is shown once. The name, purpose and privacy link are what the person sees on the consent screen.
Step 3: send the person to the consent screen
Create a PKCE pair. The verifier stays with you; only its hash travels:
VERIFIER=$(openssl rand -base64 64 | tr -d '=+/\n' | cut -c1-64)
CHALLENGE=$(printf %s "$VERIFIER" | openssl dgst -sha256 -binary | openssl base64 | tr '+/' '-_' | tr -d '=')
Then open this address in the person's browser (line breaks added for reading):
https://platform.anpheros.com/oauth/authorize?response_type=code
&client_id=client_Hg6CknNkYlMgN190
&redirect_uri=https://cardio.example/callback
&scope=openid fhirUser patient/Observation.rs?category=laboratory patient/Condition.rs offline_access
&state=8c1e… ← a random value you check on return
&code_challenge=$CHALLENGE&code_challenge_method=S256
&purpose=treatment&lang=en
Ask for the least you need. This application reads laboratory results and diagnoses, nothing else. offline_access gives a refresh token, so the person does not have to approve again every 30 minutes.
The person signs in to Anpheros, reads the request in plain language, chooses which record to share (their own, a family member's they care for, or the one your app created for them) and how long the access lasts, then allows or denies. They return to your redirect URI:
https://cardio.example/callback?code=…&state=8c1e…
Check that state is the value you sent before using the code.
Step 4: exchange the code for tokens
curl -X POST https://platform.anpheros.com/oauth/token \
-d grant_type=authorization_code -d client_id=client_Hg6CknNkYlMgN190 \
-d code=$CODE -d code_verifier=$VERIFIER -d redirect_uri=https://cardio.example/callback
{"access_token": "at_…", "token_type": "Bearer", "expires_in": 1800,
"scope": "patient/Observation.rs?category=laboratory patient/Condition.rs openid fhirUser offline_access",
"patient": "82bfd321-6a4d-4aea-943a-a62f4d81d419", "grant_id": "grant_…",
"id_token": "eyJ…", "refresh_token": "rt_…"}
scopelists what was granted. Keep it with the tokens and show only the features it covers.patientis the record's id for your application. It is pairwise: another application sees the same person under a different id.id_tokenis an OpenID Connect token whosefhirUserclaim names the patient resource.- The code works once, and only with the matching
code_verifier.
Step 5: call the API within the scopes
curl "https://platform.anpheros.com/fhir/R4/Observation?patient=$PATIENT&category=laboratory" -H "Authorization: Bearer $ACCESS_TOKEN"
Lab results come back as usual. Ask for something outside the grant, such as medications:
curl "https://platform.anpheros.com/fhir/R4/MedicationStatement?patient=$PATIENT" -H "Authorization: Bearer $ACCESS_TOKEN"
{"resourceType": "OperationOutcome", "issue": [{"severity": "error", "code": "forbidden",
"diagnostics": "The consent behind this token does not allow 's' on MedicationStatement."}]}
That is a 403. Searches inside the grant are filtered too: with Observation.rs?category=laboratory, a search for all observations returns only laboratory ones. Any other patient does not exist for this token (404). The REST view (/v1) and the context API apply the same scopes.
Step 6: refresh
Access tokens last 30 minutes. Refresh tokens last 30 days and rotate on every use:
curl -X POST https://platform.anpheros.com/oauth/token \
-d grant_type=refresh_token -d client_id=client_Hg6CknNkYlMgN190 -d refresh_token=$REFRESH_TOKEN
You get a new access token and a new refresh token. Store the new one right away. Reusing an old refresh token is treated as theft: it fails with 400 and revokes the whole grant.
Step 7: revocation
The person can revoke your access from Anpheros at any time, and you can revoke it yourself, for example when they disconnect in your app:
curl -X POST https://platform.anpheros.com/oauth/revoke -d token=$REFRESH_TOKEN -d client_id=client_Hg6CknNkYlMgN190
From then on every call with that grant answers 401. Handle it by showing the person that the connection ended and offering to connect again. Subscribe to the consent.revoked webhook to find out without waiting for a failed call.
With the SDKs
The TypeScript and Dart SDKs do steps 3 to 6 for you: OAuthFlow.authorizeUrl with a generated PKCE pair, exchange on the callback, and automatic refresh with rotation. Full examples: How to build an AI healthcare app with a FHIR backend (TypeScript) and How to build a healthcare app with Flutter (Dart).
What Anpheros implements
| Part of SMART | In Anpheros |
|---|---|
| Discovery | /.well-known/smart-configuration on the FHIR base, with the endpoints, scopes and PKCE methods supported |
| Launch | standalone launch (the application starts, then asks for access); EHR launch from within a clinical system is not offered |
| Authorisation | OAuth 2.1 with PKCE required, authorisation code flow |
| Scopes | SMART v2 patient scopes, patient/<Resource>.<cruds> with category filters, plus openid and fhirUser |
| Identity | an OpenID Connect id_token naming the patient as a FHIR resource |
| Consent | a hosted consent screen in the patient's language, with the duration chosen by the patient, mirrored as a FHIR Consent |
| Tokens | 30-minute access tokens, rotating 30-day refresh tokens bound to the consent, revocation |
| Conformance | the capability statement declares the SMART security service; the "Standalone Launch" group of the Inferno SMART App Launch STU2 test kit passes 22 of 22 tests (run of 28 September 2026); the other Inferno groups and test kits have not been run |
Writing works the same way: write scopes (patient/Observation.c, .u, .d) let your application create, update or delete within the record. Everything written carries your application as the author, retries with an idempotency key never duplicate a measurement, and deletions are soft and keep history (Medical data API).
What is not there yet
- EHR launch (the application opened from inside a hospital system with a launch context).
system/scopes for backend services; organisations use API keys instead.- Bulk Data export (
$export);$everythingon a patient is available.
These are listed with the rest in Versioning and deprecation, and the capability statement is always the authoritative source.
Frequently asked questions
What is SMART on FHIR?
An open standard, built on OAuth 2.0/2.1 and OpenID Connect, that defines how an application asks for access to a FHIR record, which scopes describe that access, and how the server advertises its endpoints. Any SMART-capable client library can use any SMART-capable server.
Do I need PKCE?
Yes. Anpheros requires PKCE with S256 for every authorisation code flow, for public and confidential clients alike. It stops an intercepted authorisation code from being used by anyone else.
What happens if the person says no?
They return to your redirect URI with error=access_denied and your state, and no code. Explain what the feature needs and let them try again; calls outside a grant always answer 403.
Can a parent give consent for a child?
Yes. On the consent screen a person can choose a record they care for, such as a child's, if it is linked to their account. The grant applies to that record only.
Related
- Authentication and OAuth: API keys, tokens and lifetimes in detail
- Healthcare API with patient consent: what consent changes in your application's design
- Consent and access model
- FHIR platform
- Build a healthcare app
Ask your AI
Paste this into Claude, ChatGPT, Cursor or Claude Code. Your assistant reads this guide and adapts it to your code.
Read https://developers.anpheros.com/guides/smart-on-fhir-backend.md and help me apply this guide to my own project. Use the Anpheros sandbox (base URL https://platform.anpheros.com) with my API key from the ANPHEROS_KEY environment variable, never written in the code. Give me complete, runnable code, the response I should expect, and how to fix the errors in https://developers.anpheros.com/guides/troubleshooting.md if a call fails.Open in ClaudeOpen in ChatGPTThis guide as MarkdownBuild the whole integration with an AI coding tool
Try it free in the sandbox · first FHIR call in 5 minutesFree · 30 synthetic patients · no cardYour first API call in 2 minutes →
