{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://agentictradestandard.org/schemas/rest.schema.json",
  "title": "SACP REST bodies",
  "description": "Request and response bodies of the SACP REST profile. Added in SACP 1.3.0-draft. Amounts are decimal strings in major units with an ISO 4217 currency. Timestamps are RFC 3339 in UTC.",
  "$defs": {
    "Money": {
      "type": "object",
      "required": [
        "amount",
        "currency"
      ],
      "additionalProperties": false,
      "properties": {
        "amount": {
          "type": "string",
          "pattern": "^-?(0|[1-9][0-9]*)(\\.[0-9]+)?$",
          "description": "Decimal number as a string in major units. JSON numbers are not used for money."
        },
        "currency": {
          "type": "string",
          "pattern": "^[A-Z]{3}$",
          "description": "ISO 4217 alphabetic code."
        }
      }
    },
    "Timestamp": {
      "type": "string",
      "format": "date-time",
      "pattern": "Z$",
      "description": "RFC 3339 date-time in UTC with the suffix Z."
    },
    "ItemRef": {
      "type": "object",
      "required": [
        "id",
        "qty"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128
        },
        "qty": {
          "type": "integer",
          "minimum": 1,
          "maximum": 1000
        }
      }
    },
    "Destination": {
      "type": "object",
      "required": [
        "country"
      ],
      "additionalProperties": false,
      "properties": {
        "country": {
          "type": "string",
          "pattern": "^[A-Z]{2}$",
          "description": "ISO 3166-1 alpha-2."
        },
        "postcode": {
          "type": "string",
          "maxLength": 16,
          "description": "Only when the carrier needs it for a quote. Never a street address."
        }
      }
    },
    "Product": {
      "type": "object",
      "required": [
        "id",
        "name",
        "price",
        "availability"
      ],
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128
        },
        "name": {
          "type": "string"
        },
        "description": {
          "type": "string"
        },
        "gtin": {
          "type": "string",
          "pattern": "^[0-9]{8}$|^[0-9]{12,14}$"
        },
        "price": {
          "$ref": "#/$defs/Money"
        },
        "availability": {
          "enum": [
            "in_stock",
            "out_of_stock",
            "preorder",
            "unknown"
          ]
        },
        "url": {
          "type": "string",
          "format": "uri"
        },
        "attributes": {
          "type": "object",
          "additionalProperties": {
            "type": [
              "string",
              "number",
              "boolean"
            ]
          }
        }
      }
    },
    "SearchRequest": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "agent": {
          "$ref": "agent-lane.schema.json"
        },
        "query": {
          "type": "string",
          "maxLength": 500
        },
        "max_price": {
          "oneOf": [
            {
              "$ref": "#/$defs/Money"
            },
            {
              "type": "number",
              "deprecated": true,
              "description": "Deprecated since 1.3.0-draft. A shop SHOULD still accept it and read it as a price in the shop currency."
            }
          ]
        },
        "limit": {
          "type": "integer",
          "minimum": 1,
          "maximum": 100,
          "default": 20
        },
        "cursor": {
          "type": "string",
          "maxLength": 1024,
          "description": "Opaque. Copy next_cursor from the previous page."
        }
      }
    },
    "ProductList": {
      "type": "object",
      "required": [
        "items",
        "next_cursor"
      ],
      "additionalProperties": false,
      "properties": {
        "items": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/Product"
          }
        },
        "next_cursor": {
          "type": [
            "string",
            "null"
          ],
          "description": "null on the last page."
        },
        "total_estimate": {
          "type": "integer",
          "minimum": 0,
          "description": "Optional. Not exact."
        }
      }
    },
    "ShippingQuoteRequest": {
      "type": "object",
      "required": [
        "items",
        "destination"
      ],
      "additionalProperties": false,
      "properties": {
        "agent": {
          "$ref": "agent-lane.schema.json"
        },
        "items": {
          "type": "array",
          "minItems": 1,
          "maxItems": 100,
          "items": {
            "$ref": "#/$defs/ItemRef"
          }
        },
        "destination": {
          "$ref": "#/$defs/Destination"
        }
      }
    },
    "ShippingOption": {
      "type": "object",
      "required": [
        "method",
        "cost"
      ],
      "additionalProperties": false,
      "properties": {
        "method": {
          "type": "string"
        },
        "carrier": {
          "type": "string"
        },
        "cost": {
          "$ref": "#/$defs/Money"
        },
        "eta_date": {
          "type": "string",
          "format": "date"
        }
      }
    },
    "ShippingQuoteResponse": {
      "type": "object",
      "required": [
        "options"
      ],
      "additionalProperties": false,
      "properties": {
        "options": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/ShippingOption"
          }
        },
        "valid_until": {
          "$ref": "#/$defs/Timestamp"
        }
      }
    },
    "OfferRequest": {
      "type": "object",
      "required": [
        "item",
        "proposal"
      ],
      "additionalProperties": false,
      "properties": {
        "agent": {
          "$ref": "agent-lane.schema.json"
        },
        "item": {
          "$ref": "#/$defs/ItemRef"
        },
        "proposal": {
          "type": "object",
          "required": [
            "unit_price"
          ],
          "additionalProperties": false,
          "properties": {
            "unit_price": {
              "$ref": "#/$defs/Money"
            }
          }
        }
      }
    },
    "OfferResponse": {
      "type": "object",
      "required": [
        "status"
      ],
      "additionalProperties": false,
      "properties": {
        "status": {
          "enum": [
            "accepted",
            "countered",
            "rejected"
          ]
        },
        "counter": {
          "type": "object",
          "required": [
            "unit_price"
          ],
          "additionalProperties": false,
          "properties": {
            "unit_price": {
              "$ref": "#/$defs/Money"
            }
          }
        },
        "valid_until": {
          "$ref": "#/$defs/Timestamp"
        }
      }
    },
    "CollectionRequest": {
      "type": "object",
      "required": [
        "items"
      ],
      "additionalProperties": false,
      "properties": {
        "agent": {
          "$ref": "agent-lane.schema.json"
        },
        "items": {
          "type": "array",
          "minItems": 1,
          "maxItems": 100,
          "items": {
            "$ref": "#/$defs/ItemRef"
          }
        }
      }
    },
    "CollectionResponse": {
      "type": "object",
      "required": [
        "collection_id",
        "items",
        "subtotal"
      ],
      "additionalProperties": false,
      "properties": {
        "collection_id": {
          "type": "string"
        },
        "items": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "id",
              "qty",
              "unit_price"
            ],
            "additionalProperties": false,
            "properties": {
              "id": {
                "type": "string"
              },
              "qty": {
                "type": "integer"
              },
              "unit_price": {
                "$ref": "#/$defs/Money"
              }
            }
          }
        },
        "subtotal": {
          "$ref": "#/$defs/Money"
        },
        "expires_at": {
          "$ref": "#/$defs/Timestamp"
        }
      }
    },
    "ApprovedTerms": {
      "type": "object",
      "required": [
        "snapshot",
        "terms_hash"
      ],
      "additionalProperties": false,
      "description": "The terms snapshot the customer approved, its SHA-256 hash, and an optional detached JWS signature from the shop.",
      "properties": {
        "snapshot": {
          "$ref": "terms-snapshot.schema.json"
        },
        "terms_hash": {
          "$ref": "checkout-outcomes.schema.json#/$defs/TermsHash"
        },
        "quote_signature": {
          "type": "string"
        }
      }
    },
    "AuthorityPresentation": {
      "type": "object",
      "additionalProperties": false,
      "description": "The signed mandate and the signed approvals bound to the terms hash.",
      "properties": {
        "mandate": {
          "type": "string",
          "description": "Compact JWS whose payload follows mandate.schema.json."
        },
        "mandate_ref": {
          "type": "string",
          "format": "uri"
        },
        "approvals": {
          "type": "array",
          "items": {
            "type": "string",
            "description": "Compact JWS whose payload follows approval.schema.json."
          }
        }
      }
    },
    "Webhook": {
      "type": "object",
      "required": [
        "url"
      ],
      "additionalProperties": false,
      "properties": {
        "url": {
          "type": "string",
          "format": "uri",
          "pattern": "^https://",
          "description": "HTTPS only. The shop MUST NOT follow redirects and MUST NOT call private or link-local addresses."
        },
        "events": {
          "type": "array",
          "items": {
            "enum": [
              "checkout.confirmed",
              "checkout.expired",
              "checkout.cancelled",
              "checkout.failed"
            ]
          },
          "uniqueItems": true
        }
      }
    },
    "PrepareRequest": {
      "type": "object",
      "required": [
        "items"
      ],
      "additionalProperties": false,
      "properties": {
        "agent": {
          "$ref": "agent-lane.schema.json"
        },
        "collection_id": {
          "type": "string"
        },
        "items": {
          "type": "array",
          "minItems": 1,
          "maxItems": 100,
          "items": {
            "$ref": "#/$defs/ItemRef"
          }
        },
        "shipping": {
          "type": "object",
          "required": [
            "method",
            "destination"
          ],
          "additionalProperties": false,
          "properties": {
            "method": {
              "type": "string"
            },
            "destination": {
              "$ref": "#/$defs/Destination"
            }
          }
        },
        "payment_handoff": {
          "type": "string",
          "description": "A name from payment_handoff in the discovery document."
        },
        "approved_terms": {
          "$ref": "#/$defs/ApprovedTerms"
        },
        "authority": {
          "$ref": "#/$defs/AuthorityPresentation"
        },
        "webhook": {
          "$ref": "#/$defs/Webhook"
        }
      }
    },
    "Checkout": {
      "allOf": [
        {
          "$ref": "checkout-outcomes.schema.json#/$defs/PrepareOk"
        },
        {
          "required": [
            "agent_reference"
          ],
          "properties": {
            "agent_reference": true
          },
          "additionalProperties": true
        }
      ]
    },
    "ConfirmRequest": {
      "type": "object",
      "required": [
        "terms_hash"
      ],
      "additionalProperties": false,
      "properties": {
        "agent": {
          "$ref": "agent-lane.schema.json"
        },
        "terms_hash": {
          "$ref": "checkout-outcomes.schema.json#/$defs/TermsHash",
          "description": "The hash of the exact terms the agent commits to. It MUST equal terms_hash of the checkout the agent read."
        },
        "authority": {
          "$ref": "#/$defs/AuthorityPresentation"
        }
      }
    },
    "CancelRequest": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "agent": {
          "$ref": "agent-lane.schema.json"
        },
        "reason": {
          "type": "string",
          "maxLength": 500
        }
      }
    },
    "WebhookEvent": {
      "type": "object",
      "required": [
        "id",
        "type",
        "created",
        "checkout_id",
        "agent_reference",
        "data"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string",
          "minLength": 8,
          "maxLength": 128,
          "description": "Unique per event. Equal to the SACP-Webhook-Id header. The receiver deduplicates on it."
        },
        "type": {
          "enum": [
            "checkout.confirmed",
            "checkout.expired",
            "checkout.cancelled",
            "checkout.failed"
          ]
        },
        "created": {
          "$ref": "#/$defs/Timestamp"
        },
        "checkout_id": {
          "type": "string"
        },
        "agent_reference": {
          "$ref": "checkout-outcomes.schema.json#/$defs/AgentReference"
        },
        "data": {
          "type": "object",
          "required": [
            "checkout"
          ],
          "additionalProperties": false,
          "properties": {
            "checkout": {
              "$ref": "#/$defs/Checkout"
            }
          }
        }
      }
    },
    "Jwk": {
      "type": "object",
      "required": [
        "kty",
        "kid"
      ],
      "properties": {
        "kty": {
          "enum": [
            "OKP",
            "EC",
            "RSA"
          ]
        },
        "kid": {
          "type": "string",
          "description": "RFC 7638 JWK thumbprint (SHA-256, base64url)."
        },
        "use": {
          "enum": [
            "sig"
          ]
        },
        "alg": {
          "type": "string"
        },
        "crv": {
          "type": "string"
        },
        "x": {
          "type": "string"
        },
        "y": {
          "type": "string"
        },
        "n": {
          "type": "string"
        },
        "e": {
          "type": "string"
        }
      }
    },
    "Jwks": {
      "type": "object",
      "required": [
        "keys"
      ],
      "properties": {
        "keys": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/Jwk"
          }
        }
      }
    }
  }
}
