{
  "openapi": "3.1.0",
  "info": {
    "title": "Trackrift Collector API",
    "version": "1.0.0",
    "description": "Public ingestion API served from api.trackrift.com (or your workspace CNAME). Covers browser batching (/collect), server-side ingest (/s2s/collect), bootstrap cookies, and the universal script loader. Workspace REST (leads, orders, outbound webhooks) is documented at https://trackrift.com/docs/api and will be added to this spec in a future release.",
    "contact": {
      "name": "Trackrift",
      "url": "https://trackrift.com/docs"
    }
  },
  "servers": [
    { "url": "https://api.trackrift.com", "description": "Production collector" },
    { "url": "https://t.trackrift.com", "description": "Default tracking subdomain (script + collect)" }
  ],
  "tags": [
    { "name": "Ingestion", "description": "Event collection from browser and server" },
    { "name": "Identity", "description": "Cookies, bootstrap, magic links" },
    { "name": "Scripts", "description": "SDK bundles" }
  ],
  "paths": {
    "/collect": {
      "post": {
        "tags": ["Ingestion"],
        "summary": "Ingest tracking events (browser)",
        "description": "Accepts a single event or a batch of up to 100 events. Called by @trackrift/sdk via sendBeacon or fetch(keepalive). Requires first-party cookies for best identity continuity.",
        "operationId": "collectEvents",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/CollectPayload" }
            }
          }
        },
        "responses": {
          "204": { "description": "Accepted (no body)" },
          "400": { "description": "Invalid payload", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } },
          "403": { "description": "Origin not allowed for workspace" },
          "429": { "description": "Rate limited" }
        }
      }
    },
    "/s2s/collect": {
      "post": {
        "tags": ["Ingestion"],
        "summary": "Server-side / offline event ingest",
        "description": "Used by @trackrift/sdk/server sendServerEvent and CRM webhooks. Requires Bearer token (SERVER_INGEST_TOKEN per workspace).",
        "operationId": "collectServerEvents",
        "security": [{ "bearerIngest": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/TrackingEvent" }
            }
          }
        },
        "responses": {
          "204": { "description": "Accepted" },
          "401": { "description": "Missing or invalid Bearer token" },
          "503": { "description": "Server ingest disabled on this deployment" }
        }
      }
    },
    "/bootstrap": {
      "get": {
        "tags": ["Identity"],
        "summary": "Issue or adopt anonymous_id and session cookies",
        "description": "Called by the SDK on init. Optional query `aid` (UUID) for cross-domain identity handoff.",
        "operationId": "bootstrap",
        "parameters": [
          {
            "name": "aid",
            "in": "query",
            "schema": { "type": "string", "format": "uuid" },
            "description": "Import anonymous_id from another domain (also passed as URL ?_aid=)"
          }
        ],
        "responses": {
          "200": {
            "description": "Cookie pair established",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": ["anonymous_id", "session_id"],
                  "properties": {
                    "anonymous_id": { "type": "string", "format": "uuid" },
                    "session_id": { "type": "string", "format": "uuid" }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/m": {
      "post": {
        "tags": ["Identity"],
        "summary": "Consume magic-link token",
        "description": "Links current anonymous_id to a signed identity when the landing URL contains ?_uid=<token>.",
        "operationId": "magicLinkConsume",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["token"],
                "properties": {
                  "token": { "type": "string" },
                  "anonymous_id": { "type": "string", "format": "uuid" }
                }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "Identity merged" },
          "401": { "description": "Invalid or expired token" }
        }
      }
    },
    "/v1/u.js": {
      "get": {
        "tags": ["Scripts"],
        "summary": "Universal tracking script",
        "description": "Loads the SDK with workspace public_id. Paste: <script async src=\"https://t.trackrift.com/v1/u.js?id=PUBLIC_ID\"></script>",
        "operationId": "universalScript",
        "parameters": [
          {
            "name": "id",
            "in": "query",
            "required": true,
            "schema": { "type": "string", "minLength": 6, "maxLength": 40 },
            "description": "Workspace public_id"
          }
        ],
        "responses": {
          "200": { "description": "JavaScript bundle", "content": { "application/javascript": { "schema": { "type": "string" } } } },
          "404": { "description": "Unknown public_id" }
        }
      }
    },
    "/sdk.js": {
      "get": {
        "tags": ["Scripts"],
        "summary": "Standalone SDK bundle",
        "operationId": "sdkBundle",
        "responses": {
          "200": { "description": "JavaScript bundle", "content": { "application/javascript": { "schema": { "type": "string" } } } }
        }
      }
    },
    "/health": {
      "get": {
        "summary": "Health check",
        "operationId": "health",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "status": { "type": "string", "example": "ok" },
                    "service": { "type": "string", "example": "collector" },
                    "ts": { "type": "integer" }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerIngest": {
        "type": "http",
        "scheme": "bearer",
        "description": "Server ingest token from Settings → API keys"
      }
    },
    "schemas": {
      "CollectPayload": {
        "oneOf": [
          { "$ref": "#/components/schemas/TrackingEvent" },
          {
            "type": "object",
            "required": ["events"],
            "properties": {
              "events": {
                "type": "array",
                "minItems": 1,
                "maxItems": 100,
                "items": { "$ref": "#/components/schemas/TrackingEvent" }
              }
            }
          }
        ]
      },
      "TrackingEvent": {
        "type": "object",
        "required": ["event_id", "event_name", "event_time", "anonymous_id", "page", "consent", "source"],
        "properties": {
          "event_id": { "type": "string", "format": "uuid" },
          "event_name": { "type": "string", "pattern": "^[a-z][a-z0-9_]*$", "maxLength": 64 },
          "event_time": { "type": "integer", "description": "Unix seconds" },
          "anonymous_id": { "type": "string", "format": "uuid" },
          "public_id": { "type": "string" },
          "email": { "type": "string", "format": "email" },
          "phone": { "type": "string" },
          "user_id": { "type": "string" },
          "external_id": { "type": "string" },
          "click_ids": { "type": "object", "additionalProperties": { "type": "string" } },
          "utm": { "type": "object", "additionalProperties": { "type": "string" } },
          "page": {
            "type": "object",
            "properties": {
              "url": { "type": "string" },
              "path": { "type": "string" },
              "title": { "type": "string" }
            }
          },
          "context": { "type": "object", "additionalProperties": true },
          "value": { "type": "number" },
          "currency": { "type": "string", "minLength": 3, "maxLength": 3 },
          "content_ids": { "type": "array", "items": { "type": "string" } },
          "consent": { "$ref": "#/components/schemas/Consent" },
          "properties": { "type": "object", "additionalProperties": true },
          "source": { "type": "string", "enum": ["web", "server", "offline", "crm"] }
        }
      },
      "Consent": {
        "type": "object",
        "properties": {
          "ad_storage": { "type": "boolean" },
          "ad_user_data": { "type": "boolean" },
          "ad_personalization": { "type": "boolean" },
          "analytics_storage": { "type": "boolean" }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "error": { "type": "string" }
        }
      }
    }
  }
}
