Most openEHR material explains archetypes, templates and the reference model. Far less explains what an engineer actually does on the first day of an integration: which endpoints to call, in which order, with which headers, and what the server sends back when something is wrong.
This post is for developers connecting an application or service to an openEHR clinical data repository. It covers the release to build against, a typical call sequence, finding and creating EHRs, data formats, committing and updating compositions, AQL queries, error handling and testing.
What is the openEHR REST API?
Choosing an openEHR repository?
Our selection guide compares what to check in an openEHR clinical data repository before you build on it.
The openEHR REST API is the published HTTP interface that applications use to create EHRs, commit and read compositions, manage templates and run AQL queries against an openEHR clinical data repository. Agnotic Technologies builds integrations against it so the same client code works across conformant servers.
The specification is shared, but servers differ in which parts they implement and which extensions they add. Read your server's documentation beside the specification, not instead of it.
Which release should you build against?
Build against the latest released version your server supports, and pin it in your client configuration. Trial releases are useful to read but risky to depend on.
openEHR International lists Release 1.1.0 as the latest released openEHR REST API specification (ITS-REST), with EHR, Query, Definition, Demographic, Admin and System APIs, while Release 1.2.0 is published with trial status.
Ask your server vendor which release and which APIs they conform to, and which optional parts they skip. Many integrations only need the EHR, Definition and Query APIs, so confirm those first.
How does a typical call sequence look?
A typical integration uploads its template once, then for each patient finds or creates an EHR, commits compositions and reads data back with AQL. Updates use the previous version identifier.
- Step 1, template: upload the operational template through the Definition API and fetch example data for it.
- Step 2, EHR: look up the patient's EHR by subject id and namespace, or create one with POST /ehr.
- Step 3, commit: POST the composition to /ehr/{ehr_id}/composition.
- Step 4, update: PUT the new version with If-Match set to the preceding version.
- Step 5, read: run AQL through POST /query/aql with parameters and paging.
Your integration service keeps three things between calls: the patient's subject id, the mapping from form fields to template paths, and the last version_uid of each composition it may update.
How do you find or create an EHR for a patient?
Find an EHR by the patient's subject id and namespace, and create one only when the lookup returns nothing. The subject values are matched against the EHR_STATUS subject reference.
Look up before every create, even when your own database says the patient is new. Another system may already have created the EHR, and a second one for the same person splits the record.
Choose the namespace carefully, for example one per issuing organization's patient identifier. Two systems that create EHRs for the same person with different namespaces leave you with duplicate records that are hard to merge later.
Creating an EHR also creates its EHR_STATUS. If your server lets you supply it, set the subject and the queryable and modifiable flags on purpose rather than accepting defaults.
Flat, structured or canonical JSON?
Use the simplified flat format for form-style data entry, the structured format when you need nesting, and canonical JSON when you need every reference model attribute. Most application integrations start with flat.
The openEHR REST API overview says clients and servers must use application/openehr.wt.flat+json for the Simplified Flat JSON format and application/openehr.wt.structured+json for the Simplified Structured JSON format.
Fetch the template's example data before writing any mapping code. It shows the exact paths and value shapes the server expects, which saves trial and error against validation errors.
Versioning with ETag and If-Match
Compositions are versioned, so an update must name the version it replaces. The composition update operation requires the existing latest version_uid, the preceding version, in the If-Match header.
The openEHR REST API overview says clients should send If-Match with state-changing requests to prevent lost updates; if the condition is false, the service must not perform the request, must respond 412 Precondition Failed and should return the latest version_uid in the ETag header.
Store the version_uid returned on every commit, and use the versioned object id in the URL when you update. When you get a 412, fetch the latest version, merge or ask the user, and retry. Never retry blindly with the new ETag.
How should you query with AQL?
Query with AQL through the Query API, passing values as query parameters rather than building the AQL string by hand. Parameters avoid injection mistakes and keep the query text the same from call to call.
- Ad-hoc queries: POST /query/aql with the AQL text and parameters.
- Stored queries: named and versioned through the Definition API, then executed by name.
- EHR scope: pass ehr_id to run a query within one patient's record.
- Paging: use offset and fetch, since the default row count depends on the server.
AQL paths follow the archetypes inside your templates, so a query written for one template version may return nothing after a change. Keep queries next to the templates they depend on and test them together.
Move stable queries into stored queries once they work. Named, versioned queries are easier to review and change than AQL strings scattered through client code.
How should errors be handled?
Handle errors by status code and keep the server's error details. The specification lists 400 for bad requests, 409 for conflicts or possible duplicates, 412 for failed preconditions and 422 for semantic errors in a well-formed request.
Send Prefer: return=representation when you want error details back, since services may add them for 4xx and 5xx responses when it is present. Log the details with the template id and the composition path so modellers can see which element failed.
Integration failure points
Most openEHR integration problems come from identity and versioning, not from the payload.
- Duplicate EHRs: different namespaces for the same patient across systems.
- Lost updates: updates sent without If-Match, or retried with a fresh ETag after a 412.
- Template drift: a new template version uploaded while clients still send old paths.
- String-built AQL: values pasted into query text instead of passed as parameters.
- Unbounded queries: no fetch limit, so results depend on the server's default.
Each of these is cheap to prevent in the client and expensive to clean up in the repository. Duplicate EHRs and lost updates in particular leave clinical records that someone must reconcile by hand.
A worked example: a pre-operative assessment form
A surgical team's app captures a pre-operative assessment. The integration uploads the hospital's assessment template, then looks up each patient's EHR by hospital number and namespace.
The app sends the completed form as flat JSON, and the service stores the returned version_uid. When the anaesthetist amends an answer the next day, the service sends a PUT with that version in If-Match.
A nurse had edited the same assessment an hour earlier, so the server answers 412. The app shows both versions, the anaesthetist confirms the merge, and the retry succeeds. A ward dashboard reads the latest assessments with a stored AQL query.
Who owns what?
An openEHR integration has clinical, platform and application owners.
- Clinical modellers: templates, their versions and the meaning of each path.
- Platform team: the repository, its release, APIs enabled, access control and backups.
- Integration engineers: the client, mappings, version handling, retries and logging.
- Identity owner: subject namespaces and how patient identifiers are issued.
Agree how template changes are released before the first one happens. A new template version should reach the test server first, with client mappings updated and tests passing, before production accepts compositions against it.
How do you test against an openEHR server?
Test against a non-production server with the same release and templates as production. Cover the happy path and every error your client claims to handle.
- Commit tests: one valid composition per template, and one invalid for each common mistake.
- Version tests: two clients updating the same composition, expecting one 412.
- Identity tests: lookup before create, and no duplicate EHR for a known subject.
- Query tests: stored queries with parameters and paging against seeded test data.
Run these tests whenever the server, a template or your client changes. Template updates are the most common cause of silent breakage.
Measure the integration in production too: commit failures by status code, 412 rates by template, query times for stored queries and EHR lookups that end in a create. A rising create rate for known patients usually points to an identity problem.
What are openEHR REST API best practices?
Pin the specification release, upload and version templates first, and look up EHRs by subject before creating them. Use flat JSON for form data and fetch example data before mapping.
Send If-Match on every update, handle 412 with a real merge, pass AQL parameters instead of building strings, set fetch limits and keep the server's error details in your logs.
How Agnotic can help
Agnotic builds openEHR integrations for health systems and digital health teams: template uploads, EHR lookup and creation, composition commits and updates, stored AQL queries and the tests that keep them working. We work with your modellers and your repository vendor.
Connect your apps to an openEHR repository
We design the data layer and integrations behind clinical applications built on openEHR.
Related reading on Agnotic
openEHR, FHIR and clinical data APIs.


