Sync orders to your systems

Copy new orders into your accounting, fulfillment or analytics tools reliably by polling the Orders API.

If you want orders to flow into another system, such as accounting software or a warehouse tool, the dependable pattern is to poll the Orders API on a schedule and remember how far you have read. This guide shows how.

The approach

  1. Run a job every few minutes.
  2. Ask for orders created since the last time you looked.
  3. Save each order in your system, using the order id as a unique key so that repeats are harmless.
  4. Move your "last seen" timestamp forward only after all orders in the batch are saved.

Fetch new orders

Use from_ts, a Unix timestamp in seconds, to ask for orders created after your last checkpoint:

const API = process.env.WAFLE_API;
const headers = { 'X-Wafle-Store-Key': process.env.WAFLE_KEY };

async function fetchSince(fromTs) {
  const orders = [];
  for (let page = 1; ; page++) {
    const res = await fetch(
      `${API}/stores/your-store/orders?status=paid&from_ts=${fromTs}&per_page=100&page=${page}`,
      { headers },
    );
    if (res.status === 429) {
      const wait = Number(res.headers.get('retry-after') || 5);
      await new Promise((r) => setTimeout(r, wait * 1000));
      page--; // retry the same page
      continue;
    }
    if (!res.ok) throw new Error(`Orders request failed: ${res.status}`);
    const body = await res.json();
    const batch = body.orders ?? body;
    orders.push(...batch);
    if (batch.length < 100) break;
  }
  return orders;
}

Store orders idempotently

Use the order id as a unique key in your database. If the job runs twice, or overlaps with the previous run, the same order is written once.

import json
import sqlite3

db = sqlite3.connect("orders.db")
db.execute("create table if not exists orders (id integer primary key, payload text)")

def save(order):
    db.execute("insert or replace into orders (id, payload) values (?, ?)",
               (order["id"], json.dumps(order)))
    db.commit()

Overlap your windows

Query from a little before your last checkpoint, for example five minutes earlier, and rely on idempotent storage. This protects you from orders that become paid slightly after they are created.

Update when status changes

Orders move from paid to shipped to delivered. If your system needs the latest status, refresh recent orders on a slower schedule, for example the last seven days once an hour.

Be kind to the API

  • Poll no more often than you need, and use status and time filters.
  • Respect Retry-After on HTTP 429. See Errors and rate limits.
  • Use one key for this job, with only orders:read.
Was this helpful?

Need help with an integration? Contact support.