{
  "openapi": "3.1.0",
  "info": {
    "title": "SheetsToLabels Business API",
    "version": "1.0.0",
    "description": "Generate print-ready label, card, badge, and barcode PDFs from cloud-synced templates."
  },
  "servers": [
    {
      "url": "https://sheetstolabels.com",
      "description": "Production"
    }
  ],
  "security": [{ "bearerAuth": [] }],
  "tags": [
    { "name": "Generation" },
    { "name": "Templates" },
    { "name": "Print layouts" }
  ],
  "paths": {
    "/api/v1/generate-pdf": {
      "post": {
        "operationId": "generatePdf",
        "summary": "Generate a print-ready PDF",
        "tags": ["Generation"],
        "parameters": [
          {
            "name": "X-Integration-Source",
            "in": "header",
            "required": false,
            "description": "Short identifier for the automation source, such as make, n8n, airtable, or google-apps-script.",
            "schema": {
              "type": "string",
              "minLength": 1,
              "maxLength": 64,
              "pattern": "^[a-z0-9][a-z0-9._-]*$"
            }
          },
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "description": "A unique key for safe retries. Reuse it only with the same request body for up to 24 hours.",
            "schema": {
              "type": "string",
              "minLength": 1,
              "maxLength": 255,
              "pattern": "^[\\x21-\\x7E]+$"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/GeneratePdfRequest" },
              "examples": {
                "singleCard": {
                  "summary": "One card per PDF page",
                  "value": {
                    "templateId": "custom-1234567890",
                    "layout": { "type": "single" },
                    "records": [
                      {
                        "variables": {
                          "name": "Alex Morgan",
                          "code": "ag193",
                          "qrUrl": "https://example.com/ag193"
                        }
                      }
                    ]
                  }
                },
                "labelSheet": {
                  "summary": "Avery-compatible label sheet",
                  "value": {
                    "templateId": "custom-1234567890",
                    "layout": {
                      "type": "preset",
                      "presetId": "avery-5160-compatible"
                    },
                    "records": [
                      {
                        "variables": {
                          "name": "Coffee Beans",
                          "sku": "COF-250"
                        },
                        "copies": 2
                      }
                    ]
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "The generated PDF binary.",
            "headers": {
              "Content-Disposition": {
                "schema": { "type": "string" },
                "description": "Suggested PDF filename."
              },
              "X-Request-Id": { "$ref": "#/components/headers/RequestId" },
              "X-Record-Count": { "$ref": "#/components/headers/RecordCount" },
              "X-Item-Count": { "$ref": "#/components/headers/ItemCount" },
              "X-Page-Count": { "$ref": "#/components/headers/PageCount" },
              "X-Credits-Consumed": { "$ref": "#/components/headers/CreditsConsumed" },
              "Idempotency-Replayed": {
                "schema": { "type": "string", "const": "true" },
                "description": "Present when a completed request was safely replayed without another charge."
              },
              "X-Original-Request-Id": {
                "schema": { "type": "string", "format": "uuid" },
                "description": "Original request ID for an idempotent replay."
              }
            },
            "content": {
              "application/pdf": {
                "schema": { "type": "string", "format": "binary" }
              }
            }
          },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "402": { "$ref": "#/components/responses/InsufficientCredits" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "409": { "$ref": "#/components/responses/Conflict" },
          "413": { "$ref": "#/components/responses/RequestTooLarge" },
          "422": { "$ref": "#/components/responses/Unprocessable" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "500": { "$ref": "#/components/responses/ServerError" },
          "503": { "$ref": "#/components/responses/Unavailable" }
        }
      }
    },
    "/api/v1/templates": {
      "get": {
        "operationId": "listTemplates",
        "summary": "List API-ready templates",
        "tags": ["Templates"],
        "responses": {
          "200": {
            "description": "Cloud-synced templates owned by the API key account.",
            "headers": {
              "X-Request-Id": { "$ref": "#/components/headers/RequestId" }
            },
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["data", "meta"],
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/TemplateSummary" }
                    },
                    "meta": { "$ref": "#/components/schemas/CountMeta" }
                  }
                }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "503": { "$ref": "#/components/responses/Unavailable" }
        }
      }
    },
    "/api/v1/templates/{templateId}": {
      "get": {
        "operationId": "getTemplate",
        "summary": "Inspect a template and its variables",
        "tags": ["Templates"],
        "parameters": [
          {
            "name": "templateId",
            "in": "path",
            "required": true,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "Template metadata, variable names, and compatible print presets.",
            "headers": {
              "X-Request-Id": { "$ref": "#/components/headers/RequestId" }
            },
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["data"],
                  "properties": {
                    "data": { "$ref": "#/components/schemas/TemplateDetail" }
                  }
                }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "422": { "$ref": "#/components/responses/Unprocessable" },
          "429": { "$ref": "#/components/responses/RateLimited" },
          "503": { "$ref": "#/components/responses/Unavailable" }
        }
      }
    },
    "/api/v1/layout-presets": {
      "get": {
        "operationId": "listLayoutPresets",
        "summary": "List supported sheet-layout presets",
        "tags": ["Print layouts"],
        "parameters": [
          {
            "name": "search",
            "in": "query",
            "required": false,
            "schema": { "type": "string" }
          },
          {
            "name": "widthMm",
            "in": "query",
            "required": false,
            "schema": { "type": "number", "exclusiveMinimum": 0 }
          },
          {
            "name": "heightMm",
            "in": "query",
            "required": false,
            "schema": { "type": "number", "exclusiveMinimum": 0 }
          }
        ],
        "responses": {
          "200": {
            "description": "Avery and APLI-compatible print-layout presets.",
            "headers": {
              "X-Request-Id": { "$ref": "#/components/headers/RequestId" }
            },
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["data", "meta"],
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": { "$ref": "#/components/schemas/LayoutPreset" }
                    },
                    "meta": { "$ref": "#/components/schemas/CountMeta" }
                  }
                }
              }
            }
          },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "403": { "$ref": "#/components/responses/Forbidden" },
          "429": { "$ref": "#/components/responses/RateLimited" }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "API key"
      }
    },
    "headers": {
      "RequestId": {
        "schema": { "type": "string", "format": "uuid" },
        "description": "Include this request ID when contacting support."
      },
      "RecordCount": {
        "schema": { "type": "integer", "minimum": 1, "maximum": 250 }
      },
      "ItemCount": {
        "schema": { "type": "integer", "minimum": 1, "maximum": 250 }
      },
      "PageCount": {
        "schema": { "type": "integer", "minimum": 1, "maximum": 25 }
      },
      "CreditsConsumed": {
        "schema": { "type": "integer", "minimum": 0, "maximum": 25 }
      }
    },
    "schemas": {
      "GeneratePdfRequest": {
        "type": "object",
        "additionalProperties": false,
        "required": ["templateId", "layout", "records"],
        "properties": {
          "templateId": { "type": "string", "minLength": 1 },
          "layout": { "$ref": "#/components/schemas/PrintLayout" },
          "records": {
            "type": "array",
            "minItems": 1,
            "maxItems": 250,
            "items": { "$ref": "#/components/schemas/Record" }
          }
        }
      },
      "Record": {
        "type": "object",
        "additionalProperties": false,
        "required": ["variables"],
        "properties": {
          "variables": {
            "type": "object",
            "additionalProperties": {
              "type": ["string", "number", "boolean", "null"]
            }
          },
          "copies": {
            "type": "integer",
            "minimum": 1,
            "maximum": 250,
            "default": 1
          }
        }
      },
      "PrintLayout": {
        "oneOf": [
          { "$ref": "#/components/schemas/SingleLayout" },
          { "$ref": "#/components/schemas/PresetLayout" },
          { "$ref": "#/components/schemas/ManualLayout" }
        ],
        "discriminator": { "propertyName": "type" }
      },
      "SingleLayout": {
        "type": "object",
        "additionalProperties": false,
        "required": ["type"],
        "properties": { "type": { "const": "single" } }
      },
      "PresetLayout": {
        "type": "object",
        "additionalProperties": false,
        "required": ["type", "presetId"],
        "properties": {
          "type": { "const": "preset" },
          "presetId": { "type": "string", "minLength": 1 },
          "startSlot": { "type": "integer", "minimum": 1 }
        }
      },
      "ManualLayout": {
        "type": "object",
        "additionalProperties": false,
        "required": ["type", "paper", "grid", "spacingMm"],
        "properties": {
          "type": { "const": "manual" },
          "paper": { "$ref": "#/components/schemas/PaperRequest" },
          "grid": { "$ref": "#/components/schemas/Grid" },
          "spacingMm": { "$ref": "#/components/schemas/Spacing" },
          "startSlot": { "type": "integer", "minimum": 1 }
        }
      },
      "PaperRequest": {
        "oneOf": [
          {
            "type": "object",
            "additionalProperties": false,
            "required": ["size"],
            "properties": { "size": { "enum": ["Letter", "A4"] } }
          },
          {
            "type": "object",
            "additionalProperties": false,
            "required": ["size", "widthMm", "heightMm"],
            "properties": {
              "size": { "const": "Custom" },
              "widthMm": { "type": "number", "exclusiveMinimum": 0 },
              "heightMm": { "type": "number", "exclusiveMinimum": 0 }
            }
          }
        ]
      },
      "Grid": {
        "type": "object",
        "additionalProperties": false,
        "required": ["rows", "columns"],
        "properties": {
          "rows": { "type": "integer", "minimum": 1, "maximum": 100 },
          "columns": { "type": "integer", "minimum": 1, "maximum": 100 }
        }
      },
      "Spacing": {
        "type": "object",
        "additionalProperties": false,
        "required": ["top", "right", "bottom", "left", "gapX", "gapY"],
        "properties": {
          "top": { "type": "number", "minimum": 0 },
          "right": { "type": "number", "minimum": 0 },
          "bottom": { "type": "number", "minimum": 0 },
          "left": { "type": "number", "minimum": 0 },
          "gapX": { "type": "number", "minimum": 0 },
          "gapY": { "type": "number", "minimum": 0 }
        }
      },
      "TemplateVariable": {
        "type": "object",
        "required": ["name", "usedBy"],
        "properties": {
          "name": { "type": "string" },
          "usedBy": {
            "type": "array",
            "items": {
              "enum": ["text", "barcode", "qrcode", "shape", "image"]
            }
          },
          "example": { "type": "string" }
        }
      },
      "TemplateSummary": {
        "type": "object",
        "required": ["id", "name", "widthMm", "heightMm", "variables", "compatiblePresetIds", "updatedAt"],
        "properties": {
          "id": { "type": "string" },
          "name": { "type": "string" },
          "widthMm": { "type": "number" },
          "heightMm": { "type": "number" },
          "variables": {
            "type": "array",
            "items": { "$ref": "#/components/schemas/TemplateVariable" }
          },
          "compatiblePresetIds": {
            "type": "array",
            "items": { "type": "string" }
          },
          "updatedAt": { "type": "string", "format": "date-time" }
        }
      },
      "TemplateDetail": {
        "allOf": [
          { "$ref": "#/components/schemas/TemplateSummary" },
          {
            "type": "object",
            "required": ["compatiblePresets"],
            "properties": {
              "compatiblePresets": {
                "type": "array",
                "items": { "$ref": "#/components/schemas/LayoutPreset" }
              }
            }
          }
        ]
      },
      "LayoutPreset": {
        "type": "object",
        "required": ["id", "name", "itemSizeMm", "paper", "grid", "spacingMm"],
        "properties": {
          "id": { "type": "string" },
          "name": { "type": "string" },
          "description": { "type": "string" },
          "itemSizeMm": { "$ref": "#/components/schemas/Size" },
          "paper": {
            "type": "object",
            "required": ["size", "widthMm", "heightMm"],
            "properties": {
              "size": { "type": "string" },
              "widthMm": { "type": "number" },
              "heightMm": { "type": "number" }
            }
          },
          "grid": { "$ref": "#/components/schemas/Grid" },
          "spacingMm": { "$ref": "#/components/schemas/Spacing" }
        }
      },
      "Size": {
        "type": "object",
        "required": ["width", "height"],
        "properties": {
          "width": { "type": "number" },
          "height": { "type": "number" }
        }
      },
      "CountMeta": {
        "type": "object",
        "required": ["count"],
        "properties": { "count": { "type": "integer", "minimum": 0 } }
      },
      "Problem": {
        "type": "object",
        "required": ["type", "title", "status", "detail", "code", "requestId"],
        "properties": {
          "type": { "type": "string" },
          "title": { "type": "string" },
          "status": { "type": "integer" },
          "detail": { "type": "string" },
          "code": { "type": "string" },
          "requestId": { "type": "string", "format": "uuid" }
        }
      }
    },
    "responses": {
      "BadRequest": { "$ref": "#/components/responses/ProblemResponse" },
      "Unauthorized": { "$ref": "#/components/responses/ProblemResponse" },
      "InsufficientCredits": { "$ref": "#/components/responses/ProblemResponse" },
      "Forbidden": { "$ref": "#/components/responses/ProblemResponse" },
      "NotFound": { "$ref": "#/components/responses/ProblemResponse" },
      "Conflict": { "$ref": "#/components/responses/ProblemResponse" },
      "RequestTooLarge": { "$ref": "#/components/responses/ProblemResponse" },
      "Unprocessable": { "$ref": "#/components/responses/ProblemResponse" },
      "RateLimited": {
        "description": "The API key exceeded its request limit.",
        "headers": {
          "Retry-After": { "schema": { "type": "integer", "minimum": 1 } },
          "X-Request-Id": { "$ref": "#/components/headers/RequestId" }
        },
        "content": {
          "application/problem+json": {
            "schema": { "$ref": "#/components/schemas/Problem" }
          }
        }
      },
      "ServerError": { "$ref": "#/components/responses/ProblemResponse" },
      "Unavailable": { "$ref": "#/components/responses/ProblemResponse" },
      "ProblemResponse": {
        "description": "A structured API error.",
        "headers": {
          "X-Request-Id": { "$ref": "#/components/headers/RequestId" }
        },
        "content": {
          "application/problem+json": {
            "schema": { "$ref": "#/components/schemas/Problem" }
          }
        }
      }
    }
  }
}
