{"openapi":"3.1.0","info":{"title":"GET /api/v1/inventory/reports/margin-by-sku","version":"1.0.0","description":"Margin-by-SKU report (actual revenue and booked COGS)"},"servers":[{"url":"https://api.ondayzero.com","description":"Production"}],"paths":{"/api/v1/inventory/reports/margin-by-sku":{"get":{"tags":["inventory:reports"],"summary":"Margin-by-SKU report (actual revenue and booked COGS)","description":"Returns per-variant actual revenue and booked COGS from Shopify and invoice lines over the lookback window, plus current price, landed cost and per-unit margin. Powers the Landed COGS 'Truth' tab.","operationId":"get_margin_by_sku","parameters":[{"name":"lookback_days","in":"query","required":false,"schema":{"type":"integer","maximum":365,"minimum":1,"description":"Trailing window (days) used to compute revenue + units sold.","default":30,"title":"Lookback Days"},"description":"Trailing window (days) used to compute revenue + units sold."},{"name":"limit","in":"query","required":false,"schema":{"type":"integer","maximum":1000,"minimum":1,"description":"Max variants returned (sorted by trailing revenue desc).","default":100,"title":"Limit"},"description":"Max variants returned (sorted by trailing revenue desc)."},{"name":"authorization","in":"header","required":false,"schema":{"anyOf":[{"type":"string"},{"type":"null"}],"title":"Authorization"}},{"name":"x-business-id","in":"header","required":false,"schema":{"anyOf":[{"type":"string"},{"type":"null"}],"title":"X-Business-Id"}}],"responses":{"200":{"description":"Successful Response","content":{"application/json":{"schema":{"$ref":"#/components/schemas/SuccessEnvelope_MarginByVariantResponse_"}}}},"401":{"description":"Unauthorized - Missing or expired token, an MCP OAuth token used against the REST API, or no access to this business","content":{"application/json":{"example":{"error":"unauthorized","message":"Authentication required","code":"AUTH_001","request_id":"3f0e7c2a-9b4d-4e1a-8c6f-2d5b7a1e9c30"},"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"403":{"description":"Forbidden - Insufficient permissions","content":{"application/json":{"example":{"error":"forbidden","message":"You don't have permission to access this resource","code":"AUTH_002","request_id":"3f0e7c2a-9b4d-4e1a-8c6f-2d5b7a1e9c30"},"schema":{"$ref":"#/components/schemas/ErrorResponse"}}}},"422":{"description":"Validation Error","content":{"application/json":{"schema":{"$ref":"#/components/schemas/ErrorResponse"},"example":{"error":"validation_error","message":"Validation failed: amount: Input should be a valid integer","errors":{"amount":"Input should be a valid integer"},"code":"GEN_002","request_id":"3f0e7c2a-9b4d-4e1a-8c6f-2d5b7a1e9c30"}}}}},"x-fastapi-operation-id":"get_margin_by_sku_api_v1_inventory_reports_margin_by_sku_get"}}},"components":{"schemas":{"MarginByVariantResponse":{"properties":{"rows":{"items":{"$ref":"#/components/schemas/MarginRow"},"type":"array","title":"Rows"},"lookback_days":{"type":"integer","title":"Lookback Days","default":30},"totals":{"$ref":"#/components/schemas/MarginTotals"}},"type":"object","title":"MarginByVariantResponse"},"MarginLayerBreakdown":{"properties":{"material":{"type":"integer","title":"Material","default":0},"packaging":{"type":"integer","title":"Packaging","default":0},"processing":{"type":"integer","title":"Processing","default":0},"freight":{"type":"integer","title":"Freight","default":0},"duties":{"type":"integer","title":"Duties","default":0},"handling":{"type":"integer","title":"Handling","default":0}},"type":"object","title":"MarginLayerBreakdown","description":"Six-layer landed-cost breakdown (cents per unit)."},"MarginRow":{"properties":{"variant_id":{"type":"string","title":"Variant Id"},"product_id":{"anyOf":[{"type":"string"},{"type":"null"}],"title":"Product Id"},"product_name":{"anyOf":[{"type":"string"},{"type":"null"}],"title":"Product Name"},"variant_name":{"anyOf":[{"type":"string"},{"type":"null"}],"title":"Variant Name"},"sku":{"anyOf":[{"type":"string"},{"type":"null"}],"title":"Sku"},"price_cents":{"type":"integer","title":"Price Cents"},"landed_cost_cents":{"type":"integer","title":"Landed Cost Cents"},"margin_cents":{"type":"integer","title":"Margin Cents"},"margin_pct":{"anyOf":[{"type":"number"},{"type":"null"}],"title":"Margin Pct"},"layers":{"$ref":"#/components/schemas/MarginLayerBreakdown"},"units_sold":{"type":"integer","title":"Units Sold","description":"Units sold in the window (net of returns).","default":0},"revenue_cents":{"type":"integer","title":"Revenue Cents","description":"Actual net line revenue in the window (Shopify + invoices).","default":0},"cogs_cents":{"type":"integer","title":"Cogs Cents","description":"COGS booked on those sales lines.","default":0},"gross_margin_cents":{"type":"integer","title":"Gross Margin Cents","description":"revenue_cents - cogs_cents.","default":0},"gross_margin_pct":{"anyOf":[{"type":"number"},{"type":"null"}],"title":"Gross Margin Pct"},"cogs_missing_units":{"type":"integer","title":"Cogs Missing Units","description":"Units sold with no cost recorded on the sale line.","default":0},"shopify_units":{"type":"integer","title":"Shopify Units","default":0},"invoice_units":{"type":"integer","title":"Invoice Units","default":0},"daily_velocity":{"type":"number","title":"Daily Velocity","default":0.0},"snapshot_month":{"anyOf":[{"type":"string"},{"type":"null"}],"title":"Snapshot Month"},"snapshot_source":{"anyOf":[{"type":"string"},{"type":"null"}],"title":"Snapshot Source"}},"type":"object","required":["variant_id","price_cents","landed_cost_cents","margin_cents","layers"],"title":"MarginRow","description":"Per-SKU margin row for the margin-by-SKU report."},"MarginTotals":{"properties":{"revenue_cents":{"type":"integer","title":"Revenue Cents","default":0},"cogs_cents":{"type":"integer","title":"Cogs Cents","default":0},"gross_margin_cents":{"type":"integer","title":"Gross Margin Cents","default":0},"units_sold":{"type":"integer","title":"Units Sold","default":0},"cogs_missing_units":{"type":"integer","title":"Cogs Missing Units","default":0}},"type":"object","title":"MarginTotals"},"SuccessEnvelope_MarginByVariantResponse_":{"properties":{"success":{"type":"boolean","title":"Success","default":true},"message":{"anyOf":[{"type":"string"},{"type":"null"}],"title":"Message"},"code":{"anyOf":[{"type":"string"},{"type":"null"}],"title":"Code"},"data":{"anyOf":[{"$ref":"#/components/schemas/MarginByVariantResponse"},{"type":"null"}]}},"additionalProperties":true,"type":"object","title":"SuccessEnvelope[MarginByVariantResponse]"},"ErrorResponse":{"title":"ErrorResponse","type":"object","description":"Envelope returned by every non-2xx response. Branch on `code` (stable) rather than `message` (human-readable, may change).","required":["error","message","code"],"properties":{"error":{"type":"string","description":"Error category: `validation_error`, `unauthorized`, `forbidden`, `not_found`, `conflict`, `rate_limited`, `server_error`, or `service_unavailable`."},"message":{"type":"string","description":"Human-readable explanation, safe to show to end users."},"code":{"type":"string","description":"Stable machine-readable code in `CATEGORY_NNN` form (e.g. `NOT_FOUND_006`, `AUTH_010`, `GEN_002`)."},"request_id":{"type":"string","description":"Correlation id for support requests. Echoes the `x-request-id` request header when one was supplied."},"errors":{"type":"object","additionalProperties":{"type":"string"},"description":"Field-level validation messages keyed by field name. Present on 400/422 validation failures only."}}}},"securitySchemes":{"BearerAuth":{"type":"http","scheme":"bearer","bearerFormat":"API Token","description":"API token authentication. Format: `Bearer dz_...`"}}},"security":[{"BearerAuth":[]}]}