Blog

    Decoding SMART on FHIR: Discovery, Launch Types, Context and PKCE Explained

    By GauravJuly 6, 20269 mins read
    On this page

    SMART on FHIR is often described as "OAuth for health apps", which is true and not very useful. The specification behind it answers a narrower set of questions: how an app finds the right servers, how it learns which patient it is looking at, and how it proves the token request came from the app that started it.

    This post reads the SMART App Launch guide the way an engineer should before writing code against any vendor. It explains discovery, the two launch types, public and confidential clients, launch context and PKCE, and what changed in version 2.

    What is SMART on FHIR, read as a specification?

    Building an app that launches from several EHRs?

    Our FHIR and SMART on FHIR team builds launch flows that work across vendors, not only in one sandbox.

    SMART on FHIR, read as a specification, is the HL7 SMART App Launch guide: a set of rules that tells an app how to discover a server's authorization endpoints, start a launch, receive patient and user context, and exchange a code for a token. Agnotic Technologies builds apps against that guide first and vendor quirks second.

    Reading the guide directly saves time later. Most launch bugs come from an assumption that one vendor's behavior is the standard.

    Which problems does SMART App Launch solve?

    SMART App Launch solves three problems that plain OAuth leaves open in healthcare. An app needs to know which patient and encounter the clinician is working on, it needs access limited to what the user may see, and it needs to work the same way across EHRs.

    The SMART App Launch implementation guide uses OAuth 2.0 and OpenID Connect so that apps can launch from an EHR with the user's and patient's context and only the access scopes they were granted.

    It does not solve data modeling, write-back rules or which resources a vendor exposes. Those come from FHIR, US Core and each vendor's own documentation, and a SMART app still has to read them.

    Discovery before the first redirect

    Discovery is the first call a SMART app makes, and skipping it is the root of many hard-coded endpoint bugs. The app should never assume where the authorization server lives.

    The SMART App Launch guide requires servers to publish their authorization endpoints and capabilities as JSON at a location formed by appending /.well-known/smart-configuration to the FHIR base URL.

    • Authorization endpoint: where the app sends the user to log in and approve access.
    • Token endpoint: where the app exchanges the authorization code for an access token.
    • Capabilities: a list that says whether the server supports EHR launch, standalone launch, PKCE methods and client types.
    • Scopes supported: the scopes the server advertises, which may differ from what your registration grants.

    Read the configuration at runtime, cache it for a short period, and log what you received. When a customer moves servers, your app follows without a code change.

    How do EHR launch and standalone launch differ?

    EHR launch starts inside the EHR, which hands the app a launch token tied to the open chart. Standalone launch starts outside the EHR, so the app asks for context and the user picks a patient or signs in as one.

    Side-by-side comparison of SMART EHR launch and standalone launch: where each starts, what the app receives, where patient context comes from and which apps typically use each
    • EHR launch: the EHR calls your launch URL with an issuer and a launch value, and context comes from the chart the clinician opened.
    • Standalone launch: the app starts on its own, requests context scopes, and the server asks the user to choose or confirms the signed-in patient.
    • Clinician tools: usually EHR launch, because the clinician should never search for a patient twice.
    • Patient apps: usually standalone launch from a phone or browser, often against the patient-facing endpoint.

    Many products need both. Build the token handling once and keep the two entry points thin.

    Public or confidential client?

    A public client cannot keep a secret, such as a single-page app or a mobile app. A confidential client runs on a server that can hold a secret or a private key.

    Pick the client type from where your code runs, not from what is easiest to register. A mobile app that pretends to be confidential by shipping a client secret in the binary has no secret at all, because anyone can extract it.

    • Public: browser and mobile apps, protected by PKCE and short-lived tokens.
    • Confidential with a secret: a server-side app that stores the secret in a vault.
    • Confidential with a key: a server-side app that signs a client assertion with a private key and publishes the public key.

    What does the launch context carry?

    The launch context is the set of values the server returns alongside the access token. It tells the app who is using it and what they are looking at, so the app can render the right view straight away.

    • Patient: the FHIR id of the patient in context, when patient context was requested and granted.
    • Encounter: the current encounter, when the EHR supports it and the app asked for it.
    • User identity: an id token and a FHIR user reference, when the app requested OpenID Connect scopes.
    • Display hints: values such as whether the app should show its own patient banner.

    Treat every context value as optional. A server can grant less than you asked for, and the app should explain what it cannot show rather than failing with a blank screen.

    Why PKCE is mandatory

    PKCE is mandatory because authorization codes can be intercepted, especially on mobile devices and in browsers. The app proves it is the same app that started the flow by sending a secret value it created at the start.

    SMART App Launch 2.x states that all SMART apps shall support PKCE, and that servers shall support the S256 code challenge method and shall not support the plain method.

    In practice the app generates a random code verifier, sends a hashed challenge with the authorization request, and sends the original verifier with the token request. Confidential clients use PKCE too; it is not only for public apps.

    What changed between SMART 1.0 and 2.x?

    Version 2 tightened security and made permissions finer. Teams upgrading an older app usually need to touch registration, the authorization request and scope handling.

    • PKCE: required for every app, with S256 only.
    • Scope syntax: finer read and write permissions, explained in our scopes post rather than repeated here.
    • Asymmetric client authentication: confidential clients can authenticate with a signed assertion instead of a shared secret.
    • Token introspection: a defined way for resource servers to check a token's state.

    Servers may support both versions during a transition. Read the advertised capabilities and test each customer, because two sites on the same vendor can run different releases.

    Where do SMART implementations go wrong?

    Most SMART failures come from treating one vendor sandbox as the specification. The code works in development and breaks at the first customer.

    • Hard-coded endpoints: authorization URLs copied from a sandbox instead of read from discovery.
    • Ignored issuer: an EHR launch accepted without checking the issuer against the registered list.
    • Lost state: the state parameter not stored or not checked, which opens the flow to forged responses.
    • Required context: the app crashes when the server grants patient context but no encounter.
    • Token storage: access and refresh tokens kept in browser storage that other scripts can read.

    A worked example: one app, read from the spec outward

    Consider a clinician-facing medication review app that must launch from two EHR vendors. The team starts with the guide: discovery at runtime, EHR launch as the main path, a confidential server-side client with key-based authentication, and PKCE on every flow.

    Only then do they read each vendor's documentation for registration steps, supported scopes and context values. Differences go into per-customer configuration, not into branches in the launch code.

    When a third customer arrives on a newer release, the app reads the new capabilities, finds asymmetric authentication supported, and the launch works without a deploy. The only new work is registration and testing at that site.

    Who owns each part of a SMART app launch?

    SMART work crosses your team and each customer's EHR team. Name the owners before the first registration request.

    • Your engineering team: discovery, launch handling, token storage, refresh and error screens.
    • Your security lead: client type, key management, rotation and how tokens are logged without exposing them.
    • Customer EHR team: app registration, activation, the scopes they approve and the launch point in the workflow.
    • Product owner: what the app shows when context or a scope is missing.

    How do you test a SMART app against the spec?

    Test against the guide's rules, not only against a vendor sandbox's happy path. Each rule that can fail should have a test that makes it fail.

    • Discovery tests: a changed endpoint in the configuration is followed without a code change.
    • Launch tests: wrong issuer, missing state, reused code and expired launch value are all rejected.
    • Context tests: patient only, patient with encounter, and no patient at all each render a sensible screen.
    • Token tests: refresh works, revoked tokens are handled, and no token appears in logs.

    What are SMART on FHIR best practices?

    Read discovery on every launch, use PKCE everywhere, and pick the client type from where the code runs. Keep vendor differences in configuration, check the issuer and state on every flow, and treat all context as optional.

    Store tokens server-side where you can, ask for the narrowest access the feature needs, and test each customer site separately even when the vendor is the same.

    How can Agnotic help build a SMART on FHIR app?

    Agnotic builds SMART on FHIR apps for clinicians and patients, from the first launch through registration and go-live at each customer. We write the launch layer against the specification, keep vendor differences in configuration, and set up the tests that catch the failure points above.

    Build your SMART on FHIR app with Agnotic

    We design launch, tokens and context handling that hold up across EHR vendors.

    Sources

    Frequently Asked Questions

    FHIR defines the data and the API. SMART App Launch defines how an app discovers the authorization server, launches with patient and user context, and gets a token to call that API.

    Talk to an engineer about your project

    Send us the EHRs you need to launch from and the app you are building. We will review your launch and token design against the spec.

    Go deeper

    Services and solutions on this topic