{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/merchantflow-ai/json-schema/merchantflow-ai-order-schema.json",
  "title": "Order",
  "x-generated": "2026-11-18",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/merchantflow-ai-openapi.json#/components/schemas/Order",
  "type": "object",
  "properties": {
    "id": {
      "type": "string",
      "description": "MerchantFlow order id."
    },
    "external_order_id": {
      "type": "string",
      "description": "The commerce platform's own order id. For Shopify this is the NUMERIC legacy id, e.g. \"5123456789012\"."
    },
    "external_order_gid": {
      "type": [
        "string",
        "null"
      ],
      "description": "Shopify GraphQL global id, derived from external_order_id. Null for non-Shopify stores."
    },
    "order_number": {
      "type": "string"
    },
    "platform": {
      "type": "string"
    },
    "store_id": {
      "type": "string"
    },
    "created_at": {
      "type": "string",
      "format": "date-time",
      "description": "When the order was placed."
    },
    "updated_at": {
      "type": "string",
      "format": "date-time",
      "description": "The value updated_after compares against. Advances on financial changes, not only edits."
    },
    "canceled_at": {
      "type": [
        "string",
        "null"
      ],
      "format": "date-time"
    },
    "refunded_at": {
      "type": [
        "string",
        "null"
      ],
      "format": "date-time"
    },
    "currency": {
      "type": "string"
    },
    "financial_status": {
      "type": "string"
    },
    "fulfillment_status": {
      "type": [
        "string",
        "null"
      ]
    },
    "sales_channel": {
      "type": "string",
      "enum": [
        "d2c",
        "b2b"
      ]
    },
    "is_first_order": {
      "type": "boolean",
      "description": "True when this is the customer's first order with this store."
    },
    "customer_order_sequence": {
      "type": [
        "integer",
        "null"
      ],
      "description": "1 for a first purchase, 2 for the second, and so on. Distinguishes new from repeat buyers with no identifier at all."
    },
    "is_subscription_renewal": {
      "type": "boolean"
    },
    "economics": {
      "$ref": "#/$defs/OrderEconomics"
    },
    "attribution": {
      "$ref": "#/$defs/OrderAttribution"
    },
    "shipping_country_code": {
      "type": [
        "string",
        "null"
      ],
      "description": "ISO 3166-1 alpha-2. The only location data returned."
    },
    "customer_ref": {
      "type": [
        "string",
        "null"
      ],
      "description": "Pseudonymous customer identifier. Stable for your application and this merchant, so you can group a customer's orders. Deliberately NOT stable across applications, and not reversible to an email address. Null for orders with no customer email.  This is pseudonymous personal data under GDPR, not anonymous data. Handle it accordingly and do not attempt re-identification."
    },
    "line_items": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/OrderLineItem"
      }
    }
  },
  "$defs": {
    "OrderAttribution": {
      "type": "object",
      "properties": {
        "primary_source": {
          "type": [
            "string",
            "null"
          ]
        },
        "is_paid": {
          "type": "boolean"
        },
        "utm_source": {
          "type": [
            "string",
            "null"
          ]
        },
        "utm_medium": {
          "type": [
            "string",
            "null"
          ]
        },
        "utm_campaign": {
          "type": [
            "string",
            "null"
          ]
        },
        "utm_term": {
          "type": [
            "string",
            "null"
          ]
        },
        "utm_content": {
          "type": [
            "string",
            "null"
          ]
        },
        "landing_page": {
          "type": [
            "string",
            "null"
          ],
          "description": "Landing URL with query parameters, fragments, and URL credentials removed."
        },
        "referrer": {
          "type": [
            "string",
            "null"
          ],
          "description": "Referring hostname only. Paths, query parameters, fragments, and URL credentials are removed."
        },
        "device_type": {
          "type": [
            "string",
            "null"
          ]
        }
      }
    },
    "OrderEconomics": {
      "type": "object",
      "properties": {
        "gross_revenue": {
          "type": "number",
          "description": "Amount in the order currency."
        },
        "shipping_revenue": {
          "type": "number",
          "description": "Amount in the order currency."
        },
        "discount": {
          "type": "number",
          "description": "Amount in the order currency."
        },
        "tax": {
          "type": "number",
          "description": "Amount in the order currency."
        },
        "tax_included_in_price": {
          "type": "boolean"
        },
        "refunds": {
          "type": "number",
          "description": "Amount in the order currency."
        },
        "net_revenue": {
          "type": "number",
          "description": "gross_revenue minus refunds."
        },
        "cogs": {
          "type": "number",
          "description": "Amount in the order currency."
        },
        "cogs_source": {
          "type": "string",
          "description": "`cogs_map` (resolved from the merchant's cost table) or `3pl_actual` (billed by the fulfilment provider)."
        },
        "cogs_coverage_pct": {
          "type": [
            "number",
            "null"
          ],
          "description": "Percentage of units on this order that have cost data. Below 100 means `cogs` understates true cost and margin is overstated. Null when the order has no line items."
        },
        "fulfillment_cost": {
          "type": [
            "number",
            "null"
          ]
        },
        "fulfillment_cost_source": {
          "type": [
            "string",
            "null"
          ]
        },
        "payment_fee": {
          "type": [
            "number",
            "null"
          ],
          "description": "Payment processing fee for this order. See payment_fee_source - it may be an estimate from the merchant's payment fee rules rather than a fee the processor reported. Null when no fee is known."
        },
        "payment_fee_source": {
          "type": [
            "string",
            "null"
          ],
          "enum": [
            "platform",
            "rule",
            "manual",
            null
          ],
          "description": "Where payment_fee came from. `platform`: reported by the commerce platform or payment processor. `rule`: estimated from the merchant's payment fee rule for this gateway (percentage plus fixed amount), because the platform reported no fee - label it as an estimate when displayed. `manual`: entered by the merchant on a manual order. Null when payment_fee is null."
        },
        "ad_spend_allocated": {
          "type": "number",
          "description": "This order's share of the day's ad spend. See ad_spend_basis - it is an allocation, not measured spend."
        },
        "ad_spend_basis": {
          "type": "string",
          "const": "blended_daily_allocation",
          "description": "How ad_spend_allocated was derived. A day's total spend is divided across that day's eligible orders; no ad platform reports spend per order. Do not present this as measured per-order or per-funnel ad spend."
        },
        "contribution_profit": {
          "type": "number",
          "description": "net_revenue - cogs - ad_spend_allocated - payment_fee - fulfillment_cost."
        },
        "contribution_margin_pct": {
          "type": "number"
        },
        "allocated_overhead": {
          "type": "number",
          "description": "This order's share of operating expenses not already deducted above."
        },
        "net_profit": {
          "type": "number",
          "description": "contribution_profit - allocated_overhead."
        },
        "net_margin_pct": {
          "type": "number"
        }
      }
    },
    "OrderLineItem": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string"
        },
        "sku": {
          "type": [
            "string",
            "null"
          ]
        },
        "name": {
          "type": "string"
        },
        "product_id": {
          "type": [
            "string",
            "null"
          ]
        },
        "variant_id": {
          "type": [
            "string",
            "null"
          ]
        },
        "quantity": {
          "type": "integer"
        },
        "price": {
          "type": "number",
          "description": "Unit price before discount."
        },
        "total": {
          "type": "number",
          "description": "Post-discount line revenue. Legitimately 0 for a fully discounted line such as a warranty replacement."
        },
        "cogs": {
          "type": "number",
          "description": "Amount in the order currency."
        },
        "fulfillment_cost": {
          "type": "number",
          "description": "Amount in the order currency."
        },
        "refund_amount": {
          "type": "number",
          "description": "Amount in the order currency."
        },
        "refunded_quantity": {
          "type": "integer"
        }
      }
    }
  }
}