{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.fourbysix.co/docs/order.schema.json",
  "title": "FourBySix order",
  "description": "Request body for POST /v1/orders. Spec version 1.0.",
  "type": "object",
  "required": [
    "spec_version",
    "order_ref",
    "order_type",
    "items"
  ],
  "additionalProperties": false,
  "properties": {
    "spec_version": {
      "type": "string",
      "pattern": "^1\\.[0-9]+$",
      "description": "Accepts 1.x. Anything else is rejected.",
      "examples": [
        "1.0"
      ]
    },
    "order_ref": {
      "type": "string",
      "minLength": 1,
      "maxLength": 200,
      "description": "Your own order id. Unique per partner, and the idempotency key: resubmitting the same order_ref with an identical body replays the original response, while a different body is rejected with 409."
    },
    "order_type": {
      "enum": [
        "photo_print",
        "postcard"
      ]
    },
    "items": {
      "type": "array",
      "minItems": 1,
      "maxItems": 200,
      "description": "The per-account limit is reported by GET /v1/me as max_items_per_order; 200 is the default. NOT EXPRESSIBLE IN JSON SCHEMA, but enforced by the API: every item_ref must be unique within the order. Check that yourself before submitting.",
      "items": {
        "$ref": "#/$defs/item"
      }
    },
    "submitted_at": {
      "type": "string",
      "format": "date-time",
      "description": "ISO 8601. Defaults to receipt time."
    },
    "priority": {
      "enum": [
        "standard",
        "rush"
      ],
      "default": "standard"
    },
    "customer_ref": {
      "type": "string",
      "maxLength": 200,
      "description": "Opaque. Stored with the submitted order and never interpreted. Not returned by GET /v1/orders/{id}."
    },
    "shipping": {
      "type": "object",
      "description": "Where the package goes. Stored verbatim. Free-form on a photo_print order; REQUIRED on a postcard, in the shape of $defs/postcard_shipping, because it is printed on the envelope the card is mailed in."
    },
    "metadata": {
      "type": "object",
      "description": "Free-form. 4096 bytes maximum, serialized."
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "order_type": {
            "const": "postcard"
          }
        },
        "required": [
          "order_type"
        ]
      },
      "then": {
        "anyOf": [
          {
            "required": [
              "shipping"
            ],
            "properties": {
              "items": {
                "minItems": 1,
                "maxItems": 1,
                "items": {
                  "properties": {
                    "role": {
                      "const": "postcard_front"
                    },
                    "quantity": {
                      "const": 1
                    }
                  }
                }
              },
              "shipping": {
                "$ref": "#/$defs/postcard_shipping"
              }
            },
            "description": "One postcard_front, quantity 1, with the address in the strict postcard shape."
          },
          {
            "description": "The earlier shape, still accepted: one postcard_front and one postcard_back (your rendered back, which we read and set on ours). shipping free-form, as it was. The front's back may not also carry message, signature or voice.",
            "properties": {
              "items": {
                "minItems": 2,
                "maxItems": 2,
                "allOf": [
                  {
                    "contains": {
                      "properties": {
                        "role": {
                          "const": "postcard_front"
                        }
                      },
                      "required": [
                        "role"
                      ]
                    }
                  },
                  {
                    "contains": {
                      "properties": {
                        "role": {
                          "const": "postcard_back"
                        }
                      },
                      "required": [
                        "role"
                      ]
                    }
                  }
                ],
                "items": {
                  "properties": {
                    "back": {
                      "properties": {
                        "message": {
                          "type": "null"
                        },
                        "signature": {
                          "type": "null"
                        }
                      },
                      "not": {
                        "required": [
                          "voice"
                        ]
                      }
                    }
                  }
                }
              }
            }
          }
        ]
      }
    },
    {
      "if": {
        "properties": {
          "order_type": {
            "const": "photo_print"
          }
        },
        "required": [
          "order_type"
        ]
      },
      "then": {
        "properties": {
          "items": {
            "items": {
              "properties": {
                "role": {
                  "const": "print"
                }
              }
            }
          }
        }
      }
    }
  ],
  "$defs": {
    "item": {
      "type": "object",
      "required": [
        "item_ref",
        "role",
        "source",
        "print"
      ],
      "additionalProperties": false,
      "properties": {
        "item_ref": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200,
          "description": "Unique within this order."
        },
        "role": {
          "enum": [
            "print",
            "postcard_front",
            "postcard_back"
          ],
          "description": "postcard_back is the earlier postcard shape: your own rendered back, which we read rather than print."
        },
        "source": {
          "$ref": "#/$defs/source"
        },
        "quantity": {
          "type": "integer",
          "minimum": 1,
          "maximum": 1000,
          "default": 1
        },
        "print": {
          "$ref": "#/$defs/print"
        },
        "enhance": {
          "$ref": "#/$defs/enhance"
        },
        "back": {
          "$ref": "#/$defs/back"
        }
      },
      "if": {
        "properties": {
          "role": {
            "const": "postcard_front"
          }
        },
        "required": [
          "role"
        ]
      },
      "else": {
        "properties": {
          "back": {
            "properties": {
              "message": {
                "type": "null"
              },
              "signature": {
                "type": "null"
              }
            },
            "not": {
              "required": [
                "voice"
              ]
            }
          }
        },
        "description": "message, signature and voice are postcard only. On a print item message and signature may be null at most, and voice must be absent."
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "role": {
                "const": "postcard_back"
              }
            },
            "required": [
              "role"
            ]
          },
          "then": {
            "not": {
              "required": [
                "back"
              ]
            },
            "description": "A postcard_back is the back; `back` goes on the postcard_front."
          }
        }
      ]
    },
    "source": {
      "type": "object",
      "required": [
        "url"
      ],
      "additionalProperties": false,
      "properties": {
        "url": {
          "type": "string",
          "pattern": "^https://",
          "maxLength": 8192,
          "description": "https only. Normally a presigned URL on your own storage. It must still be valid when we fetch it, which is within seconds of this request: an expired URL is unrecoverable."
        },
        "content_type": {
          "type": "string",
          "maxLength": 128,
          "description": "Helps name the file when the URL path carries no extension."
        },
        "filename": {
          "type": "string",
          "maxLength": 255,
          "description": "Cosmetic, and a useful extension hint."
        },
        "sha256": {
          "type": "string",
          "pattern": "^[0-9a-fA-F]{64}$",
          "description": "If supplied, verified after download. A mismatch fails the item."
        }
      }
    },
    "print": {
      "type": "object",
      "required": [
        "size"
      ],
      "additionalProperties": true,
      "description": "Unknown keys are ACCEPTED here and stored verbatim, unlike everywhere else in this schema. That is the forward-compatibility hatch for print geometry. `finish` was required here until November 2026; the paper follows the product, so it is no longer a field, and one still sent rides that hatch.",
      "properties": {
        "size": {
          "enum": [
            "4x6"
          ]
        },
        "zoom": {
          "type": "number",
          "minimum": 1,
          "default": 1,
          "description": "1.0 (default) is the centre crop, the most of the photo the print's shape holds. 2.0 is twice the magnification. Below 1 is clamped to 1."
        },
        "center": {
          "type": "object",
          "additionalProperties": false,
          "description": "Where the middle of the print lands on the photo, as fractions. Defaults to the middle. Clamped so the frame stays on the image.",
          "properties": {
            "x": {
              "type": "number",
              "minimum": 0,
              "maximum": 1
            },
            "y": {
              "type": "number",
              "minimum": 0,
              "maximum": 1
            }
          }
        }
      }
    },
    "enhance": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "profile": {
          "enum": [
            "default",
            "none"
          ],
          "description": "default enhances the photo; none prints it as sent (still cropped to the print). Defaults to none for postcard_back and default for everything else."
        }
      }
    },
    "back": {
      "type": "object",
      "additionalProperties": false,
      "description": "What goes on the back we print, on a photo print or a postcard: where the QR code points (your own page), whether the back may show where the photo was taken, a name printed beside ours, and on a postcard the message. Absent, {} or a null qr_url means no QR code; we host no page of our own.",
      "properties": {
        "qr_url": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 100,
          "pattern": "^(?!.*\\s)https://[A-Za-z0-9\\-._~:\\[\\]@!$&'()*+,;=%][A-Za-z0-9\\-._~:/?#\\[\\]@!$&'()*+,;=%]*$",
          "description": "A string is encoded into the QR exactly as sent. null (or absent) prints no QR code and no caption. https only, at most 100 characters (the code is printed at a fixed 0.9 inch, and longer stops scanning reliably), RFC 3986 URL characters only: percent-encode anything else. The leading lookahead is only there so validators whose $ also matches before a trailing newline (Python's re) still reject one.",
          "examples": [
            "https://example.com/p/k3x9q2",
            null
          ]
        },
        "location": {
          "type": "boolean",
          "description": "false leaves the map, place name and coordinates off the back even when the file has GPS; the panel gets lines to write on instead and the date stays. Absent or true is the back with its map.",
          "examples": [
            false
          ]
        },
        "cobrand": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 24,
          "pattern": "^(?!.*\\n)[A-Za-z0-9](?:[A-Za-z0-9 &'.-]*[A-Za-z0-9.])?$",
          "description": "A name printed beside ours at the foot of the back: PRINTED BY FOURBYSIX ⚭ WITH NAME, in capitals. At most 24 characters; letters, digits, spaces and & ' . - only, starting and ending with a letter or digit. null or absent uses the name on your account, if any.",
          "examples": [
            "CHARLESTOWN PLAYHOUSE",
            null
          ]
        },
        "message": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 400,
          "pattern": "^[\\u0020-\\u007E\\u00A0-\\u017F\\u2010-\\u2027\\u2030-\\u203A\\u20AC]*(?:\\n[\\u0020-\\u007E\\u00A0-\\u017F\\u2010-\\u2027\\u2030-\\u203A\\u20AC]*){0,7}$",
          "description": "Postcard only. Printed beside the map, as sent. At most 400 characters and 8 lines (\\n starts a line). Latin letters with accents, digits and ordinary punctuation only: no emoji or other symbols, which the card's typeface cannot draw. The API also accepts \\r\\n line breaks and stores them as \\n; this schema does not.",
          "examples": [
            "Dear Ava,\n\nThe garden is finally blooming. We miss you!"
          ]
        },
        "signature": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 40,
          "pattern": "^[\\u0020-\\u007E\\u00A0-\\u017F\\u2010-\\u2027\\u2030-\\u203A\\u20AC]*$",
          "description": "Postcard only. One line under the message, in bold. At most 40 characters, same characters as message, no line breaks.",
          "examples": [
            "Love, Grandma"
          ]
        },
        "voice": {
          "type": "boolean",
          "description": "Postcard only. true when the page qr_url opens has a voice recording on it: the caption under the code then says so. Needs qr_url. We never receive the recording."
        }
      },
      "if": {
        "properties": {
          "voice": {
            "const": true
          }
        },
        "required": [
          "voice"
        ]
      },
      "then": {
        "required": [
          "qr_url"
        ],
        "properties": {
          "qr_url": {
            "type": "string"
          }
        }
      }
    },
    "postcard_shipping": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "name",
        "line1",
        "city",
        "postal_code",
        "country"
      ],
      "description": "A postcard's address, printed on its envelope and nowhere else. Every field one line of at most 100 characters. region (the state) is required when country is US.",
      "properties": {
        "name": {
          "type": "string",
          "maxLength": 100,
          "pattern": "^[^\\n]*\\S[^\\n]*$"
        },
        "line1": {
          "type": "string",
          "maxLength": 100,
          "pattern": "^[^\\n]*\\S[^\\n]*$"
        },
        "line2": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 100,
          "pattern": "^[^\\n]*\\S[^\\n]*$"
        },
        "city": {
          "type": "string",
          "maxLength": 100,
          "pattern": "^[^\\n]*\\S[^\\n]*$"
        },
        "region": {
          "type": [
            "string",
            "null"
          ],
          "maxLength": 100,
          "pattern": "^[^\\n]*\\S[^\\n]*$"
        },
        "postal_code": {
          "type": "string",
          "maxLength": 100,
          "pattern": "^[^\\n]*\\S[^\\n]*$"
        },
        "country": {
          "type": "string",
          "maxLength": 100,
          "pattern": "^[^\\n]*\\S[^\\n]*$"
        }
      },
      "if": {
        "properties": {
          "country": {
            "enum": [
              "US",
              "USA",
              "us",
              "usa"
            ]
          }
        },
        "required": [
          "country"
        ]
      },
      "then": {
        "required": [
          "region"
        ],
        "properties": {
          "region": {
            "type": "string"
          }
        }
      }
    }
  }
}
