---
title: REST API – Bulk import (upsert)
description: "This page describes the operation to use to **load or update a large number of records at once**, identifying them by a **business key** (an item code, a customer reference, a store reference) rather than by their Kafinea ID. |…"
url: "https://www.kafinea.com/en/help/api/rest-api-bulk-import-upsert/"
updated: "2026-11-22"
category: API
---

# REST API – Bulk import (upsert)

This page describes the operation to use to **load or update a large number of records
 at once**, identifying them by a **business key** (an item code, a customer reference, a
 store reference) rather than by their Kafinea ID.

| Operation | Method | Description |
| --- | --- | --- |
| `mass_upsert` | POST | Write a batch of records identified by a business key |

It is **generic**: it works for all modules, with the key you choose.

> **Reserved for administrators.** Loading a batch of records is a data
>  migration task. A non-administrator account keeps `create` and `update`, one record at a time, with
>  its own permissions.

---

## Why not `create` or `update`?

The [`create` and `update`](https://www.kafinea.com/en/help/api/rest-api-full-reference-openapi/) operations remain the right choice when you
 write one record at a time. For a data migration, they run into two issues:

- **Volume**: one record per call, meaning 16,000 calls for 16,000 customers;
- **Resuming after an interruption**: `create` always creates. An import interrupted midway cannot
   be restarted without first cleaning up what has already gone through, and `update` requires knowing
   the Kafinea ID, which you don't have yet at this point.

`mass_upsert` finds the record by its business key: it updates what exists, creates what is
 missing, and **writes nothing** when the submitted values already match what's in place. You can therefore
 resend the same batch as many times as needed.

---

## Upsert – Write a batch of records

> **POST** `https://apps.kafinea.com/{instance}/webservice.php`

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| `operation` | string | yes | Must be `mass_upsert` |
| `sessionName` | string | yes | Session identifier |
| `elementType` | string | yes | Module name (e.g. `Products`, `Accounts`) |
| `keyFields` | JSON | yes | Technical names of the fields forming the business key |
| `elements` | JSON | yes | Array of records (500 maximum per call) |

Each element of `elements` is a `champ : valeur` object, **key included**. The technical names of the
 fields are read using [`describe`](https://www.kafinea.com/en/help/api/rest-api-full-reference-openapi/).

**curl example:** update the weight and country of origin of items identified by their code.

```bash
curl -X POST https://apps.kafinea.com/YourKafinea/webservice.php 
  -d "operation=mass_upsert" 
  -d "sessionName=YOUR_SESSION_ID" 
  -d "elementType=Products" 
  -d 'keyFields=["productcode"]' 
  -d 'elements=[
        {"productcode":"9782070413110","productname":"Le Petit Prince","mdsweight":"0.180","mdsorigincountry":"FRA"},
        {"productcode":"9782070368228","productname":"La Peste","mdsweight":"0.150","mdsorigincountry":"FRA"}
      ]'
```

### Response

```json
{
  "success": true,
  "result": {
    "module": "Products",
    "keyFields": ["productcode"],
    "processed": 2,
    "created": 1,
    "updated": 1,
    "unchanged": 0,
    "records": [
      { "index": 0, "id": "14x8842", "outcome": "updated" },
      { "index": 1, "id": "14x9317", "outcome": "created" }
    ],
    "errors": []
  }
}
```

| Field | Meaning |
| --- | --- |
| `processed` | Number of records processed without errors |
| `created` | The key was unknown: the record has just been created |
| `updated` | The key existed and at least one submitted value was different |
| `unchanged` | The key existed and everything was already up to date: nothing was written |
| `records` | The line-by-line detail, with the **position in the batch** and the resulting ID |
| `errors` | The rejected lines, with their position in the batch and the reason |

---

## Choosing the business key

The key is the one you name in `keyFields`, and it can include several fields. It must
 identify **exactly one** record: if the submitted key matches more than one, the line is rejected
 with this reason rather than picking one at random. Correct the duplicate data, then resend the
 line.

A few common keys:

| What you are importing | Business key |
| --- | --- |
| Items | `["productcode"]` |
| External ID mappings | `["sourcesystem","objecttype","externalref"]` |

Key values are compared after removing leading and trailing spaces. An empty key
 value causes the line to be rejected: a business key cannot be guessed.

---

## Replayable: resend without worry

The operation is **idempotent**. The same batch sent twice produces `unchanged` on the second run:
 no duplicates, no unnecessarily updated modification date, and no stray history entries on records.

Likewise, **a faulty line does not cancel the others**: the valid lines in the batch are written, the
 rejected lines come back in `errors` with their position, to be corrected and resent on their own.

On the other hand, if the `elements` parameter itself cannot be read (invalid JSON, more than 500
 elements), the entire call is rejected: writing half of an unreadable batch would be worse than
 writing none of it.

Finally, the operation does not open any backdoor: it applies the same permissions and the same
 required-field checks as `create` and `update`. Whatever these operations reject, this one
 rejects as well.

---

## Use case: declaring IDs from an external system

The most common need is to tell Kafinea **which record corresponds to which object in an
 external system**: a customer from your online store, an item from your marketplace, a
 record from your previous software.

This is the mapping table that Kafinea checks **before** any attempt to match by
 name or email. A declared mapping is a **certainty**, not a guess: it
 eliminates duplicate customer records at the source. A mapping carries three pieces of information:

| Field | Role |
| --- | --- |
| `sourcesystem` | Where the ID comes from. For an online store: `onlinestore:` followed by the store record's ID |
| `objecttype` | The relevant Kafinea module (`Accounts`, `Products`, `SalesOrder`…) |
| `externalref` | The value the external system assigns to this object |
| `recordid` | The targeted Kafinea record, in the form of a Webservice ID (`10x54325`) |

**Uniqueness is guaranteed per source system, and that's the key point.** The same customer may
 have a different ID on each of your stores, and two stores may use the
 same number for two different customers: the mapping can express this, whereas a simple "external
 ref." field on the customer record could not.

```bash
curl -X POST https://apps.kafinea.com/YourKafinea/webservice.php 
  -d "operation=mass_upsert" 
  -d "sessionName=YOUR_SESSION_ID" 
  -d "elementType=MdsExternalRef" 
  -d 'keyFields=["sourcesystem","objecttype","externalref"]' 
  -d 'elements=[
        {"sourcesystem":"onlinestore:44053","objecttype":"Accounts","externalref":"39146","recordid":"10x54325"},
        {"sourcesystem":"onlinestore:44053","objecttype":"Products","externalref":"3760174130121","recordid":"13x8842"}
      ]'
```

`recordid` identifies a record: it is written and read like any other link to a record
 in the API, using the **full Webservice ID** returned by `query` or `retrieve`. A
 raw ID (`54325`) is rejected. The targeted record is thus a clickable link in
 Kafinea, and the customer record displays the list of its external IDs.

Declared mappings can be read back like any other module, using
 [`query`](https://www.kafinea.com/en/help/api/rest-api-requests-and-querying/).

### Migrating an existing customer base

The typical scenario is setting up an online store whose customers already exist
 in Kafinea. Without mappings, synchronization can only guess, and creates duplicates.

1. On the store record, set **customer recognition** to "Store customer ref."
    (see the online store settings).
2. Declare the mappings in batches of 500, using this operation.
3. Run the synchronization: each order finds its customer by identity.

A customer missing from the table is **not** silently created if you have unchecked "Create customers
 from the store": the order is attached to the store's fallback customer and the issue is
 named in the synchronization log. You complete the mapping, then refresh
 the order from the store.

---

## Related references 🔗

- [Full API reference](https://www.kafinea.com/en/help/api/rest-api-full-reference-openapi/)
- [Bulk deletion (trash)](https://www.kafinea.com/en/help/api/rest-api-bulk-deletion-trash/)
- [Queries and lookups](https://www.kafinea.com/en/help/api/rest-api-requests-and-querying/)
- [API introduction](https://www.kafinea.com/en/help/api/rest-api-introduction/)
