# Write many items in one request (/build/write-items-in-bulk)

Create or update up to 5,000 items in one request, match each to the item it wrote before, say which version and tags you read, and share a source between importers.



An import, a sync or a script often writes many items at once, and writes them again later. [Upsert items in bulk](/reference/api/items/bulkUpsertItems) takes up to 5,000 entries in a body of at most 16 MiB, and **matches each entry to the item it wrote before**. For one item, see [Update items without losing changes](/build/write-safely).

## Send a page of entries [#send-a-page-of-entries]

Each entry is an item to create or update. Give each a `source_id`, its ID where it came from:



**CLI**

```bash  
marfa items bulk --file bookmarks.json
```

**HTTP**

```http
POST /items/bulk
Authorization: Bearer <key>
Content-Type: application/json

{ "atomic": false, "items": [
  { "type": "core.bookmark", "source_id": "4411",
    "properties": { "title": "Lisbon tram map", "url": "https://example.com/trams" } },
  { "type": "core.bookmark", "source_id": "4412",
    "properties": { "title": "Porto guide", "url": "https://example.com/porto" } }
] }
```

**TypeScript**

```ts
const { data, error } = await marfa.POST("/items/bulk", {
  body: { atomic: false, items: bookmarks },
});
```

The file holds the same body. The answer has `counts` and one entry in `results` for each entry sent, in order, with its `outcome`:

| `outcome` | When |
| - | - |
| `created` | No item held the entry's natural key or `id`. |
| `updated` | An item held it, and the entry changed a field. |
| `skipped` | Nothing was written. `reason` is `unchanged`, `trashed` for an item in the bin, or under `mode: "create_only"`, `duplicate_source` or `duplicate_id`. |
| `errored` | The entry was refused, with its `error`. |

**A request is atomic unless you send `atomic: false`:** one refused entry then writes nothing and answers `bulk_atomic_rollback`, with the entry's own status, its code in `details.code` and its place in `details.index`. With `atomic: false`, the entry is `errored` and the others are written.

## Say what you read [#say-what-you-read]

An entry that matches an item updates it, whatever another writer did since. **To overwrite nothing you didn't see, send the `version` you read** in the entry:

* **A `version` is the base Marfa merges your change onto,** as [for one item](/build/versions#when-two-apps-write-the-same-item). A change to a field another writer changed since is refused `version_conflict`.
* **`version: 0` says you read no item.** The entry creates one where neither its natural key nor its `id` matches, and is refused `ancestor_unavailable` where an item holds the key.
* **Any other `version` names an item you read.** If no item matches, the entry is refused `item_not_found` and creates nothing. `POST /items` answers `404 item_not_found` the same way.
* **An entry with an `id` and a `version` other than `0`, whose natural key matches another item,** is refused `validation_error` naming `id`.

Tags don't move an item's version. **Send `read_tags`, the tags you read, with an entry that sets `tags`:** if the item holds another set, the entry is refused `version_conflict` and writes nothing. Order and repeats don't count. An entry that creates an item passes only with an empty list.

## Share a source between importers [#share-a-source-between-importers]

An item's [natural key](/build/write-safely#create-an-item-only-once) is its source with its `source_id`, and a source belongs to one key and the keys that claim it. **An import source belongs to no key.** Any key or app that may write a type can name one on [Create an item](/reference/api/items/createItem) and on a bulk entry:

```json
{ "type": "core.bookmark", "source": "import:core.bookmark/pinboard", "source_id": "4411",
  "properties": { "title": "Lisbon tram map", "url": "https://example.com/trams" } }
```

* **The form is `import:<type>/<name>`.** The type is the entry's exact type, and the name is 1 to 64 lowercase letters, digits, `.`, `_` or `-`. Another type, or another form, is refused `400 validation_error` naming `source`.
* **Two machines that import one file under one name land on the same items,** with no claim to set up. `marfa items import` writes this way, as [Import a file as items](/use/import-items-from-a-file) shows.
* **A writer of the type can learn that a key under the source is taken,** even by an item it can't read.
* **No key or connector can hold or claim a source that starts with `import:`.** Changing an item's `source_id` still takes a source the key holds or claims.

The rules are in the contract's [items](https://github.com/withmarfa/marfa/blob/main/conformance/spec/items.md) chapter.
