Write many items in one request
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 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.
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:
marfa items bulk --file bookmarks.jsonThe 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
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
versionis the base Marfa merges your change onto, as for one item. A change to a field another writer changed since is refusedversion_conflict. version: 0says you read no item. The entry creates one where neither its natural key nor itsidmatches, and is refusedancestor_unavailablewhere an item holds the key.- Any other
versionnames an item you read. If no item matches, the entry is refuseditem_not_foundand creates nothing.POST /itemsanswers404 item_not_foundthe same way. - An entry with an
idand aversionother than0, whose natural key matches another item, is refusedvalidation_errornamingid.
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
An item's natural key 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 and on a bulk entry:
{ "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 refused400 validation_errornamingsource. - Two machines that import one file under one name land on the same items, with no claim to set up.
marfa items importwrites this way, as Import a file as items 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'ssource_idstill takes a source the key holds or claims.
The rules are in the contract's items chapter.