Twofold v2.7

Search pages, sections and endpoints.

    Open the console

    Authentication

    Partner API keys, the OAuth linking flow a provider goes through, and how tokens are refreshed.

    Environment

    The API has two credential planes, and every endpoint uses exactly one of them.

    PlaneCredentialReaches
    PartnerYour API keyEverything about your organization: linked providers, access levels, template assignment, seats, webhook keys.
    ProviderAn OAuth access token for one linkEverything belonging to one provider: their patients, notes and transcripts.
    EndpointCredential
    GET /v2/partner/usersPartner key
    PUT /v2/partner/users/{externalUserId}/access-levelPartner key
    POST /v2/partner/users/{externalUserId}/seat/start · …/seat/endPartner key
    POST /v2/partner/users/{externalUserId}/revokePartner key
    PUT /v2/partner/users/{externalUserId}/templatesPartner key
    GET /v2/partner/seats · POST /v2/partner/seats/set-seatsPartner key
    GET /v2/partner/webhook-keys · POST /v2/partner/webhooks/testPartner key
    POST /oauth/token · POST /oauth/revokePartner key
    GET /v2/users/{externalUserId}/meAccess token
    PUT/GET /v2/users/{externalUserId}/patients…Access token
    GET /v2/users/{externalUserId}/notes…Access token

    Partner API keys

    Keys are minted on the API keys page of the partner console and look like twofold_key_prod_… or twofold_key_staging_….

    bash
    curl https://partner-api.trytwofold.com/v2/partner/users \
      -H "Authorization: Bearer twofold_key_prod_xxxxxxxxxxxx" \
      -H "Twofold-Client-Id: prtn_ab12Cd34Ef56"
    bash
    curl https://partner-api.staging.trytwofold.com/v2/partner/users \
      -H "Authorization: Bearer twofold_key_staging_xxxxxxxxxxxx" \
      -H "Twofold-Client-Id: prtn_ab12Cd34Ef56"

    Both headers are required. Twofold-Client-Id is your partnerId — it identifies you even when the key itself is wrong, which is what lets us tell you about failed authentication attempts instead of silently dropping them.

    • The plaintext is shown exactly once, at creation. We store only a SHA-256 hash and display metadata (prefix, last 4, when, by whom); nobody at Twofold can read your key back to you. Lose it and you mint a new one.
    • Several keys can be active at once. Rotation is: mint the new key, deploy it, revoke the old one — with no window where your integration is down.
    • Keys are environment-tagged. A staging key on the production host fails with wrong_environment_api_key, and vice versa.
    • Treat a key as a server-side secret. It is never valid in a browser, and CORS is not enabled for it.

    Linking a provider

    Provider data is reached with OAuth 2.0 Authorization Code + PKCE, with Twofold as the authorization server. The flow is standard; the only Twofold-specific part is that you pass your own id for the provider (external_user_id) into the authorization request, and that the token endpoint authenticates you with your API key instead of an OAuth client secret.

    1. Register your redirect URIs

    Redirect URIs are configured on the Settings page of the partner console and matched exactly — no wildcards, no prefix matching. They must be https: in production.

    On staging you can also register a loopback URI — http://localhost:3000/twofold/callback, or http://127.0.0.1:… — so you can run the linking flow against your own machine while you build. The provider's browser is the thing that follows the redirect, and on staging that browser is yours. Production takes https only, so remember to register the real URI before you go live.

    ⚠️ Loopback is redirect URIs only. Your webhook URL and activation URL must be https: in every environment, because those are fetched by Twofold's servers and opened by clinicians' browsers — neither can reach your machine. For webhooks, expose your local receiver through a tunnel (cloudflared, ngrok) and register the tunnel URL.

    text
    https://app.trytwofold.com/oauth/authorize
      ?client_id=prtn_ab12Cd34Ef56
      &redirect_uri=https://your-emr.example.com/twofold/callback
      &response_type=code
      &scope=full_access
      &state=<random csrf value>
      &external_user_id=provider-1042
      &login_hint=provider%40clinic.example
      &code_challenge=<base64url(sha256(code_verifier))>
      &code_challenge_method=S256
    text
    https://app.staging.trytwofold.com/oauth/authorize
      ?client_id=prtn_ab12Cd34Ef56
      &redirect_uri=https://your-emr.example.com/twofold/callback
      &response_type=code
      &scope=full_access
      &state=<random csrf value>
      &external_user_id=provider-1042
      &login_hint=provider%40clinic.example
      &code_challenge=<base64url(sha256(code_verifier))>
      &code_challenge_method=S256
    Parameter
    client_idRequired. Your partnerId.
    redirect_uriRequired. Must exactly match a registered URI.
    external_user_idRequired. Your id for this provider — it becomes the {externalUserId} in every later call and webhook.
    code_challengeRequired. Base64url SHA-256 of your code_verifier, 43–128 characters.
    code_challenge_methodRequired, and must be S256. The plain method is rejected.
    stateStrongly recommended. Returned untouched on the redirect; it never reaches a Twofold server.
    scopeOptional. The only valid value is full_access, which is also the default.
    login_hintOptional. The provider's email address, prefilled for them if they have to sign in or sign up. URL-encode it.
    response_typecode. Standard libraries send it.

    The provider signs in or signs up on the Twofold side, sees what you are asking for, and approves or declines.

    login_hint saves them a typing step and means nothing beyond that. If they already have a live Twofold session they go straight to consent and the hint is never used; if they do not, it prefills the email field on the sign-up page and follows them to sign-in if they take the Already have an account? link. The field stays editable the whole way. A provider who signs up with a different address than the one you sent is linked under the address they actually used — Twofold does not look accounts up by the hint, does not treat it as verified, and will not refuse a mismatch. external_user_id, not the email, is what ties the link back to your record of them, so a mismatch costs you nothing. An unparseable value is ignored rather than shown, which includes an unencoded + in the address (a raw + in a query string decodes to a space).

    3. Handle the redirect

    OutcomeWhat lands on your redirect_uri
    Approved?code=twofold_code_…&state=…
    Declined?error=access_denied&state=…
    Invalid request?error=invalid_request | invalid_scope | unauthorized_client&error_description=…&state=…

    Two cases deliberately do not redirect: an unknown client_id, and a redirect_uri that is not registered. Sending a browser to an unverified URI is exactly what RFC 6749 §4.1.2.1 forbids, so Twofold renders the error to the provider instead — if your consent link is misconfigured, you will hear it from the provider, not from a callback.

    Some outcomes are neither: they are shown to the provider as something only they can fix, and no code is minted. That happens when they are a member of a Twofold group subscription, when they are already linked to another partner, when they are already linked to you under a different external_user_id, or when the external_user_id you sent already belongs to another Twofold account.

    ⚠️ In all of those cases your callback never fires. Nothing arrives at your redirect_uri, and there is no server-side way to ask Twofold why — so a pending-link state that waits for a callback waits forever. Time it out: expire the pending link after a few minutes, and put the provider back on a screen that lets them try again or contact you. The provider knows what happened, because Twofold told them on screen; your server does not, and cannot. The same applies to a provider who simply closes the tab.

    4. Exchange the code

    Within 10 minutes, from your server:

    bash
    curl -X POST https://partner-api.trytwofold.com/oauth/token \
      -H "Authorization: Bearer twofold_key_prod_xxxxxxxxxxxx" \
      -d client_id=prtn_ab12Cd34Ef56 \
      -d grant_type=authorization_code \
      -d code=twofold_code_prod_xxxxxxxxxxxx \
      -d redirect_uri=https://your-emr.example.com/twofold/callback \
      -d code_verifier=<the verifier you hashed>
    bash
    curl -X POST https://partner-api.staging.trytwofold.com/oauth/token \
      -H "Authorization: Bearer twofold_key_staging_xxxxxxxxxxxx" \
      -d client_id=prtn_ab12Cd34Ef56 \
      -d grant_type=authorization_code \
      -d code=twofold_code_staging_xxxxxxxxxxxx \
      -d redirect_uri=https://your-emr.example.com/twofold/callback \
      -d code_verifier=<the verifier you hashed>

    The two /oauth/* endpoints take client_id in the form body, the way a standard OAuth library sends it. They also accept the Twofold-Client-Id header used everywhere else, and that wins if you send both — see Using an OAuth client library.

    json
    {
      "access_token": "twofold_access_prod_xxxxxxxxxxxx",
      "token_type": "Bearer",
      "expires_in": 3600,
      "refresh_token": "twofold_refresh_prod_xxxxxxxxxxxx",
      "scope": "full_access"
    }
    json
    {
      "access_token": "twofold_access_staging_xxxxxxxxxxxx",
      "token_type": "Bearer",
      "expires_in": 3600,
      "refresh_token": "twofold_refresh_staging_xxxxxxxxxxxx",
      "scope": "full_access"
    }

    The redirect_uri must match the one used in the authorization request. Codes are single-use and expire 10 minutes after consent.

    5. Call as the provider

    bash
    curl https://partner-api.trytwofold.com/v2/users/provider-1042/me \
      -H "Authorization: Bearer twofold_access_prod_xxxxxxxxxxxx"
    bash
    curl https://partner-api.staging.trytwofold.com/v2/users/provider-1042/me \
      -H "Authorization: Bearer twofold_access_staging_xxxxxxxxxxxx"

    No Twofold-Client-Id here — the token already identifies both you and the provider. The {externalUserId} in the path must be the one the token was issued for; anything else is rejected with external_user_id_mismatch rather than quietly returning another provider's data.

    6. Refresh, and keep the newest pair

    bash
    curl -X POST https://partner-api.trytwofold.com/oauth/token \
      -H "Authorization: Bearer twofold_key_prod_xxxxxxxxxxxx" \
      -H "Twofold-Client-Id: prtn_ab12Cd34Ef56" \
      -d grant_type=refresh_token \
      -d refresh_token=twofold_refresh_prod_xxxxxxxxxxxx
    bash
    curl -X POST https://partner-api.staging.trytwofold.com/oauth/token \
      -H "Authorization: Bearer twofold_key_staging_xxxxxxxxxxxx" \
      -H "Twofold-Client-Id: prtn_ab12Cd34Ef56" \
      -d grant_type=refresh_token \
      -d refresh_token=twofold_refresh_staging_xxxxxxxxxxxx

    Access tokens last 1 hour, refresh tokens 90 days. Every refresh rotates both — the response's refresh token replaces the one you sent, and the old one is dead. Store the new pair atomically before you use it.

    If a refresh response never reaches you, just retry with the same refresh token. For 5 minutes after a refresh token is rotated, sending it again returns a fresh pair rather than an error — we follow the rotation forward and hand you the current one. That covers the three ways a well-behaved integration ends up presenting an old token: a response we wrote but your side never received, two of your instances refreshing at the same moment, and a token-store restore. Use the pair you get back and discard the one you sent.

    ⚠️ After those 5 minutes, replaying a credential kills the link. Presenting an authorization code twice, or presenting a superseded refresh token once the grace window has passed, revokes the link itself — every token on it dies with it. This is RFC 9700 behavior: a replay that late is treated as evidence of theft, not as a retry. You find out the same way as any other revocation: a link.revoked webhook arrives with revocationReason: "twofold", the provider reads linkStatus: "revoked" in GET /v2/partner/users, and further calls answer link_revoked. The provider must go through consent again — so make refresh single-flight in your code, and never keep an old refresh token around as a fallback.

    Unlinking

    bash
    curl -X POST https://partner-api.trytwofold.com/oauth/revoke \
      -H "Authorization: Bearer twofold_key_prod_xxxxxxxxxxxx" \
      -H "Twofold-Client-Id: prtn_ab12Cd34Ef56" \
      -d token=twofold_refresh_prod_xxxxxxxxxxxx
    bash
    curl -X POST https://partner-api.staging.trytwofold.com/oauth/revoke \
      -H "Authorization: Bearer twofold_key_staging_xxxxxxxxxxxx" \
      -H "Twofold-Client-Id: prtn_ab12Cd34Ef56" \
      -d token=twofold_refresh_staging_xxxxxxxxxxxx

    Either token of the pair works, and revoking one revokes the link itself and every token on it. The response is always 200, including for a token that never existed (RFC 7009 §2.2). A link.revoked webhook follows.

    If you no longer hold a token for the provider, revoke by the id you know them by instead — same revocation, keyed on externalUserId:

    bash
    curl -X POST https://partner-api.trytwofold.com/v2/partner/users/provider-1042/revoke \
      -H "Authorization: Bearer twofold_key_prod_xxxxxxxxxxxx" \
      -H "Twofold-Client-Id: prtn_ab12Cd34Ef56"
    bash
    curl -X POST https://partner-api.staging.trytwofold.com/v2/partner/users/provider-1042/revoke \
      -H "Authorization: Bearer twofold_key_staging_xxxxxxxxxxxx" \
      -H "Twofold-Client-Id: prtn_ab12Cd34Ef56"

    This is the recovery path when a token store is lost or rolled back: the provider still appears in GET /v2/partner/users, and this ends the link so they can connect again. It returns the same user object with linkStatus: "revoked", and it is safe to retry — a provider whose link has already ended answers 200 and reports it revoked. Only an externalUserId you have never linked is a 404 / user_not_found.

    Providers can also unlink from their Twofold settings, and Twofold support can force-revoke. Your integration should treat link.revoked — and a link_revoked error on any call — as the end of that provider's data access, and offer them the consent flow again.

    Error shape on /oauth/*

    The two OAuth endpoints return the standard OAuth error body, not the Twofold error shape, so an off-the-shelf client can read them:

    json
    {
      "error": "invalid_grant",
      "error_description": "The authorization code has expired.",
      "twofoldRequestId": "req_ab12Cd34Ef56"
    }
    json
    {
      "error": "invalid_grant",
      "error_description": "The authorization code has expired.",
      "twofoldRequestId": "req_ab12Cd34Ef56"
    }

    Possible values are invalid_request, invalid_client, invalid_grant, unauthorized_client, unsupported_grant_type, invalid_scope, temporarily_unavailable and server_error. Every other endpoint uses the shape on Errors.

    Using an OAuth client library

    Any RFC 6749-compliant client handles this flow. Two things to configure:

    • There is no discovery document. Set the authorization and token endpoints manually — Twofold is an OAuth 2.0 authorization server, not an OpenID Connect provider, and there is no id_token.
    • Client authentication is your API key, not a client secret. Send it as Authorization: Bearer twofold_key_… on token requests. Your library's client_id setting works as-is: /oauth/token and /oauth/revoke accept client_id in the form body, which is where every standard library puts it. There is no client_secret — leave it unset, and do not put your API key there.

    The Twofold-Client-Id header is also accepted on these two endpoints and takes precedence if you send both. Sending both with different values is refused as invalid_client (the error_description names the disagreement) rather than resolved in your favour — a request carrying two different client ids means something is misconfigured, and we would rather tell you now than have it surface later as a mismatch against your key.

    Libraries we suggest, by language:

    LanguageLibrary
    Node.jsopenid-client
    PythonAuthlib
    Gogolang.org/x/oauth2
    Rubyoauth2
    PHPleague/oauth2-client
    Javaspring-security-oauth2-client, or Nimbus OAuth 2.0 SDK