Amazon SP-API
Purpose
Orders, catalog, inventory feeds, and settlements for channel type AMAZON.
Authentication
LWA refresh token in the vault. App-level SP-API client id and secret in env. Region and marketplace id are required once the client id is set.
Authorize URL and callback routes live on amazon.controller.ts. The callback that stores Seller Central authorization is JWT-protected in the inventory that was read, not @PublicRoute.
Sellers must re-authorize an SP-API app every 365 days. The callback records authorizedAt and refreshTokenExpiresAt (authorization + 365 days) in the connection's config, so ConnectionTokenExpiryScheduler flags it 21 days ahead and marks it EXPIRED after. Authorizing again clears the warning.
Orders API v2026-01-01 returns buyer and recipient details by app role, not restricted data token: RECIPIENT needs the Direct-to-Consumer Delivery role. Without it Amazon blanks the address. The recipient goes through the shared address handler; a later copy with no street line (Amazon withholds it once the PII window closes) never replaces the full address.
Direction
No dedicated Amazon webhook controller was found. Orders are pulled. Inventory uses an async feed: submit, persist ChannelInventoryFeedSubmission, poll with channel-feed-poll. ChannelInventoryState updates when the poll result is read.
Orders
AmazonOrderService.syncOrders, run by the ORDER sync job, pages searchOrders (Orders v2026-01-01) by lastUpdatedAfter. That operation allows one call per three minutes after a burst of 20, so:
- Pages are 100 orders, Amazon's maximum.
- Each window starts 5 minutes behind
lastOrderSync, since Amazon asks for "before" bounds at least two minutes in the past. - A 429 ends the run without failing it. The page token (valid 24 hours) and the window are saved in the cache (
amazon:order-sync:checkpoint:<channelId>), and the next run carries on with the same query.lastOrderSyncmoves to where the window began only once its last page is read. - An order that fails to import does not hold the cursor. Its id is kept (
amazon:order-sync:failed:<channelId>, 7 days) and re-read withgetOrder(burst 30) at the start of the next run, up to 25 a run. - Fetching one order by id (a payout naming an order we never imported) leaves the cursor and checkpoint alone.
The request includes PACKAGES and CANCELLATION besides buyer, recipient, fulfillment, proceeds and payment. Amazon's fulfillmentStatus stops at SHIPPED, so after that the order follows its packages: all DELIVERED makes it DELIVERED; all with detailed status RETURNED_TO_SELLER makes it RETURN_RECEIVED; UNDELIVERABLE makes it RETURN_REQUESTED; delivered and returned parcels together make it PARTIALLY_RETURNED. Each line takes its own packages' state, and a line Amazon cancelled is CANCELLED.
An unshipped order with an open buyer cancellation request is held (ON_HOLD, reason Buyer requested cancellation) until the request is gone. Channel status cannot move an order that already has a fulfillment.
Each package is kept in Order.config.channelTracking (carrier, tracking number, status, ship time, line ids), the shape Flipkart writes too, and returned on the order as channelTracking. The admin order page shows it as "Marketplace shipment". This is the tracking for orders shipped from Seller Central, which have no PointNXT shipment. For an order Amazon labels (Easy Ship), each package with a tracking number also raises order.channel-awb.reported, which records the AWB on a fulfillment that is being packed, if one exists.
Each run logs its call count, whether the window was new or resumed and completed, and its counts.
Flush job: flush-channel:AMAZON.
Settlements: sync job type settlement, file parsers, and POST .../settlements/sync with SETTLEMENT:CREATE.
The API sync reads Finances 2024-06-19 listTransactions and then Finances v0 listFinancialEventGroups. Each payout is stamped with its period and fund-transfer date, and once the group is Closed its OriginalTotal becomes the declared payout total for the control-total check. Order-less transactions (service fees, advertising, reserves) are loaded as ADJUSTMENT groups; the payout transfer itself is excluded. The event-group id is stored on the settlement as channelReportId.
File uploads accept both the current GET_V2_SETTLEMENT_REPORT_DATA_FLAT_FILE_V2 layout (amount-type / amount-description / amount) and the legacy flat file, which Amazon removes on 2026-11-11.
Fulfillment mode
Schema comment: Amazon AFN versus MFN is per order. Channel defaultFulfillmentMode defaults to OMS_MANAGED and is a fallback.
Unknown: whether AFN orders skip pick and pack, and where that branch is.
Code
src/integrations/amazon.
Failure
Feed polls that stick are requeued every 10 minutes by ChannelFeedPollReaperScheduler. Sync failures use the shared circuit breaker.
Idempotency
Feed submission ids are stored. Order natural keys were not fully traced.