Explore documentation

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.

REST
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"
  }'
SDK
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-TypeBody
application/fhir+json or application/jsonA partial Patient with the fields to change
application/json-patch+jsonJSON Patch operations (RFC 6902)
application/merge-patch+jsonA JSON Merge Patch (RFC 7386)
REST
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" }
  ]'
SDK
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:

REST
# All versions
GET /fhir/Patient/3fa85f64-5717-4562-b3fc-2c963f66afa6/_history

# A specific version
GET /fhir/Patient/3fa85f64-5717-4562-b3fc-2c963f66afa6/_history/2
SDK
const history = await esus.fhir.history("Patient", patientId);
const version = await esus.fhir.readVersion("Patient", patientId, "2");

Delete (DELETE)

REST
curl -X DELETE https://api.esus.health/fhir/Patient/3fa85f64-5717-4562-b3fc-2c963f66afa6 \
  -H "Authorization: Bearer YOUR_TOKEN"
SDK
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

CodeMeaning
200 OKSuccessful read or update
201 CreatedSuccessful creation
204 No ContentSuccessful deletion
400 Bad RequestInvalid parameters
401 UnauthorizedMissing or invalid credential
403 ForbiddenThe credential is not allowed to do that
404 Not FoundThe resource does not exist
412 Precondition FailedIf-Match does not match the current version
422 Unprocessable EntityThe 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:

REST
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" }'
SDK
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.