> 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/batch-upsert.md).

# Batch Upsert

Description of how to insert or update a collection of resources using our FHIR API.

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

The batch upsert operation allows you to update or create multiple resources in a single request. This is useful for efficiently managing large sets of data with minimal API calls. The upsert logic works as following:

* **Create:** If a resource with the specified id does not exist, it will be created based on the `resourceType` described in the body.
* **Update:** If a resource with the specified id already exists, it will be updated with the new data provided in the request.

Below is an example of how to use a <mark style="color:green;">`PUT`</mark> request to insert or 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}/`

**Headers**

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

**Body**

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

```json
[
  {
    "name": [{ "given": ["Bob"] }],
    "active": true,
    "resourceType": "Patient",
    "id": "30405060"
  },
  {
    "name": [{ "given": ["Charles"] }],
    "active": false,
    "resourceType": "Patient",
    "id": "40506070"
  },
  {
    "name": [{ "given": ["Christy"] }],
    "active": false,
    "resourceType": "Patient",
    "id": "50607080"
  }
]
```

{% endtab %}
{% endtabs %}

**Response**

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

```json
[
  {
    "name": [
      {
        "given": ["Bob"]
      }
    ],
    "active": true,
    "id": "30405060",
    "resourceType": "Patient",
    "meta": {
      "lastUpdated": "2024-07-17T06:56:48.506516Z",
      "createdAt": "2024-07-17T06:56:48.506516Z",
      "versionId": "540603"
    }
  },
  {
    "name": [
      {
        "given": ["Charles"]
      }
    ],
    "active": false,
    "id": "40506070",
    "resourceType": "Patient",
    "meta": {
      "lastUpdated": "2024-07-17T06:56:48.519054Z",
      "createdAt": "2024-07-17T06:56:48.519054Z",
      "versionId": "540605"
    }
  },
  {
    "name": [
      {
        "given": ["Christy"]
      }
    ],
    "active": false,
    "id": "50607080",
    "resourceType": "Patient",
    "meta": {
      "lastUpdated": "2024-07-17T06:56:48.519054Z",
      "createdAt": "2024-07-17T06:56:48.519054Z",
      "versionId": "540613"
    }
  }
]
```

{% endtab %}

{% tab title="201" %}

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

{% endtab %}

{% tab title="401" %}

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

{% endtab %}

{% tab title="422" %}

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

{% endtab %}
{% endtabs %}
