FHIR API
Batches and transactions
Send many changes in one request, either all-or-nothing or independently, with temporary ids that link resources together.
Transaction or batch?#
transaction |
batch |
|
|---|---|---|
| Atomic | Yes. One failure rolls back everything | No. Each entry succeeds or fails on its own |
| References between entries | Yes, through urn:uuid: fullUrls |
No |
| Typical use | Loading a patient with all their data | Many unrelated reads or writes at once |
A transaction with linked resources#
Give each new resource a temporary fullUrl of the form urn:uuid:<uuid>. Other entries reference it by that value. The server swaps in real ids when it commits.
{
"resourceType": "Bundle",
"type": "transaction",
"entry": [
{
"fullUrl": "urn:uuid:7c3a1e5e-0d3b-4a0e-9a47-1b1f1b5e0001",
"resource": {
"resourceType": "Patient",
"identifier": [{ "system": "https://fhirdrill.dev/mrn", "value": "DRILL-0100" }],
"name": [{ "family": "Bundle", "given": ["Bea"] }]
},
"request": {
"method": "POST",
"url": "Patient",
"ifNoneExist": "identifier=https://fhirdrill.dev/mrn|DRILL-0100"
}
},
{
"fullUrl": "urn:uuid:7c3a1e5e-0d3b-4a0e-9a47-1b1f1b5e0002",
"resource": {
"resourceType": "Observation",
"status": "final",
"code": { "coding": [{ "system": "http://loinc.org", "code": "29463-7", "display": "Body weight" }] },
"subject": { "reference": "urn:uuid:7c3a1e5e-0d3b-4a0e-9a47-1b1f1b5e0001" },
"valueQuantity": { "value": 72.5, "unit": "kg", "system": "http://unitsofmeasure.org", "code": "kg" }
},
"request": { "method": "POST", "url": "Observation" }
}
]
}curl -s -X POST "$FHIR" -H "Content-Type: application/fhir+json" -d @tx.json \
| jq '.entry[].response | {status, location}'The response is a transaction-response Bundle with one response per entry, in the same order as the request.
Entry methods you can mix#
request.method |
request.url |
Effect |
|---|---|---|
POST |
Patient |
create (add ifNoneExist to make it conditional) |
PUT |
Patient/abc |
update or create with that id |
PUT |
Patient?identifier=sys\|val |
conditional update (upsert) |
PATCH |
Patient/abc |
the body is a Binary or Parameters patch |
DELETE |
Observation?patient=abc |
conditional delete |
GET |
Patient?name=x |
read or search, inside a batch |
Processing order
The spec processes a transaction in a fixed order: DELETE, then POST, then PUT and PATCH, then GET, whatever order the entries are in. Don't count on one entry seeing another's changes except through references.