{
  "openapi": "3.1.0",
  "info": {
    "title": "mailer. API",
    "version": "1.1.0",
    "summary": "Send real paper mail in Canada with an API call.",
    "description": "Upload an A4 PDF (max 5 pages) and a Canadian address. mailer. prints it in black and white, puts it in an envelope with your return address, and hands it to Canada Post. $10 CAD per standard envelope, cheaper in bulk. A free sandbox key never mails anything. Operated by Websystems Inc., Toronto, Ontario.",
    "termsOfService": "https://mailer.websystems.co/terms",
    "contact": { "email": "support@websystems.co", "url": "https://mailer.websystems.co/agents" }
  },
  "servers": [{ "url": "https://mailer.websystems.co" }],
  "security": [{ "bearer": [] }],
  "paths": {
    "/v1/pricing": {
      "get": {
        "operationId": "getPricing",
        "summary": "Current prices, limits and bulk packs (no auth).",
        "security": [],
        "responses": { "200": { "description": "Price list", "content": { "application/json": { "schema": { "type": "object" } } } } }
      }
    },
    "/api/keys/test": {
      "post": {
        "operationId": "createSandboxKey",
        "summary": "Get a free sandbox key. Nothing is printed, mailed or charged.",
        "security": [],
        "responses": { "201": { "description": "Key (shown once)", "content": { "application/json": { "schema": { "type": "object", "properties": { "secret_key": { "type": "string" }, "mode": { "type": "string", "const": "test" } } } } } } }
      }
    },
    "/api/keys/checkout": {
      "post": {
        "operationId": "buyCredits",
        "summary": "Start a credit purchase. Returns a Stripe Checkout URL for a human to pay; a live key is emailed to them.",
        "security": [],
        "requestBody": { "required": true, "content": { "application/json": { "schema": { "type": "object", "required": ["email"], "properties": {
          "email": { "type": "string", "format": "email", "description": "Where the live key is sent." },
          "pack_id": { "type": "string", "enum": ["single", "pack_25", "pack_100", "pack_500", "pack_2500"] },
          "amount_cents": { "type": "integer", "minimum": 1000, "description": "Free-form top-up, credited 1:1 with no bulk discount. Ignored if pack_id is set." }
        } } } } },
        "responses": { "200": { "description": "Checkout session", "content": { "application/json": { "schema": { "type": "object", "properties": { "url": { "type": "string" }, "session_id": { "type": "string" } } } } } } }
      }
    },
    "/api/account/topup": {
      "post": {
        "operationId": "topUpCredits",
        "summary": "Start a top-up for the calling key. Returns a Stripe Checkout URL for a human.",
        "requestBody": { "required": true, "content": { "application/json": { "schema": { "type": "object", "properties": {
          "pack_id": { "type": "string", "enum": ["single", "pack_25", "pack_100", "pack_500", "pack_2500"] },
          "amount_cents": { "type": "integer", "minimum": 1000 }
        } } } } },
        "responses": { "200": { "description": "Checkout session", "content": { "application/json": { "schema": { "type": "object" } } } } }
      }
    },
    "/api/account": {
      "get": {
        "operationId": "getAccount",
        "summary": "Balance and mode of the calling key.",
        "responses": { "200": { "description": "Account", "content": { "application/json": { "schema": { "type": "object", "properties": { "key_prefix": { "type": "string" }, "mode": { "type": "string" }, "credits_cents": { "type": "integer" } } } } } } }
      }
    },
    "/v1/letters": {
      "post": {
        "operationId": "sendLetter",
        "summary": "Print and mail one envelope. Irreversible once printed: confirm with the human first.",
        "parameters": [
          { "name": "Idempotency-Key", "in": "header", "required": false, "schema": { "type": "string" }, "description": "Always send one. Retrying with the same key returns the original letter instead of mailing twice." }
        ],
        "requestBody": { "required": true, "content": {
          "multipart/form-data": { "schema": { "$ref": "#/components/schemas/LetterForm" } },
          "application/json": { "schema": { "$ref": "#/components/schemas/LetterJson" } }
        } },
        "responses": {
          "201": { "description": "Letter created", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Letter" } } } },
          "400": { "description": "Bad PDF (over 5 pages, bigger than A4), bad address, or bad return address" },
          "401": { "description": "Missing or invalid key" },
          "402": { "description": "Not enough credit. Ask the human to top up." }
        }
      },
      "get": {
        "operationId": "listLetters",
        "summary": "Latest 100 letters for this key.",
        "responses": { "200": { "description": "List", "content": { "application/json": { "schema": { "type": "object" } } } } }
      }
    },
    "/v1/letters/{id}": {
      "get": {
        "operationId": "getLetter",
        "summary": "One letter. status is test, paid or mailed; tracking_number appears once mailed (tracked and registered).",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "integer" } }],
        "responses": { "200": { "description": "Letter", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Letter" } } } }, "404": { "description": "Not found" } }
      }
    }
  },
  "components": {
    "securitySchemes": { "bearer": { "type": "http", "scheme": "bearer", "description": "test_... (sandbox) or live_... key" } },
    "schemas": {
      "Address": {
        "type": "object",
        "required": ["address_line1", "city", "province", "postal_code"],
        "properties": {
          "name": { "type": "string", "description": "Return address only: sender name printed above the address." },
          "address_line1": { "type": "string", "description": "For the recipient, put the recipient's name and street here, e.g. 'Jane Doe, 123 Maple St'." },
          "address_line2": { "type": "string" },
          "city": { "type": "string" },
          "province": { "type": "string", "minLength": 2, "maxLength": 2, "examples": ["ON"] },
          "postal_code": { "type": "string", "examples": ["M5V 1J2"] }
        }
      },
      "LetterForm": {
        "type": "object",
        "required": ["pdf", "address_line1", "city", "province", "postal_code"],
        "properties": {
          "pdf": { "type": "string", "format": "binary", "description": "A4 PDF, 1 to 5 pages, up to 4 MB." },
          "address_line1": { "type": "string" }, "address_line2": { "type": "string" },
          "city": { "type": "string" }, "province": { "type": "string" }, "postal_code": { "type": "string" },
          "service_level": { "type": "string", "enum": ["standard", "tracked", "registered"], "default": "standard" },
          "return_address": { "type": "string", "description": "JSON string of an Address with optional name. Omit to use the Websystems Inc. return address." }
        }
      },
      "LetterJson": {
        "type": "object",
        "required": ["pdf_base64", "to"],
        "properties": {
          "pdf_base64": { "type": "string" },
          "to": { "$ref": "#/components/schemas/Address" },
          "return_address": { "$ref": "#/components/schemas/Address" },
          "service_level": { "type": "string", "enum": ["standard", "tracked", "registered"], "default": "standard" }
        }
      },
      "Letter": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" }, "object": { "type": "string", "const": "letter" },
          "mode": { "type": "string", "enum": ["test", "live"] },
          "status": { "type": "string", "enum": ["test", "paid", "mailed"] },
          "service_level": { "type": "string" }, "pages": { "type": "integer" },
          "amount_cents": { "type": "integer" }, "currency": { "type": "string", "const": "cad" },
          "tracking_number": { "type": ["string", "null"] }, "tracking_service": { "type": ["string", "null"] },
          "to": { "$ref": "#/components/schemas/Address" },
          "return_address": { "$ref": "#/components/schemas/Address" },
          "created_at": { "type": "string" }
        }
      }
    }
  }
}
