> For the complete documentation index, see [llms.txt](https://docs.journl.dk/v1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.journl.dk/v1/api/fhir-api/crud/update/versioned-update.md).

# Versioned Update

Description of how to make versioned update using our FHIR API.

<mark style="color:green;">`PUT`</mark> `{base}/fhir/{type}`

When performing updates, there is a risk of unintentionally overwriting recent changes made by another operation. To avoid this issue, you can use versioned updates. This involves sending an If-Match header with the versionId of the resource you intend to update.

1. **Include the If-Match Header:** When you send an update request, include the If-Match header along with the versionId of the resource you are updating.
2. **Version Matching:** The server will compare the provided versionId with the current version of the resource.
3. **Successful Update:** If the versions match, the update will proceed successfully.
4. **Conflict Handling:** If the versions do not match, the server will respond with an OperationOutcome containing a conflict code. This indicates that the resource has been modified since you last retrieved it, and your update has not been applied to prevent data loss.

Using versioned updates helps ensure data integrity and prevents the loss of recent changes made by other users or processes.

Below is an example of how to use a versioned <mark style="color:green;">`PUT`</mark> request to update a resource. A complete list of resources can be found by visiting the resource specifications [here](https://www.hl7.org/fhir/resourcelist.html).&#x20;

**Request**\ <mark style="color:green;">`PUT`</mark> `{base}/fhir/Patient/{id}`

**Headers**

| Name          | Value              |
| ------------- | ------------------ |
| Content-Type  | `application/json` |
| Authorization | `Bearer <token>`   |
| If-Match      | `versionId`        |

**Body**

{% tabs %}
{% tab title="JSON" %}

```json
{
  "name": [{"family": "Charlie"}]
}
```

{% endtab %}
{% endtabs %}

**Response**

{% tabs %}
{% tab title="200" %}

```json
{
  "name": [
    {
      "family": "Charlie"
    }
  ],
  "id": "20304050",
  "resourceType": "Patient",
  "meta": {
    "lastUpdated": "2024-07-16T11:46:05.849929Z",
    "versionId": "540520",
    "extension": [
      {
        "url": "ex:createdAt",
        "valueInstant": "2024-07-16T10:08:33.753699Z"
      }
    ]
  }
}
```

{% endtab %}

{% tab title="201" %}

```json
{
  "success": "Created"
}
```

{% endtab %}

{% tab title="401" %}

```json
{
  "error": "Unauthorized"
}
```

{% endtab %}

{% tab title="422" %}

```json
{
  "error": "Unprocessable"
}
```

{% endtab %}
{% endtabs %}
