{
  "openapi": "3.1.0",
  "info": {
    "title": "Ovlink Pro API",
    "version": "1.0.0",
    "description": "Read and manage short links, statistics and campaigns with a Pro API key. Every request is authenticated with `x-api-key` (or `Authorization: Bearer <key>`) and scoped to the key owner."
  },
  "servers": [{ "url": "https://ovlink.sbs" }],
  "security": [{ "apiKey": [] }],
  "components": {
    "securitySchemes": {
      "apiKey": {
        "type": "apiKey",
        "in": "header",
        "name": "x-api-key",
        "description": "A key created on /account (Pro plan)."
      }
    },
    "schemas": {
      "LinkStats": {
        "type": "object",
        "properties": {
          "days": { "oneOf": [{ "type": "integer" }, { "type": "string", "enum": ["all"] }] },
          "clicks": { "type": "integer" },
          "uniques": { "type": "integer" },
          "conversions": { "type": "integer" }
        }
      },
      "LinkListItem": {
        "type": "object",
        "properties": {
          "id": { "type": "integer" },
          "short": { "type": "string" },
          "original": { "type": "string" },
          "created_at": { "type": "string" },
          "campaign_id": { "type": "integer", "nullable": true },
          "disabled": { "type": "boolean" },
          "max_clicks": { "type": "integer", "nullable": true },
          "stats": { "$ref": "#/components/schemas/LinkStats" }
        }
      },
      "Error": {
        "type": "object",
        "properties": { "error": { "type": "string" } }
      }
    }
  },
  "paths": {
    "/api/pro/v1/account": {
      "get": {
        "summary": "Account overview, plan and API usage",
        "responses": {
          "200": { "description": "Account payload" },
          "401": { "description": "Missing or invalid key", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } },
          "403": { "description": "Pro plan required" }
        }
      }
    },
    "/api/pro/v1/shorten": {
      "post": {
        "summary": "Create a short link",
        "parameters": [
          { "name": "Idempotency-Key", "in": "header", "required": false, "schema": { "type": "string" }, "description": "Replaying the same key returns the original result." }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["original_url"],
                "properties": {
                  "original_url": { "type": "string", "format": "uri" },
                  "original_b": { "type": "string", "format": "uri" },
                  "custom_alias": { "type": "string" },
                  "campaign_id": { "type": "integer" },
                  "utm": {
                    "type": "object",
                    "properties": {
                      "source": { "type": "string" },
                      "medium": { "type": "string" },
                      "campaign": { "type": "string" },
                      "term": { "type": "string" },
                      "content": { "type": "string" }
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": { "description": "Created link" },
          "400": { "description": "Validation error" },
          "401": { "description": "Missing or invalid key" }
        }
      }
    },
    "/api/pro/v1/links": {
      "get": {
        "summary": "List links with cursor pagination",
        "parameters": [
          { "name": "limit", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 25 } },
          { "name": "cursor", "in": "query", "schema": { "type": "string" }, "description": "Opaque cursor from `next_cursor`." },
          { "name": "days", "in": "query", "schema": { "type": "string", "default": "30" }, "description": "Stats window: 7, 30, 90, 365 or all." },
          { "name": "campaign_id", "in": "query", "schema": { "type": "integer" } },
          { "name": "q", "in": "query", "schema": { "type": "string", "maxLength": 80 }, "description": "Short-code prefix filter." },
          { "name": "status", "in": "query", "schema": { "type": "string", "enum": ["active", "disabled"] } }
        ],
        "responses": {
          "200": {
            "description": "A page of links",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "links": { "type": "array", "items": { "$ref": "#/components/schemas/LinkListItem" } },
                    "has_more": { "type": "boolean" },
                    "next_cursor": { "type": "string", "nullable": true }
                  }
                }
              }
            }
          },
          "400": { "description": "Invalid cursor", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } },
          "401": { "description": "Missing or invalid key" }
        }
      }
    },
    "/api/pro/v1/links/{short}/stats": {
      "get": {
        "summary": "Statistics for one link",
        "parameters": [
          { "name": "short", "in": "path", "required": true, "schema": { "type": "string" } },
          { "name": "days", "in": "query", "schema": { "type": "string", "default": "30" } },
          { "name": "bots", "in": "query", "schema": { "type": "string", "enum": ["include", "exclude"] } }
        ],
        "responses": {
          "200": { "description": "Aggregated statistics" },
          "404": { "description": "Link not found or not owned by the key" }
        }
      }
    },
    "/api/pro/v1/links/{short}/clicks": {
      "get": {
        "summary": "Click-level export",
        "parameters": [
          { "name": "short", "in": "path", "required": true, "schema": { "type": "string" } },
          { "name": "format", "in": "query", "schema": { "type": "string", "enum": ["csv", "json"], "default": "csv" } },
          { "name": "days", "in": "query", "schema": { "type": "string", "default": "30" } }
        ],
        "responses": {
          "200": { "description": "CSV or JSON rows (bounded)" },
          "404": { "description": "Link not found" }
        }
      }
    },
    "/api/pro/v1/campaigns": {
      "get": {
        "summary": "List campaigns with aggregated counters",
        "parameters": [
          { "name": "days", "in": "query", "schema": { "type": "string", "default": "30" } },
          { "name": "include_archived", "in": "query", "schema": { "type": "string", "enum": ["0", "1"] } }
        ],
        "responses": {
          "200": { "description": "Campaign list" },
          "401": { "description": "Missing or invalid key" }
        }
      }
    },
    "/api/pro/v1/campaigns/{id}": {
      "get": {
        "summary": "Statistics for one campaign",
        "parameters": [
          { "name": "id", "in": "path", "required": true, "schema": { "type": "integer" } },
          { "name": "days", "in": "query", "schema": { "type": "string", "default": "30" } }
        ],
        "responses": {
          "200": { "description": "Campaign statistics" },
          "404": { "description": "Campaign not found" }
        }
      }
    },
    "/api/pro/v1/campaigns/{id}/attribution": {
      "get": {
        "summary": "Attributed vs unattributed conversion reconciliation for one campaign (P22)",
        "parameters": [
          { "name": "id", "in": "path", "required": true, "schema": { "type": "integer" } },
          { "name": "window_days", "in": "query", "schema": { "type": "integer", "enum": [1, 7, 14, 30], "default": 7 } },
          { "name": "model", "in": "query", "schema": { "type": "string", "enum": ["last_touch", "first_touch"], "default": "last_touch" } }
        ],
        "responses": {
          "200": { "description": "Explainable attribution & multi-currency reconciliation report" },
          "404": { "description": "Campaign not found" }
        }
      }
    },
    "/api/pro/v1/campaigns/{id}/decision": {
      "get": {
        "summary": "Decision analytics: 5 core KPIs, channel comparison, period-over-period deltas & freshness badges (P23)",
        "parameters": [
          { "name": "id", "in": "path", "required": true, "schema": { "type": "integer" } },
          { "name": "days", "in": "query", "schema": { "type": "string", "default": "30" } },
          { "name": "bots", "in": "query", "schema": { "type": "string", "enum": ["exclude", "include"], "default": "exclude" } },
          { "name": "window_days", "in": "query", "schema": { "type": "integer", "enum": [1, 7, 14, 30], "default": 7 } },
          { "name": "model", "in": "query", "schema": { "type": "string", "enum": ["last_touch", "first_touch"], "default": "last_touch" } }
        ],
        "responses": {
          "200": { "description": "Decision analytics report" },
          "404": { "description": "Campaign not found" }
        }
      }
    },
    "/api/pro/v1/campaigns/{id}/snapshots": {
      "get": {
        "summary": "List persisted report provenance snapshots for a campaign (P24)",
        "parameters": [
          { "name": "id", "in": "path", "required": true, "schema": { "type": "integer" } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "default": 25 } }
        ],
        "responses": {
          "200": { "description": "Snapshot revision history with content hashes and audit entry IDs" },
          "404": { "description": "Campaign not found" }
        }
      }
    },
    "/api/pro/v1/campaigns/{id}/snapshots/{snapshotId}": {
      "get": {
        "summary": "Retrieve a specific report provenance snapshot by snapshot ID (P24)",
        "parameters": [
          { "name": "id", "in": "path", "required": true, "schema": { "type": "integer" } },
          { "name": "snapshotId", "in": "path", "required": true, "schema": { "type": "string" } },
          { "name": "surface", "in": "query", "schema": { "type": "string", "enum": ["json", "csv", "pdf", "portal"] } }
        ],
        "responses": {
          "200": { "description": "Persisted report snapshot with provenance contract" },
          "404": { "description": "Campaign or snapshot not found" }
        }
      }
    },
    "/api/pro/v1/conversions": {
      "post": {
        "summary": "Ingest a server-to-server conversion event (lead, purchase, signup)",
        "parameters": [
          { "name": "X-Ovlink-Timestamp", "in": "header", "required": false, "schema": { "type": "string" }, "description": "Optional Unix timestamp (seconds) for HMAC-SHA256 request signing." },
          { "name": "X-Ovlink-Signature", "in": "header", "required": false, "schema": { "type": "string" }, "description": "Optional sha256=<hmac> over '<timestamp>.<rawBody>'." }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["event_id", "goal"],
                "properties": {
                  "event_id": { "type": "string", "minLength": 4, "maxLength": 120 },
                  "short": { "type": "string" },
                  "link_id": { "type": "integer" },
                  "campaign_id": { "type": "integer" },
                  "goal": { "type": "string" },
                  "timestamp": { "type": "string", "format": "date-time" },
                  "source": { "type": "string", "enum": ["server_api", "js_sdk", "webhook", "checkout", "crm"] },
                  "consent": { "type": "string", "enum": ["analytics", "essential", "declined"] },
                  "value": { "type": "number", "minimum": 0 },
                  "currency": { "type": "string", "minLength": 3, "maxLength": 3 },
                  "actor": { "type": "object", "properties": { "external_id": { "type": "string" } } }
                }
              }
            }
          }
        },
        "responses": {
          "201": { "description": "Conversion event recorded" },
          "200": { "description": "Idempotent replay of existing event_id" },
          "202": { "description": "Skipped due to consent_declined (0 rows written)" },
          "400": { "description": "Validation or timestamp window error" },
          "401": { "description": "Missing/invalid API key or HMAC signature" },
          "403": { "description": "Missing shorten:write scope or Pro plan required" },
          "404": { "description": "Target link not found for account" },
          "409": { "description": "event_id already used with a different payload" }
        }
      },
      "get": {
        "summary": "List recorded conversion events",
        "parameters": [
          { "name": "short", "in": "query", "schema": { "type": "string" } },
          { "name": "campaign_id", "in": "query", "schema": { "type": "integer" } },
          { "name": "goal", "in": "query", "schema": { "type": "string" } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 100, "default": 50 } }
        ],
        "responses": {
          "200": { "description": "Conversion event list" },
          "401": { "description": "Missing or invalid key" }
        }
      }
    },
    "/api/pro/v1/openapi.json": {
      "get": {
        "summary": "This document",
        "responses": { "200": { "description": "OpenAPI 3.1 description" } }
      }
    }
  }
}
