Prompt
How do I set up incremental sync for financial data from Xero API?
Latest observation
To set up incremental sync for financial data from the Xero API, the key idea is:
- Do one initial full sync
- Store a cursor/watermark
- On each run, request only records changed since that point
- Update your cursor after a successful sync
Xero’s API is a bit different depending on the object type. Some endpoints support If-Modified-Since, and others are better handled with date-based filters or a delta sync strategy.
1) Pick the data you want to sync
Common financial entities in Xero:
- Invoices
- Bills / Accounts Payable
- Payments
- Credit notes
- Bank transactions
- Contacts
- Accounts
- Reports / balances
Check each endpoint’s docs because incremental support varies.
2) Use a sync cursor
Store a per-entity cursor, usually:
last_successful_sync_attimestamp- optionally
last_seen_idfor tie-breaking - timezone in UTC
Example:
{
"invoices": {
"last_sync_at": "2026-11-03T10:15:00Z"
},
"contacts": {
"last_sync_at": "2026-11-03T10:15:00Z"
}
}
3) Use If-Modified-Since when supported
Xero APIs often support filtering by modified time using the If-Modified-Since HTTP header.
Example request:
GET /api.xro/2.0/Invoices HTTP/1.1
Authorization: Bearer <access_token>
xero-tenant-id: <tenant_id>
If-Modified-Since: 2026-11-03T10:15:00Z
This tells Xero to return only items modified after that timestamp.
Important notes
- Use UTC
- Keep a small overlap window, e.g. subtract 5–10 minutes from your last cursor, to avoid missing late-arriving updates
- Deduplicate on your side using Xero IDs + updated timestamps
4) If the endpoint doesn’t support modified-since
Some endpoints may require:
- A since/date filter
- Pagination through recent data
- Full refresh for small datasets
For example, if you can filter by UpdatedDateUTC or a date field, use that as the cursor logic.
Pseudo-query logic:
fetch records where UpdatedDateUTC > last_sync_at - overlap
5) Handle pagination
Xero responses may paginate. You need to loop until there are no more pages.
Pseudo-flow:
cursor = load_cursor("invoices")
since = cursor - overlap
page = 1
while True:
response = GET invoices with If-Modified-Since=since and page=page
items = response["Invoices"]
if not items:
break
upsert(items)
page += 1
If the API uses continuation tokens or another paging style, follow that mechanism instead.
6) Upsert records, don’t insert-only
Incremental sync should be idempotent.
For each object:
- Use Xero’s unique ID as the primary key
- Update existing rows if they already exist
- Insert new rows if not
Store:
xero_idupdated_at- raw payload if useful
- sync metadata
7) Advance the cursor only after success
Only update your saved last_sync_at after:
- all pages are processed
- all upserts succeed
- the transaction is committed
Best practice:
- set
new_cursor = max(updated_at seen in response) - save it after sync completes
- use an overlap next time
8) Suggested sync strategy
First sync
- Pull all historical data
- Store the highest
UpdatedDateUTC/modified timestamp
Subsequent syncs
- Request changes since last cursor minus overlap
- Upsert changed items
- Move cursor forward
Periodic reconciliation
- Run a full reconciliation occasionally in case of missed changes or API quirks
9) Example implementation pattern
Cursor logic
overlap_minutes = 10
last_sync = get_cursor("invoices") # e.g. 2026-11-03 10:15:00Z
since = last_sync - timedelta(minutes=overlap_minutes)
page = 1
max_seen = last_sync
while True:
resp = xero.get_invoices(if_modified_since=since, page=page)
invoices = resp.get("Invoices", [])
if not invoices:
break
for inv in invoices:
upsert_invoice(inv)
updated = parse_xero_time(inv["UpdatedDateUTC"])
if updated > max_seen:
max_seen = updated
page += 1
save_cursor("invoices", max_seen)
10) Things to watch out for
- Clock skew: use UTC and don’t rely on local time
- Overlapping updates: always dedupe
- Deleted records: some APIs don’t return deletes as “changed”; you may need special handling
- Soft deletes / statuses: financial systems often mark inactive/voided instead of deleting
- Rate limits: respect Xero API limits and retry with backoff
- Token refresh: ensure OAuth2 tokens are refreshed automatically
11) Recommended architecture
A robust setup usually looks like:
- Scheduler: cron / queue / airflow
- Sync job per tenant and entity
- Cursor store: database table or key-value store
- Staging tables: optional, for validation
- Upsert step into canonical tables
- Monitoring:
- last sync time
- row counts
- failures/retries
- lag
12) Minimal checklist
- OAuth2 + tenant ID working
- Full initial load done
- Per-entity cursor stored
- Incremental requests use
If-Modified-Sinceor equivalent - Pagination handled
- Upserts implemented
- Cursor advances only after success
- 5–10 minute overlap added
- Retries/backoff enabled
If you want, I can also provide:
- a Node.js example,
- a Python example, or
- a database schema for storing sync cursors.