{
  "openapi": "3.1.0",
  "info": {
    "title": "The Systems Vault — machine surface",
    "version": "1.0.0",
    "description": "Key-gated publish + ingest endpoints and the public read surfaces. The full admin surface is exposed over MCP at POST /mcp (self-describing via tools/list) and is not duplicated here."
  },
  "servers": [
    {
      "url": "https://app.kyler.ai"
    }
  ],
  "components": {
    "securitySchemes": {
      "publishKey": {
        "type": "http",
        "scheme": "bearer",
        "description": "The root ADMIN_API_KEY, or a minted svk_ key carrying the publish_product scope. Used by /api/publish/template and /mcp."
      },
      "rootKey": {
        "type": "http",
        "scheme": "bearer",
        "description": "The root ADMIN_API_KEY ONLY. These ingest endpoints compare the bearer token against that one secret — a scoped svk_ key is rejected with 401. (The swipe endpoint is the exception: see swipeKey.)"
      },
      "swipeKey": {
        "type": "http",
        "scheme": "bearer",
        "description": "The root ADMIN_API_KEY, or a minted svk_ key carrying the swipe_ingest scope. Used by /api/ingest/swipe/{tenant}, which is called from a browser extension — a credential living on a laptop should be brand-scoped and revocable, not the root secret."
      }
    },
    "schemas": {
      "SwipeCount": {
        "type": "object",
        "properties": {
          "raw": {
            "type": "string",
            "example": "1.2K",
            "description": "The literal string as rendered on the page."
          },
          "n": {
            "type": "integer",
            "example": 1200,
            "description": "Parsed value. Omit when unreadable — a partial capture still saves."
          }
        }
      }
    }
  },
  "paths": {
    "/mcp": {
      "post": {
        "summary": "MCP endpoint (JSON-RPC) — the whole admin surface",
        "description": "Streamable HTTP JSON-RPC. Call tools/list for the full, self-describing tool surface. Humans connect over OAuth; machines send a Bearer key (or use /mcp/<key> when headers are impossible).",
        "security": [
          {
            "publishKey": []
          }
        ],
        "responses": {
          "200": {
            "description": "JSON-RPC result"
          }
        }
      }
    },
    "/api/publish/template": {
      "post": {
        "summary": "Publish a product into a brand's vault",
        "security": [
          {
            "publishKey": []
          }
        ],
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string"
            },
            "description": "Retry-safe publishing: the first success is remembered per brand for 24h; a replay returns that template with idempotent_replay=true and status 200."
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "required": [
                  "file"
                ],
                "properties": {
                  "file": {
                    "type": "string",
                    "format": "binary"
                  },
                  "tenant": {
                    "type": "string",
                    "description": "Brand slug (or pass tenant_id)"
                  },
                  "tenant_id": {
                    "type": "string"
                  },
                  "title": {
                    "type": "string",
                    "description": "Defaults from the filename"
                  },
                  "description": {
                    "type": "string"
                  },
                  "category": {
                    "type": "string"
                  },
                  "tags": {
                    "type": "string"
                  },
                  "format": {
                    "type": "string"
                  },
                  "publish": {
                    "type": "string",
                    "enum": [
                      "true",
                      "false"
                    ],
                    "description": "Default true; false = draft"
                  },
                  "publish_at": {
                    "type": "string",
                    "description": "ISO timestamp — a future value schedules the drop"
                  },
                  "is_free": {
                    "type": "string",
                    "enum": [
                      "true",
                      "false"
                    ],
                    "description": "Free lead magnet"
                  },
                  "idempotency_key": {
                    "type": "string",
                    "description": "Alternative to the header"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Idempotent replay of an earlier publish"
          },
          "201": {
            "description": "Created"
          },
          "401": {
            "description": "Invalid API key"
          },
          "403": {
            "description": "Key not authorized for this brand"
          }
        }
      }
    },
    "/api/ingest/ghl/{tenant}": {
      "post": {
        "summary": "Ingest a contact / opt-in",
        "security": [
          {
            "rootKey": []
          }
        ],
        "parameters": [
          {
            "name": "tenant",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "Brand slug, e.g. 'leverage-layer'"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "description": "At least one of email or phone is required — a contact with neither is rejected.",
                "anyOf": [
                  {
                    "required": [
                      "email"
                    ]
                  },
                  {
                    "required": [
                      "phone"
                    ]
                  }
                ],
                "properties": {
                  "email": {
                    "type": "string"
                  },
                  "phone": {
                    "type": "string"
                  },
                  "firstName": {
                    "type": "string"
                  },
                  "lastName": {
                    "type": "string"
                  },
                  "companyName": {
                    "type": "string"
                  },
                  "tags": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  },
                  "contactId": {
                    "type": "string",
                    "description": "The provider's own contact id"
                  },
                  "content_id": {
                    "type": "string",
                    "description": "Attributes first touch to a content concept"
                  },
                  "lead_magnet": {
                    "type": "string"
                  },
                  "lifecycle_stage": {
                    "type": "string"
                  },
                  "attributionSource": {
                    "type": "object",
                    "description": "First-touch attribution payload (immutable once set)"
                  },
                  "lastAttributionSource": {
                    "type": "object",
                    "description": "Most recent attribution payload"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Accepted",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "ingested": {
                      "type": "boolean",
                      "description": "false = replay skipped"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Malformed payload or unknown tenant"
          },
          "401": {
            "description": "Invalid API key"
          }
        }
      }
    },
    "/api/ingest/affiliate/{tenant}": {
      "post": {
        "summary": "Ingest an affiliate click or settled sale",
        "description": "Send offer_id on sales — without it the revenue cannot be joined to the offer that earned it and reports only as unattributed.",
        "security": [
          {
            "rootKey": []
          }
        ],
        "parameters": [
          {
            "name": "tenant",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "Brand slug, e.g. 'leverage-layer'"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "event"
                ],
                "properties": {
                  "event": {
                    "type": "string",
                    "enum": [
                      "click",
                      "sale"
                    ]
                  },
                  "transaction_id": {
                    "type": "string",
                    "description": "Required for sales — the idempotency key"
                  },
                  "offer_id": {
                    "type": "string"
                  },
                  "content_id": {
                    "type": "string"
                  },
                  "contact_email": {
                    "type": "string"
                  },
                  "amount_cents": {
                    "type": "integer"
                  },
                  "currency": {
                    "type": "string"
                  },
                  "network": {
                    "type": "string"
                  },
                  "occurred_at": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Accepted",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "ingested": {
                      "type": "boolean",
                      "description": "false = replay skipped"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Malformed payload or unknown tenant"
          },
          "401": {
            "description": "Invalid API key"
          }
        }
      }
    },
    "/api/ingest/product/{tenant}": {
      "post": {
        "summary": "Ingest an owned-product or platform order",
        "description": "Set channel='platform' for white-label / licensing / done-for-you receipts so platform income is measured on its own instead of inflating the digital-products pillar.",
        "security": [
          {
            "rootKey": []
          }
        ],
        "parameters": [
          {
            "name": "tenant",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "Brand slug, e.g. 'leverage-layer'"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "order_id"
                ],
                "properties": {
                  "order_id": {
                    "type": "string",
                    "description": "The idempotency key"
                  },
                  "template_id": {
                    "type": "string"
                  },
                  "content_id": {
                    "type": "string"
                  },
                  "contact_email": {
                    "type": "string"
                  },
                  "amount_cents": {
                    "type": "integer"
                  },
                  "currency": {
                    "type": "string"
                  },
                  "source": {
                    "type": "string"
                  },
                  "occurred_at": {
                    "type": "string"
                  },
                  "channel": {
                    "type": "string",
                    "enum": [
                      "subscription",
                      "product",
                      "affiliate",
                      "platform",
                      "marketplace",
                      "other"
                    ],
                    "description": "Default 'product'"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Accepted",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "ingested": {
                      "type": "boolean",
                      "description": "false = replay skipped"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Malformed payload or unknown tenant"
          },
          "401": {
            "description": "Invalid API key"
          }
        }
      }
    },
    "/api/ingest/email/{tenant}": {
      "post": {
        "summary": "Mirror email campaign counters",
        "security": [
          {
            "rootKey": []
          }
        ],
        "parameters": [
          {
            "name": "tenant",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "Brand slug, e.g. 'leverage-layer'"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "campaign_id"
                ],
                "properties": {
                  "campaign_id": {
                    "type": "string"
                  },
                  "name": {
                    "type": "string"
                  },
                  "content_id": {
                    "type": "string"
                  },
                  "sent": {
                    "type": "integer"
                  },
                  "opens": {
                    "type": "integer"
                  },
                  "clicks": {
                    "type": "integer"
                  },
                  "revenue_cents": {
                    "type": "integer"
                  },
                  "sent_at": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Accepted",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "ingested": {
                      "type": "boolean",
                      "description": "false = replay skipped"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "Malformed payload or unknown tenant"
          },
          "401": {
            "description": "Invalid API key"
          }
        }
      }
    },
    "/api/ingest/swipe/{tenant}": {
      "post": {
        "summary": "Save one captured social post into a brand's swipe corpus",
        "description": "Called by the Vault Swipe browser extension. Idempotent on the canonicalized source URL: a re-save returns 200 with deduped:true, refreshes the counts and appends a metrics-history row, and never overwrites the operator's rating/note/tags. A new capture returns 201.",
        "security": [
          {
            "swipeKey": []
          }
        ],
        "parameters": [
          {
            "name": "tenant",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "Brand slug, e.g. 'leverage-layer'"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "source_url",
                  "body",
                  "extractor_version"
                ],
                "properties": {
                  "extractor_version": {
                    "type": "string",
                    "example": "fb-organic-v1"
                  },
                  "platform": {
                    "type": "string",
                    "example": "facebook"
                  },
                  "kind": {
                    "type": "string",
                    "enum": [
                      "organic",
                      "ad"
                    ]
                  },
                  "source_url": {
                    "type": "string"
                  },
                  "external_id": {
                    "type": "string"
                  },
                  "captured_at": {
                    "type": "string",
                    "format": "date-time"
                  },
                  "author": {
                    "type": "object",
                    "properties": {
                      "name": {
                        "type": "string"
                      },
                      "handle": {
                        "type": "string"
                      },
                      "url": {
                        "type": "string"
                      }
                    }
                  },
                  "posted": {
                    "type": "object",
                    "description": "Facebook shows '3d', not a timestamp — precision records how much to trust iso.",
                    "properties": {
                      "raw": {
                        "type": "string"
                      },
                      "iso": {
                        "type": "string",
                        "format": "date-time"
                      },
                      "precision": {
                        "type": "string",
                        "enum": [
                          "exact",
                          "day",
                          "approx",
                          "unknown"
                        ]
                      }
                    }
                  },
                  "body": {
                    "type": "string"
                  },
                  "body_lang": {
                    "type": "string"
                  },
                  "link_url": {
                    "type": "string"
                  },
                  "counts": {
                    "type": "object",
                    "description": "Each count carries the literal string we parsed plus the number, so a locale bug stays diagnosable.",
                    "properties": {
                      "reactions": {
                        "$ref": "#/components/schemas/SwipeCount"
                      },
                      "comments": {
                        "$ref": "#/components/schemas/SwipeCount"
                      },
                      "shares": {
                        "$ref": "#/components/schemas/SwipeCount"
                      },
                      "views": {
                        "$ref": "#/components/schemas/SwipeCount"
                      }
                    }
                  },
                  "raw_html": {
                    "type": "string",
                    "description": "The post subtree, so a later extractor can re-parse it. Capped at 64KB."
                  },
                  "screenshot_b64": {
                    "type": "string",
                    "description": "Optional JPEG/PNG/WebP of the post, base64 or data-URL, max 3MB decoded. Format is sniffed from magic bytes, not trusted from the caller. A rejected image never fails the capture — the response's swipe.screenshot field says 'saved' or why it was dropped."
                  },
                  "screenshot_width": {
                    "type": "integer"
                  },
                  "screenshot_height": {
                    "type": "integer"
                  },
                  "meta": {
                    "type": "object",
                    "properties": {
                      "tags": {
                        "type": "array",
                        "items": {
                          "type": "string"
                        }
                      },
                      "rating": {
                        "type": "integer",
                        "minimum": 1,
                        "maximum": 5
                      },
                      "note": {
                        "type": "string"
                      },
                      "is_exemplar": {
                        "type": "boolean"
                      }
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Existing swipe refreshed (deduped:true)"
          },
          "201": {
            "description": "New swipe captured"
          },
          "400": {
            "description": "Unknown brand, or a payload with no body/source_url"
          },
          "401": {
            "description": "Missing or unscoped key"
          },
          "403": {
            "description": "Brand-scoped key used against another brand"
          }
        }
      }
    },
    "/api/feed.json": {
      "get": {
        "summary": "A brand's published drops, as JSON (public)",
        "description": "The brand is resolved from the request Host. No authentication.",
        "responses": {
          "200": {
            "description": "JSON feed of published products"
          }
        }
      }
    },
    "/api/feed.xml": {
      "get": {
        "summary": "A brand's published drops, as RSS (public)",
        "description": "Same feed as /api/feed.json in RSS form — the shape Zapier/Buffer/GHL consume.",
        "responses": {
          "200": {
            "description": "RSS feed of published products"
          }
        }
      }
    },
    "/go/{slug}": {
      "get": {
        "summary": "First-party affiliate click surface on a brand's own domain (public)",
        "description": "By default this renders the brand's opt-in page (first name + email) for the offer and 200s; submitting it posts to /api/offers/optin, which captures the lead and returns the affiliate destination. An offer with gate_optin=false — and any visitor who already opted in for this brand — gets the original behavior instead: the click is recorded and the response 302s to the offer destination with the controlled UTM grammar. Add ?c=<content-id> to attribute the click to a concept. The brand is taken from the hostname, so this form only works on a brand's own domain.",
        "parameters": [
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The opt-in page for this offer"
          },
          "302": {
            "description": "Redirect to the offer (gate off, or an opted-in visitor)"
          },
          "404": {
            "description": "Unknown or retired offer"
          }
        }
      }
    },
    "/api/offers/optin": {
      "post": {
        "summary": "Submit the affiliate opt-in gate (public)",
        "description": "Captures the contact against the brand and returns the affiliate destination for the browser to follow, plus a 90-day cookie that lets that visitor past the gate on later clicks. The destination is derived server-side from the stored offer — the request names a brand and an offer slug, never a URL.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "slug",
                  "email"
                ],
                "properties": {
                  "slug": {
                    "type": "string",
                    "description": "Offer slug, as it appears in the /go path"
                  },
                  "brand": {
                    "type": "string",
                    "description": "Brand (tenant) slug — required on the shared platform host, where the hostname names no brand. Ignored on a brand's own domain, which may only ever speak for itself."
                  },
                  "email": {
                    "type": "string",
                    "format": "email"
                  },
                  "first_name": {
                    "type": "string"
                  },
                  "c": {
                    "type": "string",
                    "description": "Content id the click is attributed to"
                  },
                  "s": {
                    "type": "string",
                    "description": "Source token"
                  },
                  "m": {
                    "type": "string",
                    "description": "UTM medium"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "{ ok: true, redirect_url } — send the visitor there"
          },
          "400": {
            "description": "Missing slug or invalid email"
          },
          "404": {
            "description": "Unknown brand, or unknown or retired offer"
          },
          "429": {
            "description": "Rate limited"
          }
        }
      }
    },
    "/go/{brand}/{slug}": {
      "get": {
        "summary": "First-party affiliate click surface, brand named in the path (public)",
        "description": "Same gate and redirect as /go/{slug}, for a brand served on the shared platform host, where the hostname identifies no brand. Always use the go_url returned by the offers API rather than assembling either form by hand.",
        "parameters": [
          {
            "name": "brand",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "Brand (tenant) slug"
          },
          {
            "name": "slug",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "The opt-in page for this offer"
          },
          "302": {
            "description": "Redirect to the offer (gate off, or an opted-in visitor)"
          },
          "404": {
            "description": "Unknown brand, or unknown or retired offer"
          }
        }
      }
    }
  }
}