{
  "openapi": "3.1.0",
  "info": {
    "title": "DielineGrid Enterprise Connected Packaging API",
    "version": "1.2.0",
    "summary": "GS1 Digital Link 1.2, 100% K-only DeviceCMYK PDF exports, Zero-Downtime API Key Rotation, and Anti-Hostage Edge Routing",
    "description": "Production API for global CPG, apparel, cosmetics, and ERP/PIM integrations (SAP, NetSuite, Shopify). Features prefix-tagged SHA-256 API keys, dual-key rotation grace windows, Idempotency-Key deduplication, batch upserts up to 500 SKUs, and Day-1 standalone Cloudflare Worker export.",
    "contact": {
      "name": "DielineGrid Platform Engineering",
      "email": "hello@dielinegrid.com",
      "url": "https://dielinegrid.com/docs/api"
    }
  },
  "servers": [
    {
      "url": "https://dielinegrid.com",
      "description": "Control Plane API"
    },
    {
      "url": "https://prfqr.com",
      "description": "Isolated Edge Resolver Data Plane"
    }
  ],
  "components": {
    "securitySchemes": {
      "BearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "dlg_live_<40-hex> or dlg_test_<40-hex>"
      }
    }
  },
  "security": [{ "BearerAuth": [] }],
  "paths": {
    "/api/v1/routes": {
      "get": {
        "summary": "List workspace QR & GS1 routes",
        "description": "Requires scope `routes:read`.",
        "parameters": [
          { "name": "limit", "in": "query", "schema": { "type": "integer", "default": 50, "maximum": 200 } },
          { "name": "offset", "in": "query", "schema": { "type": "integer", "default": 0 } }
        ],
        "responses": { "200": { "description": "Paginated list of routes" }, "401": { "description": "Unauthorized" } }
      },
      "post": {
        "summary": "Create or upsert a GS1 Digital Link or D2C SKU route",
        "description": "Requires scope `routes:write`. Supports `Idempotency-Key` header for safe ERP retries. Writes to D1 and syncs immediately to global Cloudflare Edge KV.",
        "parameters": [
          { "name": "Idempotency-Key", "in": "header", "required": false, "schema": { "type": "string" } }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["defaultUrl"],
                "properties": {
                  "codeType": { "type": "string", "enum": ["gs1", "sku"] },
                  "routingKey": { "type": "string", "description": "GTIN-8/12/13/14 (for gs1) or SKU slug (for sku)" },
                  "mode": { "type": "string", "enum": ["redirect", "passport"] },
                  "defaultUrl": { "type": "string", "format": "uri" },
                  "title": { "type": "string" },
                  "passport": { "type": "object" }
                }
              }
            }
          }
        },
        "responses": { "201": { "description": "Created and synced to edge KV" }, "400": { "description": "Validation error" }, "429": { "description": "Daily quota exceeded" } }
      }
    },
    "/api/v1/routes/batch": {
      "post": {
        "summary": "Atomic bulk upsert up to 500 SKUs (SAP / PIM / ERP)",
        "description": "Requires scope `routes:write`. Supports `Idempotency-Key` header.",
        "responses": { "200": { "description": "Batch summary with upserted/failed counts" } }
      }
    },
    "/api/v1/routes/{routingKey}": {
      "patch": {
        "summary": "Update route destination, rules, or SKU Passport without reprinting",
        "description": "Requires scope `routes:write`.",
        "parameters": [{ "name": "routingKey", "in": "path", "required": true, "schema": { "type": "string" } }],
        "responses": { "200": { "description": "Updated and synced to edge KV" } }
      },
      "delete": {
        "summary": "Delete a route from D1 and edge KV",
        "description": "Requires scope `routes:write`.",
        "parameters": [{ "name": "routingKey", "in": "path", "required": true, "schema": { "type": "string" } }],
        "responses": { "200": { "description": "Deleted" } }
      }
    },
    "/api/v1/cmyk-pdf": {
      "post": {
        "summary": "Generate pure vector 100% K-only DeviceCMYK PDF (0 0 0 1 k)",
        "description": "Requires scope `cmyk:export`. Returns raw `application/pdf` binary with `X-DielineGrid-X-Dimension-Mm` and `X-DielineGrid-POS-Grade` response headers.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["url"],
                "properties": {
                  "url": { "type": "string", "format": "uri" },
                  "sizeMm": { "type": "number", "default": 25 },
                  "quietZoneModules": { "type": "integer", "default": 4 },
                  "title": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": { "200": { "description": "Binary PDF 1.4 in /DeviceCMYK color space" } }
      }
    },
    "/api/v1/keys": {
      "get": {
        "summary": "List API keys (prefixes, scopes, and grace-window status)",
        "description": "Requires scope `keys:manage`.",
        "responses": { "200": { "description": "List of key metadata (plaintext secrets are never returned)" } }
      },
      "post": {
        "summary": "Create a new scoped API key (dlg_live_ or dlg_test_)",
        "description": "Requires scope `keys:manage`. Plaintext key is returned once and stored hashed with peppered SHA-256.",
        "responses": { "201": { "description": "Created key with one-time plaintext secret" } }
      }
    },
    "/api/v1/keys/{id}/rotate": {
      "post": {
        "summary": "Zero-downtime dual-key rotation with configurable grace window",
        "description": "Requires scope `keys:manage`. Immediately issues a replacement key while keeping the previous key valid for `graceHours` (1, 24, or 168 hours) so global ERP clusters roll over with zero downtime.",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string" } }],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "graceHours": { "type": "integer", "enum": [1, 24, 168], "default": 24 }
                }
              }
            }
          }
        },
        "responses": { "201": { "description": "New key issued and old key placed into grace period" } }
      }
    },
    "/api/v1/keys/{id}": {
      "delete": {
        "summary": "Instant emergency revocation of an API key",
        "description": "Requires scope `keys:manage`.",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string" } }],
        "responses": { "200": { "description": "Key revoked immediately" } }
      }
    },
    "/api/v1/domains": {
      "get": { "summary": "List custom brand subdomains", "responses": { "200": { "description": "Domains list" } } },
      "post": { "summary": "Register a brand subdomain (qr.yourbrand.com) and receive DNS TXT token", "responses": { "201": { "description": "Pending domain with TXT token" } } }
    },
    "/api/v1/domains/{id}/verify": {
      "post": {
        "summary": "Verify DNS TXT ownership via Cloudflare DoH and unlock silent <30ms redirects",
        "parameters": [{ "name": "id", "in": "path", "required": true, "schema": { "type": "string" } }],
        "responses": { "200": { "description": "Domain verified" }, "422": { "description": "TXT record not found yet" } }
      }
    },
    "/api/v1/audit-logs": {
      "get": {
        "summary": "Read immutable compliance audit trail (routes, keys, domains)",
        "description": "Requires scope `audit:read`.",
        "responses": { "200": { "description": "Latest 100 audit events" } }
      }
    },
    "/api/v1/export/worker": {
      "get": {
        "summary": "Day-1 Anti-Hostage Export: standalone Cloudflare Worker + KV JSON bundle",
        "description": "Requires scope `routes:read`. Pass `?format=js` to download a directly deployable worker.js file.",
        "responses": { "200": { "description": "Standalone Worker script and routes JSON" } }
      }
    }
  }
}
