Getting started
Update and delete patients
Save changes without overwriting other people's, read earlier versions and delete test records.
You already know how to create and read a Patient. Now let’s change it, look at its history and delete it. Use only fictitious data in these examples.
Set up the SDK server client to run the examples using esus. Use the fictional patient’s ID as patientId.
Update (PUT)
PUT merges the fields you send into the stored resource: fields you leave out are not removed. You can send the full resource or only what changes.
curl -X PUT https://api.esus.health/fhir/Patient/3fa85f64-5717-4562-b3fc-2c963f66afa6 \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/fhir+json" \
-H 'If-Match: W/"1"' \
-d '{
"resourceType": "Patient",
"name": [
{ "use": "official", "family": "Smith", "given": ["John", "Michael"] }
],
"birthDate": "1990-01-16"
}'
const { etag } = await esus.fhir.read("Patient", patientId);
if (!etag) throw new Error("Missing Patient ETag");
await esus.fhir.update("Patient", patientId, {
"name": [
{ "use": "official", "family": "Smith", "given": ["John", "Michael"] }
],
"birthDate": "1990-01-16"
}, etag);
Reads, PUT, and PATCH return an ETag header with the version (W/"2"). If you send If-Match and someone saved another version first, the API answers 412 Precondition Failed instead of overwriting their change. The header is optional, but use it when several people edit the same patient.
Targeted changes (PATCH)
PATCH accepts three formats, chosen by Content-Type:
| Content-Type | Body |
|---|---|
application/fhir+json or application/json | A partial Patient with the fields to change |
application/json-patch+json | JSON Patch operations (RFC 6902) |
application/merge-patch+json | A JSON Merge Patch (RFC 7386) |
curl -X PATCH https://api.esus.health/fhir/Patient/3fa85f64-5717-4562-b3fc-2c963f66afa6 \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json-patch+json" \
-d '[
{ "op": "replace", "path": "/gender", "value": "other" }
]'
const { etag } = await esus.fhir.read("Patient", patientId);
if (!etag) throw new Error("Missing Patient ETag");
await esus.fhir.patchJson("Patient", patientId, [
{ op: "replace", path: "/gender", value: "other" },
], etag);
PATCH also honors If-Match and returns the updated resource.
Read earlier versions
Every change creates a new version:
# All versions
GET /fhir/Patient/3fa85f64-5717-4562-b3fc-2c963f66afa6/_history
# A specific version
GET /fhir/Patient/3fa85f64-5717-4562-b3fc-2c963f66afa6/_history/2
const history = await esus.fhir.history("Patient", patientId);
const version = await esus.fhir.readVersion("Patient", patientId, "2");
Delete (DELETE)
curl -X DELETE https://api.esus.health/fhir/Patient/3fa85f64-5717-4562-b3fc-2c963f66afa6 \
-H "Authorization: Bearer YOUR_TOKEN"
await esus.fhir.delete("Patient", patientId);
The API answers 204 No Content and the patient is permanently removed: afterwards both reads and _history return 404 Not Found.
Understand an error response
| Code | Meaning |
|---|---|
| 200 OK | Successful read or update |
| 201 Created | Successful creation |
| 204 No Content | Successful deletion |
| 400 Bad Request | Invalid parameters |
| 401 Unauthorized | Missing or invalid credential |
| 403 Forbidden | The credential is not allowed to do that |
| 404 Not Found | The resource does not exist |
| 412 Precondition Failed | If-Match does not match the current version |
| 422 Unprocessable Entity | The body does not match the resource schema |
Errors come back as an OperationOutcome. For example, a stale If-Match:
{
"resourceType": "OperationOutcome",
"issue": [
{
"severity": "error",
"code": "conflict",
"diagnostics": "Version mismatch — re-read the resource and retry with the current versionId"
}
]
}
Validate without saving
$validate checks a resource of any type without storing it:
curl -X POST https://api.esus.health/fhir/\$validate \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/fhir+json" \
-d '{ "resourceType": "Patient", "gender": "masculine" }'
const { body: outcome } = await esus.fhir.operation("POST /fhir/$validate", {
body: { "resourceType": "Patient", "gender": "masculine" },
});
The response is an OperationOutcome listing the problems found.
Keep practicing
You have worked through the first steps. Try the practice questions to review what you learned. In Course mode, your progress is saved in this browser.