Sifr Wahid Developers

Guides

Sync products and stock from an ERP

The SKU is the link. Load once, upsert every item, copy pictures in the background, and move stock by delta.

The SKU is the link. Give every variant in the store the SKU your system uses. The design view, with a sequence diagram: Product and stock sync.

Scopes for this sync

Products and inventory: read and write. Add webhooks read and write to hear about the merchant's own changes.

  1. First load

    Read GET /products?per_page=100 page by page, passing meta.next_cursor as cursor until it is null, and keep each product and variant id against its SKU.

  2. Upsert each item

    • GET /products/sku/{sku}. A 404 means a new item: POST /products with the name and the variants (each with sku and price in halalas), weight_grams when it ships, and status: active to show it.
    • When found: PATCH /products/sku/{sku} with only the fields that changed (keys you do not send stay).
    • URL-encode the SKU in the path. A SKU with a / cannot be addressed there, because the path takes the SKU as one segment: find the product with GET /products?filter[sku]= and the encoded SKU, then use its id with PATCH /products/{id}.
    • Never send a quantity on a product: any quantity key is answered 422. Stock has its own path.
    • A product with sizes or colours: options with their values, and each variant's values holds one value key of every option.

    A variant you send replaces the saved one: send its sku, barcode and sale_price again, or they are cleared (hidden keeps its value when left out). options, variants and images each replace the whole set when sent.

    The fields of a product: Create product and Update product by SKU.

  3. Pictures

    Send public links in images (or POST /products/{id}/images with {url}). They are copied in the background: the answer lists the link in images_pending with status pending (and 202 for one picture), then it joins images and product.updated is sent. When the copy fails it turns failed with image_unreachable, too_many_images or copy_failed. A product has 10 pictures, and a store has at most 50 links waiting at once (beyond that, 429 images_queue_full with Retry-After). A copy job downloads for 60 seconds at most, so with many slow links some may end copy_failed: send them again in a new write.

  4. Stock by delta

    • Read the level: GET /inventory?sku={sku} (or variant_id), per branch. Branch ids come from GET /branches.
    • Each movement in your system: POST /inventory/adjustments with {"items":[{"sku":"…","mode":"increment","quantity":5}]} or decrement, up to 100 lines a call, applied all or nothing, with an Idempotency-Key from the movement number. Without branch_id it goes to the default branch.
    • mode: set makes the level an exact number: for a stock count only, because it erases what the store sold between your read and your write.
    • A decrement below zero: 409 insufficient_stock with available, and nothing is applied.
    HTTP
    POST /inventory/adjustments
    Idempotency-Key: odoo-move-48213
    
    {"items":[{"sku":"…","mode":"increment","quantity":5}]}

    The answer gives quantity_after per item. See Adjust stock.

  5. The merchant's own changes

    Subscribe to product.updated, product.created and product.deleted to mirror them in your system. See Webhooks.

Before you run it on the real store

  • Every variant's SKU in the store is the one in your system.
  • Prices are in halalas: 50 SAR is 5000.
  • Every stock movement sends an Idempotency-Key derived from the movement number.
  • set only for stock counts.