Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Document external references for system integrations (#14857)
Damian Legawiec479d85cbf1e9
diff --git a/docs/developer/core-concepts/external-references.mdx b/docs/developer/core-concepts/external-references.mdxnew file mode 100644index 0000000000..94bfca2680--- /dev/null+++ b/docs/developer/core-concepts/external-references.mdx@@ -0,0 +1,234 @@+---+title: External References+description: Record the keys your ERP, PIM, DAM or CRM knows each record by, and address Spree records with those keys instead of Spree IDs.+---++## Overview++When Spree is connected to other systems, the same product lives in several places at once. Your ERP knows it as `MAT-100`, your PIM as `SKU-88412`, and Spree as `prod_86Rf07xd4z`.++External references are how Spree remembers those other keys. Each record can carry one key per system, and every integration can then find, create and update records using the key it already knows — without ever storing a Spree ID.++They solve three problems that come up in every integration:++- **Mapping.** Which Spree product is ERP item `MAT-100`?+- **Repeatable feeds.** A nightly import cannot know whether Spree has seen a row before. Sending the same row twice must update the record, not create a duplicate.+- **Several systems at once.** A PIM sync must never erase the key the ERP wrote.++## How references are stored++```mermaid+erDiagram+ Product ||--o{ ExternalReference : "is known as"+ Order ||--o{ ExternalReference : "is known as"+ Company ||--o{ ExternalReference : "is known as"++ ExternalReference {+ string system+ string external_id+ json metadata+ }+```++An external reference has three parts:++| Field | What it holds | Example |+|---|---|---|+| `system` | Which system the key belongs to | `erp`, `pim`, `netsuite` |+| `external_id` | The key that system knows the record by | `MAT-100` |+| `metadata` | Optional bookkeeping for the integration (a version, an etag, when it last synced) | `{ "etag": "W/\"42\"" }` |++Two rules keep the mapping unambiguous, and the database enforces both:++- **One key per system per record.** A product has at most one `erp` key.+- **One record per key.** Within a store, `erp:MAT-100` names exactly one product.++### System keys++A system key is a short lowercase name made of letters, digits and underscores, such as `erp`, `akeneo` or `legacy_pim`. Spree lowercases and trims what you send, so `ERP` and `erp` are the same system.++The system does not have to be a connected [integration](/developer/providers/overview). A nightly CSV from a legacy system that has no live connection still needs somewhere to record its keys. When a connector does exist, use the same name it uses, so the connector, its references and its settings page all agree.++## What can carry external references++| Record | Typical source of the key |+|---|---|+| Products | PIM or ERP item number |+| Variants | ERP or PIM SKU-level key |+| Categories | PIM category code |+| Media | DAM asset ID |+| Stock locations | ERP warehouse code |+| Stock levels | ERP inventory record |+| Orders | ERP sales order number, written back after the order is sent there |+| Companies | ERP or CRM account number (B2B) |+| Sellers | Payout provider account, recorded by the [payout provider](/developer/providers/payouts) itself |++Customers are deliberately absent. A customer account can shop in more than one store, so a store-level key for it has no single owner. In B2B, the buyer's ERP identity belongs on their [company](/developer/core-concepts/companies) instead.++## Writing references++Send `external_references` with any create or update on the Admin API. It accepts the same map the API returns:++<CodeGroup>++```typescript Admin SDK+const product = await adminClient.products.create({+ name: 'Canvas Tote',+ external_references: { erp: 'MAT-100', pim: 'SKU-88412' },+})+```++```bash CLI+spree api post /products -d '{+ "name": "Canvas Tote",+ "external_references": { "erp": "MAT-100", "pim": "SKU-88412" }+}'+```++</CodeGroup>++A list is accepted too, which some feeds find more natural:++```json+{+ "external_references": [+ { "system": "erp", "external_id": "MAT-100" },+ { "system": "pim", "external_id": "SKU-88412" }+ ]+}+```++What a write changes:++- **Systems you name are set.** A system the record has no key for gets one; a system it already has gets the new key.+- **Systems you don't name are left alone.** A PIM sending only `pim` never touches the `erp` key.+- **A blank key removes the reference.** Send `{ "erp": "" }` to unlink a record from the ERP.++## Reading references++Admin API responses for every record above except sellers include `external_references` as a flat map — the same shape you send:++```json Response+{+ "id": "prod_86Rf07xd4z",+ "name": "Canvas Tote",+ "external_references": {+ "erp": "MAT-100",+ "pim": "SKU-88412"+ }+}+```++External references never appear in the Store API. Which ERP a merchant runs, and the keys records have in it, is back-office information.++In the dashboard, references show in the **JSON** view on each record's detail page. There is no form for editing them: they are written by integrations and read by whoever is debugging a sync.++## Addressing records by your keys++For any record type listed in [What can carry external references](#what-can-carry-external-references), you can write `external:<system>:<key>` in the path instead of its Spree ID. An integration that only ever learned the ERP's key can then read, update and delete without first asking Spree for the record's ID.++<CodeGroup>++```typescript Admin SDK+const product = await adminClient.products.get('external:erp:MAT-100')++await adminClient.products.update('external:erp:MAT-100', {+ name: 'Canvas Tote — Natural',+})+```++```bash CLI+spree api get /products/external:erp:MAT-100++spree api patch /products/external:erp:MAT-100 -d '{+ "name": "Canvas Tote — Natural"+}'+```++</CodeGroup>++The lookup follows the same rules as a Spree ID. It only finds records in the current store, and an API key or admin user that cannot see the record gets a `404`, exactly as they would for its Spree ID.++<Warning>+Use this form only for keys made of letters, digits, dashes, underscores and colons. A key containing a slash or a dot cannot be part of a URL path; address that record by its Spree ID instead.+</Warning>++## Repeatable feeds++A create that names a key Spree already knows **updates that record instead of creating a new one**. A feed can therefore send every row on every run, and the result is the same as if it had sent each row once.++<CodeGroup>++```typescript Admin SDK+// First run: creates the product.+// Every later run: updates the same product.+await adminClient.products.create({+ name: 'Canvas Tote',+ external_references: { erp: 'MAT-100' },+})+```++```bash CLI+spree api post /products -d '{+ "name": "Canvas Tote",+ "external_references": { "erp": "MAT-100" }+}'+```++</CodeGroup>++The update needs the same permission an ordinary update would, so a key that may create records but not edit them cannot use a create to change one.++Image feeds work the same way. Media created from a URL with `external_references` is recorded under those keys, and a later run with the same key finds the existing image instead of adding a second copy.++## When a key is already taken++If an update names a key that already belongs to a **different** record, Spree keeps the record's other changes and answers with `422`:++```json Response+{+ "error": {+ "code": "conflicting_external_reference",+ "message": "External has already been taken"+ }+}+```++This usually means two records in Spree map to one record in the other system — for example, a product that was duplicated by hand. Remove the reference from one of them (send a blank key), then retry.++## Filtering companies by key++Companies can be filtered by their references, which helps when reconciling a list of accounts against an ERP:++```http+GET /api/v3/admin/companies?q[external_references_system_eq]=erp&q[external_references_external_id_eq]=ACME-1+```++For every other record, look it up by its key with the `external:<system>:<key>` path form above.++## References belong to a store++A reference is owned by the store its record belongs to. Two stores can each map `erp:MAT-100` to their own product without colliding, and a key from one store never finds a record in another. A feed that serves several stores sends its rows to each store separately.++## External references, custom fields or metadata?++All three can hold "an ID from another system", but only one is built for it.++| | External references | [Custom fields](/developer/core-concepts/metafields) | [Metadata](/developer/customization/metadata) |+|---|---|---|---|+| **For** | A record's key in another system | Data a merchant curates | Any data your code keeps |+| **Unique per store** | Yes, enforced | No | No |+| **Find a record by it** | Yes, in the URL path | Through filters | No |+| **Create-or-update by it** | Yes | No | No |+| **Several systems at once** | Yes, one key each | One field per system | Your own structure |+| **Store API** | Never | Configurable | Never |++If an integration needs to find a record again by a key it owns, use an external reference. If a merchant should see and edit the value, use a custom field. Anything else your code needs to remember belongs in metadata.++## Related++- [ERP](/developer/providers/erp) — feeding stock and orders from an ERP+- [PIM](/developer/providers/pim) — syncing product content from a PIM+- [Imports & Exports](/developer/core-concepts/imports-exports) — file-based feeds+- [Companies](/developer/core-concepts/companies) — B2B accounts and their ERP identity+- [Admin SDK](/developer/sdk/admin/resources) — the TypeScript client used in the examplesdiff --git a/docs/developer/core-concepts/metafields.mdx b/docs/developer/core-concepts/metafields.mdxindex a674b5f012..f80ade1f7e 100644--- a/docs/developer/core-concepts/metafields.mdx+++ b/docs/developer/core-concepts/metafields.mdx@@ -5,7 +5,7 @@ description: Add your own structured, typed data to products, orders and other r## Overview-Sooner or later you need to store something Spree doesn't have a field for. A fabric composition. A care instruction. A gift message. An ID from the system you sync with.+Sooner or later you need to store something Spree doesn't have a field for. A fabric composition. A care instruction. A gift message.Custom fields are how you do that without changing the database. You declare a field once — its name, its type, who can see it — and from then on it can be set on any record of that kind, edited in the dashboard, returned by the API, and searched or filtered like any built-in field.@@ -219,11 +219,12 @@ Spree has two ways to store your own data, and they are not competing — they s| **Visibility** | Configurable per field | Never read by the Store API || **Searchable** | Yes, opt in | Not through the product search |-Put it simply: **custom fields are for people, metadata is for machines.** If a merchant should type it, define a custom field. If it is a sync token or an external ID that only your integration reads, use metadata.+Put it simply: **custom fields are for people, metadata is for machines.** If a merchant should type it, define a custom field. If it is a sync token that only your integration reads, use metadata. If it is the key another system knows the record by, use an [external reference](/developer/core-concepts/external-references).## Related- [Metadata](/developer/customization/metadata) — the machine-readable alternative+- [External References](/developer/core-concepts/external-references) — keys from your ERP, PIM or CRM- [Products](/developer/core-concepts/products) — the most common place for custom fields- [Search & Filtering](/developer/core-concepts/search-filtering) — how filters and search work- [Admin SDK](/developer/sdk/admin/resources) — managing definitions and values in TypeScriptdiff --git a/docs/developer/how-to/build-a-b2b-store.mdx b/docs/developer/how-to/build-a-b2b-store.mdxindex 333d122c95..dcab1601e5 100644--- a/docs/developer/how-to/build-a-b2b-store.mdx+++ b/docs/developer/how-to/build-a-b2b-store.mdx@@ -937,7 +937,7 @@ To add your own B2B cards, such as a credit account or an ERP reference, the comThe B2B building blocks are ordinary Spree resources, so the usual customization paths apply:- **Events.** Companies, company invitations and catalogs publish lifecycle events, and orders publish `order.placed`. Subscribe to them to sync accounts and orders with an ERP or CRM. See [Events](/developer/core-concepts/events).-- **External references.** Companies accept `external_references`, so an ERP can address a company by its own account number.+- **[External references](/developer/core-concepts/external-references).** Companies accept `external_references`, so an ERP can address a company by its own account number.- **Tax identifier validators.** Register a validator that checks numbers against a live registry.- **Checkout requirements.** Register an extra requirement, such as "a cost center is required", and it appears in the cart's `requirements` next to the built-in ones. See [Checkout customization](/developer/customization/checkout).diff --git a/docs/developer/providers/erp.mdx b/docs/developer/providers/erp.mdxindex 7c6806321a..139264584d 100644--- a/docs/developer/providers/erp.mdx+++ b/docs/developer/providers/erp.mdx@@ -28,7 +28,7 @@ Two ways in, both writing stock movements rather than overwriting numbers — so## Addressing records by your keys-The same product is known to your ERP and your PIM under different keys. External references are store-scoped mappings that let any feed address a Spree record as `external:<system>:<id>` — a create that names a key Spree already knows updates that record instead of failing.+The same product is known to your ERP and your PIM under different keys. External references are store-scoped mappings that let any feed address a Spree record as `external:<system>:<id>` — a create that names a key Spree already knows updates that record instead of failing. See [External References](/developer/core-concepts/external-references).## Checkout holds and failure policydiff --git a/docs/developer/providers/payouts.mdx b/docs/developer/providers/payouts.mdxindex 4dcf74d6c3..ad40a57da6 100644--- a/docs/developer/providers/payouts.mdx+++ b/docs/developer/providers/payouts.mdx@@ -183,7 +183,7 @@ If your rails have no idempotency keys, look the movement up by that key (or by## Seller accounts-A seller's account with your provider is stored as an [external reference](/developer/providers/erp#addressing-records-by-your-keys) filed under your `reference_system`, so a marketplace that migrates between providers keeps every account on record:+A seller's account with your provider is stored as an [external reference](/developer/core-concepts/external-references) filed under your `reference_system`, so a marketplace that migrates between providers keeps every account on record:| Need | Call ||---|---|diff --git a/docs/developer/providers/pim.mdx b/docs/developer/providers/pim.mdxindex 1ffe37a112..c41fac4ef4 100644--- a/docs/developer/providers/pim.mdx+++ b/docs/developer/providers/pim.mdx@@ -13,7 +13,7 @@ Your PIM owns product content — names, descriptions, attributes, associations,## Product data feeds-Products, variants and prices flow in through the Admin API and CSV imports. Feeds address records by the keys your PIM already uses via external references (`external:<system>:<id>`), so an integration never has to store Spree IDs to update what it created.+Products, variants and prices flow in through the Admin API and CSV imports. Feeds address records by the keys your PIM already uses via [external references](/developer/core-concepts/external-references) (`external:<system>:<id>`), so an integration never has to store Spree IDs to update what it created.## The pricing providerdiff --git a/docs/docs.json b/docs/docs.jsonindex 47155ce078..f0127beeda 100644--- a/docs/docs.json+++ b/docs/docs.json@@ -701,6 +701,7 @@"developer/core-concepts/translations","developer/core-concepts/slugs","developer/core-concepts/metafields",+ "developer/core-concepts/external-references","developer/core-concepts/imports-exports","developer/core-concepts/reporting"]@@ -2164,6 +2165,7 @@"developer/core-concepts/translations","developer/core-concepts/slugs","developer/core-concepts/metafields",+ "developer/core-concepts/external-references","developer/core-concepts/imports-exports","developer/core-concepts/reporting"]