On-Behalf-Of Delegation
On-Behalf-Of lets your backend use one privileged ClientApplication as its machine-to-machine identity and supply a user's membership context for each OpenOrchid request. This is the decision guide's single machine-to-machine user approach. OpenOrchid applies the delegated membership's access policy and records the application and delegated profile on resource writes.
Use this page to implement delegation with an application credential. For a backend using exchanged user credentials, follow server-held token exchange.
Overview
Consider this common OpenOrchid architectural pattern:

Your backend authenticates the person using your application's login system, resolves the corresponding OpenOrchid membership, and attaches that context to outgoing OpenOrchid requests. Derive the delegation header from the verified identity and authorized application context. See Server-to-Server Communication for recommended practices.
The safest use of On-Behalf-Of is for FHIR API requests under /fhir/R4/, where the delegated membership's access policy controls the response. Endpoints outside the FHIR API, such as /auth/, /oauth2/, /admin/, and /scim/, manage sessions, credentials, accounts, and project settings. They do not all handle delegation the same way. If your backend sends delegated requests to these endpoints, test each one with each type of delegated membership you use, and confirm the behavior before you release. See Security Considerations.
How to Use
Authenticate the application
Authenticate your backend as a ClientApplication with Project Admin rights. Follow Client Credentials to obtain an application access token. On-Behalf-Of also supports Basic Authentication. Keep these application credentials on the trusted backend.
Resolve and attach user context
Authenticate the incoming user request and resolve its OpenOrchid membership using a mapping controlled by your backend. Include the X-Medplum-On-Behalf-Of header on each outgoing request. The value can be either:
- A membership reference,
ProjectMembership/{id}. This explicitly selects the membership stored in your backend's mapping. - A profile reference, such as
Patient/{id}orPractitioner/{id}. OpenOrchid looks up the correspondingProjectMembershipin the application's project.
The server also accepts the name X-OpenOrchid-On-Behalf-Of, but send X-Medplum-On-Behalf-Of when you use MedplumClient. The client turns off its request cache and request batching for delegated requests only when it sees that name. Without that, one user could receive another user's cached response.
For more information on ProjectMemberships and profile resources, see the User Management Guide.
Include the header on every delegated request. OpenOrchid resolves the delegation context for that request, and resource versions created or updated under that context retain the delegated profile in meta.onBehalfOf.
Handle missing or invalid context
Without the header, OpenOrchid uses the application's own access. A valid header for a different user can delegate to that user. Reject requests with missing or inconsistent user context before calling OpenOrchid, and do not forward an arbitrary delegation header supplied by the caller.
Treat a rejected delegation request as a failure; do not retry it without the header. Keep privileged application operations separate from handlers serving user requests.
Live updates
Do not use On-Behalf-Of for WebSocket subscriptions. Use a user-scoped OpenOrchid token, or deliver updates through a backend that enforces each recipient's resource and field-level access. WebSocket notification authorization does not preserve the HTTP request's delegation context.
Security Considerations
The application credential has Project Admin rights, and the delegated membership's access policy is often the only control on what a delegated request can do. Combined with a backend that forwards requests too freely, this setup can expose that credential to an end user. Review the following before you put a delegating backend in front of end users.
Forward an allowlist of requests
Do not build a passthrough proxy that forwards any path, method, or body supplied by the browser. Define the specific requests that each handler needs, such as reading Patient resources or creating Observation resources, and reject everything else.
- Restrict delegated requests to FHIR API paths under
/fhir/R4/. Within the FHIR API, allow only the resource types and operations your application uses. - Do not forward requests from end users to
/auth/,/oauth2/,/admin/,/scim/,/email/, or/keyvalue/. These endpoints act on sessions, credentials, accounts, and project configuration. Some reject delegation. Others honor it or act on the application's own login. For example,/auth/preauthorizecreates a sign-in code for the delegated user, and a logout request can end the application's session instead of a user session. - Remove any
Authorization,X-OpenOrchid-*orX-Medplum-*headers that the caller sent before you add your own. The server accepts both names, so strip both. - Return only the fields your frontend needs, rather than passing OpenOrchid responses through unchanged.
Give each delegated membership a restrictive access policy
OpenOrchid checks every delegated FHIR request against the delegated membership's access policy. That policy is the main control on what the end user can see and change.
- Assign an explicit access policy to every membership you delegate to. A
ProjectMembershipwithout an access policy has full read and write access to the project's resources, includingClientApplicationresources and their secrets. - Exclude
ClientApplication,Bot, and other configuration resources from end-user policies. If a role needs to readClientApplication, addsecretandretiringSecretto hidden fields. - Do not delegate end-user requests to Project Admin memberships. A delegated admin membership has admin rights, including on the
/admin/endpoints.
Protect the application credential
- Store the client ID and secret only on the backend, and never return them to the browser.
- Use a dedicated
ClientApplicationfor each delegating backend, so you can identify its traffic and revoke it on its own. - If you suspect that a client secret was exposed, rotate it with
$rotate-secret, and review theAuditEventresources and resource history created by that application.
How it Works
When the OpenOrchid Server receives a request with the X-Medplum-On-Behalf-Of header, it will:
- Authenticate the application and check its Project Admin authority.
- Resolve the header to a
ProjectMembershipand its profile. The membership and its project must be active. Project Admin delegation stays within the application's project. Super admin context can delegate across projects using an explicitProjectMembership/{id}reference; profile references always resolve in the application's project. - Apply the delegated membership's access policy and any SMART resource scopes on the application's login.
- Execute the request. On resource creates and updates, record the application in
meta.authorand the delegated profile reference inmeta.onBehalfOf. Read responses retain the attribution stored on the resource version.
Example
Here is a curl example of using the X-Medplum-On-Behalf-Of header:
curl 'https://api.medplum.com/fhir/R4/Patient' \
--user $MY_CLIENT_ID:$MY_CLIENT_SECRET \
-H 'content-type: application/fhir+json' \
-H 'x-medplum: extended' \
-H 'x-medplum-on-behalf-of: ProjectMembership/00000000-001a-4722-afa1-0581d2c52a87' \
--data-raw '{"resourceType":"Patient","name":[{"given":["Homer"],"family":"Simpson"}]}'Here is the same example using the Medplum SDK:
const medplum = new MedplumClient({
clientId: MY_CLIENT_ID,
clientSecret: MY_CLIENT_SECRET,
});
await medplum.createResource(
{
resourceType: 'Patient',
name: [{ given: ['Homer'], family: 'Simpson' }],
},
{
headers: {
'X-Medplum': 'extended',
'X-Medplum-On-Behalf-Of': 'ProjectMembership/00000000-001a-4722-afa1-0581d2c52a87',
},
}
);For a client dedicated to one delegated membership, you can set the header at construction. Keep that client isolated to the selected user context:
const medplumWithDefaults = new MedplumClient({
clientId: MY_CLIENT_ID,
clientSecret: MY_CLIENT_SECRET,
defaultHeaders: {
'X-Medplum': 'extended',
'X-Medplum-On-Behalf-Of': 'ProjectMembership/00000000-001a-4722-afa1-0581d2c52a87',
},
});Note the two extra HTTP request headers:
X-Medplum: extended- This is required to enable the extended OpenOrchid API features, such asmeta.authorandmeta.onBehalfOf.X-Medplum-On-Behalf-Of- A membership reference (ProjectMembership/{id}) or profile reference identifying the user context for this request.
Here is the resulting FHIR JSON:
{
"resourceType": "Patient",
"id": "00000000-689b-4e20-a3c2-6f55108b432a",
"meta": {
"versionId": "00000000-b58b-47c8-9fc0-f85627d333c9",
"lastUpdated": "2024-07-13T21:18:45.850Z",
"author": {
"reference": "ClientApplication/00000000-d361-46f0-adf4-f56da467dc08",
"display": "My Client"
},
"onBehalfOf": {
"reference": "Practitioner/00000000-9886-4b83-a25a-3b99563b8127",
"display": "My Test User"
}
},
"name": [{ "given": ["Homer"], "family": "Simpson" }]
}