FHIR API
Conditional operations and versioning
Avoid duplicates, avoid overwriting someone else's change and read the history of any resource.
Integration engines retry. Networks drop. The same message arrives twice. Conditional operations are how you make writes idempotent: you can safely send the same write more than once and get the same result.
Conditional create#
Create the resource only if nothing matches the search in If-None-Exist:
curl -s -X POST "$FHIR/Patient" -H "Content-Type: application/fhir+json" \
-H "If-None-Exist: identifier=https://fhirdrill.dev/mrn|DRILL-0002" \
-d '{"resourceType":"Patient","identifier":[{"system":"https://fhirdrill.dev/mrn","value":"DRILL-0002"}],"name":[{"family":"Twice"}]}' \
-o /dev/null -w "%{http_code}\n"| Matches | Result |
|---|---|
| 0 | 201 Created |
| 1 | 200 OK and nothing is created |
| 2 or more | 412 Precondition Failed |
Conditional update (upsert)#
PUT to a search URL instead of an id:
curl -s -X PUT "$FHIR/Patient?identifier=https://fhirdrill.dev/mrn|DRILL-0002" \
-H "Content-Type: application/fhir+json" \
-d '{"resourceType":"Patient","identifier":[{"system":"https://fhirdrill.dev/mrn","value":"DRILL-0002"}],"name":[{"family":"Twice","given":["Updated"]}]}'No match creates the resource, one match updates it, and more than one match returns 412. This is the pattern to use for HL7 ADT A08 updates; see ADT to FHIR Patient.
Optimistic locking with If-Match#
Stop two clients from overwriting each other's changes:
Read and note the ETag#
curl -s -i "$FHIR/Patient/drill-linh" | grep -i etag
# ETag: W/"3"Update only if it's still version 3#
curl -s -X PUT "$FHIR/Patient/drill-linh" -H "Content-Type: application/fhir+json" \
-H 'If-Match: W/"3"' -d @patient.json -o /dev/null -w "%{http_code}\n"If someone else updated it in the meantime, you get 412 Precondition Failed. Read it again, apply your change again and retry.
History#
curl -s "$FHIR/Patient/drill-linh/_history" | jq '.entry[] | {v: .resource.meta.versionId, at: .resource.meta.lastUpdated, method: .request.method}'
curl -s "$FHIR/Patient/_history?_since=2026-11-25T00:00:00Z&_count=20" # every Patient change
curl -s "$FHIR/_history?_count=20" # every change on the serverHistory is the simplest way to answer "what changed, and when?" while debugging an integration.
Conditional delete#
curl -s -X DELETE "$FHIR/Patient?identifier=https://fhirdrill.dev/mrn|DRILL-0002"