Keeping in Sync
To keep your own copy of a store’s data current, use webhooks for changes as they happen and a scheduled catch-up through the public API for anything a webhook did not deliver.
Webhooks first
Section titled “Webhooks first”Subscribe to the event types you mirror and apply each snapshot as it arrives, following Best Practices: deduplicate on the event id and skip events older than what you already hold. Most changes reach you this way within seconds.
Webhooks are not enough on their own. A delivery can fail all of its attempts, events sent while an endpoint is disabled are not replayed, and products have no webhook events.
Saved payment methods are the one thing the catch-up below cannot repair: they come only from customer.payment_method_changed, and no public route lists them. A delivery that failed all of its attempts is still in the delivery log, and an admin can Resend it from there. Events that happened while the endpoint was disabled were never sent to it or queued, so there is nothing to resend, and a payment method that changed in that window stays stale in your copy until it changes again.
Catch up on modifiedOn
Section titled “Catch up on modifiedOn”Every object you sync carries a modifiedOn that moves on each change, and each list route can filter and sort on it:
| Route | Rows |
|---|---|
GET /subscription/paged | subscriptions, without lines |
GET /customer/paged | customers, without addresses |
GET /order/paged | orders, with their lines |
GET /product/paged | products, without prices |
Each list sorts by modifiedOn and then by its id, so rows with the same modifiedOn always come back in the same order. Paging is by position, and modifiedOn is stamped when a change starts, not when it is saved. An order’s card is charged inside the step that saves the order, so an order can become visible a few seconds after its modifiedOn. The recipe below allows for both by leaving a 5-minute margin at each end of a run.
On a schedule, for example every 15 minutes:
- Work out this run’s window. Its end is the time the run starts minus 5 minutes. Its start is the last
modifiedOnyou recorded at the end of the previous run, minus 5 minutes. On the very first run, use any time before your oldest record. - Call the route with
modifiedOnStartandmodifiedOnEndset to that window,sortBy=modifiedOn,sortOrder=ASC, andperPage=100. Keep the samemodifiedOnEndfor every request in the run, so the run finishes even while new changes keep arriving; those belong to the next run. - Upsert each row by its id (
subscriptionId,storeCustomerId,orderId, orproductId), replacing the copy you hold unless yours has a newermodifiedOn. The overlap re-reads a few rows, which is harmless. Make the comparison and the write one atomic step, using the same conditional update your webhook handler uses (Best Practices), so a webhook that lands while the run is reading cannot be overwritten by an older row. - If the page was full, set
modifiedOnStartto the last row’smodifiedOnand ask for page 1 again. Repeat until a page comes back short. - Record the latest
modifiedOnthis run saw, for the next run’s start. If it saw no rows, keep the value you had.
Move modifiedOnStart forward rather than asking for page 2, 3, and so on. A fresh page 1 stays fast however much has changed, and a row that changes while you read moves past modifiedOnEnd instead of shifting the rows you have not read yet. The one exception: if every row on a full page has the same modifiedOn, which a bulk change can cause, page forward at that start until the timestamp changes. If one of those rows changes while you page, the rest of the group shifts by one and a row can be skipped. The next run’s 5-minute overlap reads that group again when it is close to where the run stopped. Otherwise the full pass below can pick it up, but it pages the same way, so it can miss the row again if the same thing happens while it reads.
Run a full pass now and then
Section titled “Run a full pass now and then”Once a day or once a week, walk each list from the beginning the same way, starting at a modifiedOnStart before your first record and ending at a modifiedOnEnd 5 minutes before the pass starts, and reconcile it with your copy. That repairs most of what a missed event and a missed catch-up both dropped, and finds rows you hold that may no longer exist. It pages the same way as the catch-up, so a row that changed while the pass was reading can be missing from it, and the next pass usually picks it up. Treat a row missing from a pass as a reason to check, not as proof: before you delete a subscription, customer, or product you hold because the pass did not return it, fetch it by id to confirm it is gone.
Fetch one record only when you need its detail
Section titled “Fetch one record only when you need its detail”The list rows leave out the heavier parts of an object. Fetch the full record by id only for rows you flag as changed and need in detail:
GET /subscription?subscriptionId=for the lines and recent renewal orders.GET /customer?storeCustomerId=for the saved addresses.GET /customer/enroller?storeCustomerId=Sandbox only for who enrolled the customer, as the store’s commission provider records it: name, referral handle and the id the provider knows the enroller by. It is read from the provider on each call. A change of enroller made in the provider does not move the customer’smodifiedOnand sends no event, so do not keep a copy; fetch it at the moment you need it, such as when you provision the customer elsewhere.isEnrollerKnown: falsemeans the provider could not be reached, so retry rather than treating it as “no enroller”.GET /product?productId=for every price and its volumes.
Order rows from GET /order/paged are already complete.
Next renewal orders
Section titled “Next renewal orders”Subscription objects show each subscription’s regular lines and when its next order runs (effectiveProcessingDate), but not the one-time changes made to that next order only: its own shipping address, card, shipping method or shipping price, or an item’s price, quantity or skip for that order. To see the next order exactly as it will run, call GET /subscription/upcoming-order/all?storeCustomerId=, and call it again for that customer when subscription.upcoming_order_changed, subscription.items_changed, subscription.processing_date_changed, or subscription.pricing_changed arrives. The last one covers a change of the customer’s tier: the next order is priced at the tier Open Shop has stored, so once that moves, its tier-following lines show the new prices (a one-time price set on the next order stays as set). Subscriptions that renew on the same day to the same address, shipping method and card are grouped into one order there, and an order due today is already being processed and is not listed.
Prices that follow a customer’s tier
Section titled “Prices that follow a customer’s tier”Subscription lines that follow the customer’s tier are priced at the tier Open Shop last stored for the customer. After a change of tier, for example a rank change in the commission system, that stored tier can lag by up to 24 hours. When it catches up, modifiedOn moves and subscription.pricing_changed is sent for each of the customer’s subscriptions with such lines that is not cancelled, paused and failed ones included, so the next catch-up picks them up.
When you need the price a customer would pay right now, call GET /customer/{storeCustomerId}/price-context. It returns the tier checkout would charge at that moment, resolved the same way checkout does. Where the tier depends on standing held by the commission provider, as it does for IDLife, that standing is cached for a short time: standing.fetchedOn says when it was last read from the provider, and standing.isStale is true when that read is older than the freshness window and a refresh is running in the background. The tier in the response is still the one checkout would charge. If the provider cannot be reached at all, the tier falls back to the customer’s account type.
Retry order creation safely
Section titled “Retry order creation safely”Send an Idempotency-Key header, such as a UUID, with every POST /order. The key is stored with the order it creates, so a repeat of the same request with the same key returns that order, without creating or charging it again, however long after and however the first call ended. That includes a timeout or a 5xx that came after the order was placed.
Retry a timeout, a network error, or a 5xx with the same key. When you are not sure whether an order was placed, resending the same request with the same key is how you find out. A request that created no order, such as a declined card, can also be retried with the same key. The same key with a different request gets 422, and a repeat that arrives while the first call is still running gets 409. Use a new key only for a new order.