{
  "openapi": "3.1.0",
  "info": {
    "title": "MatrixAds Ad Network API",
    "version": "1.0.0",
    "description": "Programmatic Conversational AI Ad Exchange & Monetization Engine API. Sub-50ms vector ad matching and FTC-compliant double-entry settlement."
  },
  "servers": [
    {
      "url": "https://thematrixads.space/api/v1",
      "description": "Production API Server"
    },
    {
      "url": "http://localhost:3000/api/v1",
      "description": "Local Development Server"
    }
  ],
  "paths": {
    "/ads/match": {
      "post": {
        "summary": "Match Contextual Ad",
        "description": "Performs cosine vector similarity search against active advertiser campaigns to return a sponsored ad card match for a user prompt.",
        "operationId": "matchAd",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["prompt", "publisher_key"],
                "properties": {
                  "prompt": {
                    "type": "string",
                    "example": "I need cloud hosting for my Next.js application"
                  },
                  "publisher_key": {
                    "type": "string",
                    "example": "syn_pk_live_1234567890abcdef"
                  },
                  "session_id": {
                    "type": "string",
                    "example": "user_session_9876"
                  },
                  "geo": {
                    "type": "string",
                    "example": "US"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Ad match result",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "match": { "type": "boolean" },
                    "reason": { "type": "string" },
                    "ad": {
                      "type": "object",
                      "properties": {
                        "id": { "type": "string" },
                        "format": { "type": "string", "enum": ["CARD", "FOLLOWUP_PROMPT", "ACTION_LINK"] },
                        "headline": { "type": "string" },
                        "body_text": { "type": "string" },
                        "cta_url": { "type": "string" },
                        "merchant": { "type": "string" },
                        "disclosure_label": { "type": "string" },
                        "clearing_price_usd": { "type": "string" },
                        "match_score": { "type": "number" },
                        "impression_token": { "type": "string" }
                      }
                    },
                    "impression_token": { "type": "string" },
                    "latency_ms": { "type": "number" }
                  }
                }
              }
            }
          },
          "401": { "description": "Invalid or inactive publisher API key" },
          "429": { "description": "Rate limit exceeded" }
        }
      }
    },
    "/track/impression": {
      "post": {
        "summary": "Report Impression Beacon",
        "description": "Fires a view impression beacon when a matched ad card enters the user's viewport.",
        "operationId": "reportImpression",
        "parameters": [
          {
            "name": "token",
            "in": "query",
            "required": true,
            "schema": { "type": "string" },
            "description": "HMAC signed impression token returned from /ads/match"
          }
        ],
        "responses": {
          "200": { "description": "Impression recorded successfully" },
          "400": { "description": "Invalid or expired impression token" }
        }
      }
    },
    "/track/click": {
      "get": {
        "summary": "Click Redirect & Settlement",
        "description": "Verifies HMAC click token, checks click fraud pipeline, executes double-entry financial settlement, and redirects user to advertiser destination URL.",
        "operationId": "trackClick",
        "parameters": [
          {
            "name": "ct",
            "in": "query",
            "required": true,
            "schema": { "type": "string" },
            "description": "HMAC signed click token"
          }
        ],
        "responses": {
          "302": { "description": "Redirects to target advertiser landing page" },
          "400": { "description": "Fraudulent or expired click token" }
        }
      }
    }
  }
}
