Using the API¶
Mendari's REST and FHIR API by example: a quickstart in three calls, FHIR in and out, CDS
Hooks, and authentication. A running server shows the same examples on its own landing page,
/docs, with its address filled in and a table of what every call needs; its full reference,
with "Try it out", is /docs/reference.
Every example below is one of the scripts in src/mendari/dashboard/snippets/, included here as
it is. CI runs them against a local server, once in open mode and once with a service key
(scripts/check_doc_snippets.py), so they work as written. They need curl and jq, and
every body is synthetic.
Before you start¶
In a clone, uv sync installs Mendari, and the server with it (without uv: pip install .; no
extra is needed). The server serves the bundled demo agent (mendari/portal-message-agent); to
serve your own too, set MENDARI_AGENTS_PATH to their folders (:-separated, searched
first). MENDARI_AGENTS (comma-separated ids) then narrows what is served to those agents, which
must be on the path.
Start a server, and in another terminal name it and, with sign-in on, your API key:
uv run mendari serve # http://localhost:8000
export MENDARI=http://localhost:8000 # the server
export MENDARI_KEY= # with sign-in on: an API key (ohk_…); open mode ignores it
Each script runs as a whole (bash quickstart.sh), or step by step: copy the lines under each
# step: comment.
Quickstart in three calls¶
List the agents, ask the demo agent for a decision on a synthetic patient message (one of its behaviour tests), and read the decision's audit record:
#!/usr/bin/env bash
# Mendari's REST API in three calls: list the agents, ask the bundled demo agent for a decision on a
# synthetic patient message, and read the decision's audit record. The body is synthetic, one of
# the agent's own behaviour tests.
#
# MENDARI is the server (`mendari serve` listens on http://localhost:8000). With sign-in on,
# MENDARI_KEY is an API key (`ohk_…`, the `service` role); open mode ignores it. Needs curl and
# jq. Each "# step:" is shown on its own (the README has them verbatim), and CI runs this file
# against a local server, in open mode and with a service key (scripts/check_doc_snippets.py).
# step: agents — List the agents this server runs
curl -sS --fail-with-body "$MENDARI/api/agents" -H "Authorization: Bearer $MENDARI_KEY" \
| jq '.[] | {id, version, risk}'
# step: decide — Decide on a synthetic patient message
decision=$(curl -sS --fail-with-body -X POST \
"$MENDARI/api/agents/mendari/portal-message-agent/decide" \
-H "Authorization: Bearer $MENDARI_KEY" -H 'content-type: application/json' \
-d '{"message": {"body": "Could you refill my lisinopril? I am running low."}}')
jq '{id, outcome, recommendation, priority, reasons}' <<<"$decision"
id=$(jq -r .id <<<"$decision")
# step: record — Read the decision's audit record
curl -sS --fail-with-body "$MENDARI/api/audit/$id" -H "Authorization: Bearer $MENDARI_KEY" \
| jq '{outcome, review_status, events: [.events[].event]}'
Every decision ends as AUTO (safe to act on), HUMAN_REVIEW (a person decides, with the agent's
recommendation), ABSTAIN (not confident enough to say) or BLOCK (the input or a safety rule
stops it). Check outcome, not only the status code: an input that fails the agent's schema is a
recorded decision with outcome: BLOCK.
FHIR in, FHIR out¶
The FHIR base is /fhir, and GET /fhir/metadata is its CapabilityStatement. POST
/fhir/$decide takes Parameters (agent, and data as a Bundle) and returns the decision as a
FHIR Bundle, a GuidanceResponse first: act on its outcome, not its status. Decisions are read
and searched as Task, and their audit trail as AuditEvent and Provenance, your
organization's only. Mendari is a decision engine, not a FHIR data server: it stores and serves no
clinical resources.
#!/usr/bin/env bash
# FHIR in, FHIR out: what the FHIR base does, $decide on a synthetic Bundle, the decision read
# back as a Task, and its audit trail as AuditEvent and Provenance. The Bundle is the API
# reference's own example (server/api_docs.py FHIR_EXAMPLE): synthetic.
#
# MENDARI is the server; with sign-in on, MENDARI_KEY is an API key (`ohk_…`, the `service`
# role); open mode ignores it. Needs curl and jq. CI runs this file in open mode and with a
# service key; a step only an administrator may run is skipped with the key.
# step: metadata — What this FHIR base does (public)
curl -sS --fail-with-body "$MENDARI/fhir/metadata" | jq '{fhirVersion, software}'
# step: decide — Decide from a FHIR Bundle, and get the decision as FHIR
bundle=$(curl -sS --fail-with-body -X POST "$MENDARI/fhir/\$decide" \
-H "Authorization: Bearer $MENDARI_KEY" -H 'content-type: application/fhir+json' \
-d '{"resourceType": "Parameters", "parameter": [
{"name": "agent", "valueString": "mendari/portal-message-agent"},
{"name": "data", "resource": {"resourceType": "Bundle", "type": "collection", "entry": [
{"fullUrl": "urn:uuid:7f1c2a4e-0d1b-4c55-9a51-3c1e2d000001", "resource": {
"resourceType": "Patient", "id": "example-patient",
"meta": {"tag": [{"system": "https://mendari.co/fhir/sandbox", "code": "synthetic"}]},
"identifier": [{"system": "https://mendari.co/fhir/synthetic-case", "value": "1"}],
"name": [{"family": "Synthetic", "given": ["Example"]}],
"gender": "unknown", "birthDate": "1984-06-15"}},
{"fullUrl": "urn:uuid:7f1c2a4e-0d1b-4c55-9a51-3c1e2d000002", "resource": {
"resourceType": "Communication", "id": "example-message", "status": "in-progress",
"subject": {"reference": "Patient/example-patient"}, "sent": "2026-09-29T13:00:00Z",
"payload": [{"contentString": "Can I get a refill of my blood pressure medicine?"}]}}]}}]}')
jq '[.entry[].resource.resourceType]' <<<"$bundle" # a GuidanceResponse first
jq '.entry[].resource | select(.resourceType == "Parameters").parameter[]
| select(.name == "outcome")' <<<"$bundle" # act on the outcome, not on `status`
id=$(jq -r '.entry[0].resource.identifier[0].value' <<<"$bundle") # the decision id
# step: search — The review queue, and the decision as a Task with its audit trail
curl -sS --fail-with-body "$MENDARI/fhir/Task?status=requested&_total=accurate" \
-H "Authorization: Bearer $MENDARI_KEY" | jq .total # the review queue
curl -sS --fail-with-body "$MENDARI/fhir/Task/$id/_history/1" \
-H "Authorization: Bearer $MENDARI_KEY" | jq '{id, status, meta}' # as first decided
curl -sS --fail-with-body "$MENDARI/fhir/AuditEvent?entity=Task/$id" \
-H "Authorization: Bearer $MENDARI_KEY" | jq '[.entry[].resource.id]' # its audit trail
curl -sS --fail-with-body "$MENDARI/fhir/Provenance?target=Task/$id" \
-H "Authorization: Bearer $MENDARI_KEY" | jq '[.entry[].resource.id]'
# step: trail — One decision's audit trail as a FHIR Bundle (Mendari's own API: an unscoped service key)
curl -sS --fail-with-body "$MENDARI/api/audit/$id/fhir" -H "Authorization: Bearer $MENDARI_KEY" \
| jq '[.entry[].resource.resourceType]'
# step: export — The whole audit trail as FHIR NDJSON (open mode, or an administrator signed in)
curl -sS --fail-with-body \
"$MENDARI/api/audit/export/fhir?agent_id=mendari/portal-message-agent&since=2026-09-01" \
| jq -c '{resourceType, id}'
CDS Hooks. GET /cds-services lists the services, one per agent that declares a hook, and
POST /cds-services/{id} decides on a hook's context and prefetch, answering with cards. An
EHR calls them with a Mendari key (the cds-hooks preset), or, once registered, with its own
signed token: Connecting an EHR.
A prefetch key you leave out, or send empty ({}, [], ""), is read from your fhirServer,
if the deployment allows it; with no fhirServer the request is a 412. A null value means you
have no data, and it is not read. Every value you send goes through the structural R4 check: a
structure error is a 400. A hook always answers: when a key cannot be read (a failed read, an
OperationOutcome, a value that is not a resource, a resource or search page that is not R4
JSON), or a note is only a link or does not decode, the decision is ABSTAIN with reasons such
as could not read labs: timeout, it is recorded, and its card is an info card that says so.
Reasons never repeat a URL, a query or what a server answered. A request fault (the wrong hook, a
malformed field, a required resource you sent something else for) is still a 4xx. An agent
may also read what a link in the record points to (a note's Binary, a Coverage's payer, a
MedicationRequest's drug) from your fhirServer, at most 50 notes and 50 followed references a
decision and 100 reads a request; a link it does not read is a reason too, and fhir/decide and
$decide never read one (linked data). An agent reads a note as text only when it
is text/plain or text/html: an RTF, PDF or CDA note abstains with could not read <field>:
unsupported_type.
A decision that read a value through the deployment's site code map is never automated: its
reasons say site code translated (<code set>): a person decides
(site codes).
Cards and feedback. A card's uuid is its decision's id. Its one suggestion has a uuid of
its own, and the card says selectionBehavior: at-most-one and offers three overrideReasons
(https://mendari.co/fhir/CodeSystem/card-override-reasons: not relevant, already addressed,
clinical judgement, each with its display). The summary uses the agent's display names, and the
detail says the agent's status, the notice and one plain sentence for the outcome. When the
deployment sets MENDARI_PUBLIC_URL, the reasons are behind the card's link to the decision in
the audit trail (which needs a Mendari sign-in), source.url is the agent's card, and, when SMART
is on and your fhirServer is registered for launches in your organization, a smart link
opens the agent. Without it, the reasons are in the detail, but for the confidence thresholds',
whose figures carry no interval.
POST /cds-services/{id}/feedback takes CDS Hooks 2.0 feedback, 1 to 50 cards: each accepted
with the suggestion taken (acceptedSuggestions), or overridden with one of the card's
reasons or none, and its outcomeTimestamp. Each is chained on the decision as card.feedback,
with codes and ids only: userComment is never kept. It is all or nothing. A suggestion or a
reason the card did not offer is a 422, and so is a field CDS Hooks 2.0 does not define or an
outcomeTimestamp with no time in UTC. extension, which CDS Hooks 2.0 reserves for
implementations, is accepted on any object and never kept. A card that is not a decision of that
service in your organization, or, with sign-in on, not one your own credential asked for, is one
404 for all of them. It needs the decide scope and counts against the decide rate limit, not
the daily cap.
curl -s -X POST "$MENDARI/cds-services/mendari.portal-message-agent/feedback" \
-H "authorization: Bearer $MENDARI_KEY" -H 'content-type: application/json' \
-d '{"feedback": [{"card": "<card uuid>", "outcome": "overridden",
"overrideReason": {"reason": {"system": "https://mendari.co/fhir/CodeSystem/card-override-reasons",
"code": "already-addressed"}},
"outcomeTimestamp": "2026-10-18T12:00:00-04:00"}]}'
# {"recorded": 1}
List the services and call the demo agent's hook, with a synthetic patient and message as prefetch.
With sign-in off, the CDS services answer on this machine only (127.0.0.1, localhost): from
anywhere else they are a 403, whatever the request carries.
#!/usr/bin/env bash
# CDS Hooks: the services, one per agent that declares a hook, and a call to the demo agent's
# (the Portal Message Agent's) with a synthetic patient and message as prefetch (the API reference's own FHIR example,
# server/api_docs.py FHIR_EXAMPLE), answered with cards.
#
# MENDARI is the server; with sign-in on, MENDARI_KEY is an API key (`ohk_…`, the `service` role
# or the `cds-hooks` preset); open mode ignores it, and answers CDS Hooks on this machine only, so
# 127.0.0.1 and localhost need no flag. An EHR that signs its own tokens is registered instead
# (docs/guides/connecting-an-ehr.md). Needs curl and jq. CI runs this file in open mode, with a
# service key and with a key from the `cds-hooks` preset.
# step: discover — The CDS services this server offers
curl -sS --fail-with-body "$MENDARI/cds-services" -H "Authorization: Bearer $MENDARI_KEY" \
| jq '[.services[] | {id, hook}]'
# step: hook — Call the agent's hook with the patient and the message as prefetch
curl -sS --fail-with-body -X POST "$MENDARI/cds-services/mendari.portal-message-agent" \
-H "Authorization: Bearer $MENDARI_KEY" -H 'content-type: application/json' \
-d '{"hook": "mendari-message-received", "hookInstance": "7f1c2a4e-0d1b-4c55-9a51-3c1e2d000003",
"context": {"patientId": "example-patient", "communicationId": "example-message"},
"prefetch": {
"patient": {
"resourceType": "Patient", "id": "example-patient",
"meta": {"tag": [{"system": "https://mendari.co/fhir/sandbox", "code": "synthetic"}]},
"identifier": [{"system": "https://mendari.co/fhir/synthetic-case", "value": "1"}],
"name": [{"family": "Synthetic", "given": ["Example"]}],
"gender": "unknown", "birthDate": "1984-06-15"},
"communication": {
"resourceType": "Communication", "id": "example-message", "status": "in-progress",
"subject": {"reference": "Patient/example-patient"}, "sent": "2026-09-29T13:00:00Z",
"payload": [{"contentString": "Can I get a refill of my blood pressure medicine?"}]}}}' \
| jq '.cards[] | {summary, indicator}'
What a decision read. Every decision made from FHIR (CDS Hooks, fhir/decide, $decide, the
SMART launch) carries data_coverage in its record, which GET /api/audit/{id} returns: for
each mapped field and list, the paths it looked for, the code systems the agent declares, the
agent's own codes, and what it found, as counts, with what could not be read counted by reason.
It holds no value, id or text. The answers of fhir/decide and the SMART launch's decide hold it
too. A CDS Hooks card and $decide's GuidanceResponse do not: they give the decision id (the
card's uuid and extension.mendari.decision_id, the GuidanceResponse identifier). In the
dashboard, a FHIR decision's record shows it as "What it read": counts by field and by reason.
A decision on JSON input carries null. Beside it, site_codes is the version and sha256 of
the site code map the decision read its record with (MENDARI_SITE_CODES, its organization's),
or null without one (Connecting an EHR).
SMART on FHIR. When a deployment sets MENDARI_SMART_CLIENT_ID, an EHR launches Mendari at
/smart/launch, and the dashboard opens on that patient's work.
Authentication¶
Open mode (the default, without MENDARI_OIDC_ISSUER and MENDARI_OIDC_CLIENT_ID): anyone
who can reach the server can call every operation, and the Authorization header is ignored,
but for a JWT on /cds-services: that is a CDS client's token, verified as with sign-in on, and
refused unless an EHR is registered for it.
With sign-in on:
- People sign in at
/auth/loginwith their organization's identity provider; the dashboard and the API reference use that session. A script that reads with a person's session cookie sendsX-Mendari-Request: 1too, as Mendari's own pages do: a read with the cookie and neitherSec-Fetch-SitenorOriginis otherwise refused, as another site's link in an older browser would be. - Machines send
Authorization: Bearer ohk_…, an API key a signed-in administrator creates withPOST /api/auth/keysand rotates withPOST /api/auth/keys/{id}/rotate(the old key works for an overlap, 24 hours unless asked). A key has one role,viewerorservice, expires (90 days unless asked, 365 at most), and may carry scopes that narrow it: SMART v2 reads such assystem/Task.rs, andhttps://mendari.co/fhir/scope/decideorhttps://mendari.co/fhir/scope/outcome. The decide scope also lists the agents it may decide with (GET /api/agents,GET /cds-services); no scope reaches Mendari's other reads under/api. For each documented integration path there is a preset ("preset": "fhir","cds-hooks", …) with the role and scope it needs: the list. The dashboard's Access view (API keys) makes keys from these presets, shows each once, and rotates and revokes them. - An EHR registered in
MENDARI_CDS_CLIENTScalls the CDS Hooks services with its own signed token (RS384 or ES384,audthe URL it calls, ajtiused once), as the principalcds:<id>, which reaches nothing else: Connecting an EHR. - With SMART Backend Services on, a registered client exchanges a signed assertion (RS384 or
ES384, never a client secret) for a five-minute access token,
Bearer mat_…. A client keeps its id when its public keys change (POST /api/auth/clients/{id}/keys, or the Access view's Clients tab): its tokens stop at once. - Where the agent registry is served under the engine (
MENDARI_REGISTRY=on, off by default), an administrator claims a namespace and mints publisher tokens withPOST /api/registry/namespacesandPOST /api/registry/tokens(people only, like API keys). A publisher token (Bearer ohr_…) publishes to its own namespace at/registry/v1/...and is a credential nowhere else; an API key or a session cookie is never one there: Running a registry. The registry's reviewers (members of its one review organization who holdregistry_reviewer; people only) read the queue of pending claims and decide them, and review, recall and delete versions, under/api/registry/review: Registry reviewers. - A credential never goes in a URL or a form. Refusals are a
401or403withWWW-Authenticate, except a signed-in person's who has not yet acknowledged the notice (GET /api/notice): a403withMendari-Notice-Requiredon routes that take data in.
#!/usr/bin/env bash
# Authentication: whether sign-in is on, and what your API key may do. People sign in at
# /auth/login; machines send `Authorization: Bearer ohk_…`, a key a signed-in administrator
# creates (never in a URL or a form, and never with another key).
#
# MENDARI is the server; MENDARI_KEY is your API key (open mode ignores it). Needs curl and jq.
# CI runs this file in open mode and with a service key (scripts/check_doc_snippets.py).
# step: mode — Is sign-in on here? (public, no credential)
curl -sS --fail-with-body "$MENDARI/api/auth/me" | jq '{mode, signed_in}'
# step: me — What your key may do: its role, permissions, scopes and expiry
curl -sS --fail-with-body "$MENDARI/api/auth/me" -H "Authorization: Bearer $MENDARI_KEY" \
| jq .principal
An organization's administrators read the chained events of what it owns (its keys, clients,
people, automation approvals and holds, evaluation reports) at GET /api/audit/administrative,
oldest first, or newest first with order=desc, and paged (next is the following page's
after, in the same order; kind, subject such as key:<id>, and since / until as
instants with their offset narrow it), and export them as
FHIR AuditEvents of ids and codes at GET /api/audit/administrative/export/fhir. A decision's
own trail is GET /api/audit/{id}.
What each call needs (public, read, decide, an administrator, …) comes from the server's route
rules; a running server's /docs lists every call with it. Setting up sign-in, roles and keys is
in Deploying with real patient data.
An organization's agent store¶
Where the operator turns it on (MENDARI_AGENT_STORE=on, memberships mode), a person who holds
agent_author in an organization keeps that organization's own versions of agents under
/api/store (the agent store). Every route acts for the
organization the person acts for; no key or client reaches them.
| Route | What it does |
|---|---|
GET /api/store/agents, GET /api/store/agents/{namespace}/{name} |
The organization's agents and their versions: state, validation, dataset checks |
GET /api/store/versions/{digest} |
One version, with its Decision Card rendered from its files |
GET /api/store/versions/{digest}/file?path=… |
One text file of a version (UTF-8, at most 1 MiB), for its editor |
POST /api/store/versions |
A signed archive (mendari archive build) under the body limit |
POST /api/store/uploads, PUT …/parts/{n}, POST …/complete |
A larger archive in parts |
POST /api/store/copies |
agent (the id of an agent served here) and name: a draft copy of that agent, renamed <organization>/<name> |
POST /api/store/versions/{digest}/edits |
{files: [{path, content}]}: a new draft with those text files replaced |
POST /api/store/versions/{digest}/tests |
Run the version's behaviour tests |
DELETE /api/store/versions/{digest} |
Withdraw a draft |
GET, POST /api/store/keys, DELETE /api/store/keys/{key_id} |
Authors' keys (administrators) |
GET, POST /api/store/datasets, GET …/{dataset_id} |
Your labelled datasets, checked as a version is |
POST /api/store/jobs, GET /api/store/jobs, GET …/{job_id} |
Evaluation jobs: a version (or the agent it was copied from) on a dataset |
POST /api/store/jobs/{job_id}/advance |
Run a job's next slice (about 20 s); the last writes its report |
GET /api/store/comparisons?draft=…¤t=… |
Two finished jobs, paired on the same cases |
POST /api/store/agents/{namespace}/{name}/improvement |
The improvement dataset from reviewers' corrections (needs reading inputs too) |
GET /api/store/versions/{digest}/proposed-tests?dataset=…, POST …/proposed-tests/{dataset}/{case} |
The corrections as proposed tests; accept or dismiss one |
GET, POST /api/store/promotions, POST …/{promotion_id}/retire |
Promote a version to be served to your organization (governance, admin; never its author), or stop serving it |
An upload whose files read as a person's details is refused (422) with the detector's report:
kinds, files and lines, never the text.
The dashboard's Agent store view (LP-20j) does all of this for a person who signs in: copy,
upload, edit, test, run jobs, compare, and, for governance and admin, promote.