{
  "openapi": "3.0.3",
  "info": {
    "title": "Hafzen API",
    "description": "Brand visibility measurement across SEO, AEO, GEO and AO. Pay-per-test via Stripe checkout. Base URL: https://app.hafzen.com",
    "version": "1.0.0",
    "contact": {
      "email": "hello@hafzen.com"
    }
  },
  "servers": [
    {
      "url": "https://app.hafzen.com"
    }
  ],
  "paths": {
    "/api/companies": {
      "post": {
        "summary": "Register a company to analyze",
        "description": "Stores the website URL, brand name, market and competitors for the authenticated account.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "url"
                ],
                "properties": {
                  "url": {
                    "type": "string",
                    "format": "uri"
                  },
                  "name": {
                    "type": "string"
                  },
                  "country": {
                    "type": "string"
                  },
                  "language": {
                    "type": "string",
                    "example": "en"
                  },
                  "competitors": {
                    "type": "array",
                    "items": {
                      "type": "object",
                      "properties": {
                        "name": {
                          "type": "string"
                        },
                        "url": {
                          "type": "string"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Company created, returns companyId"
          },
          "401": {
            "description": "Authentication required"
          }
        }
      }
    },
    "/api/analysis": {
      "post": {
        "summary": "Launch a visibility test run",
        "description": "Starts an analysis run for a company. Pillars can be run separately (seo, aeo, geo, ao). Pay-per-test: requires a purchase covering the selected pillars, except the one-time free GEO quick scan.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "companyId"
                ],
                "properties": {
                  "companyId": {
                    "type": "string",
                    "format": "uuid"
                  },
                  "pillars": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "enum": [
                        "seo",
                        "aeo",
                        "geo",
                        "ao"
                      ]
                    }
                  },
                  "engines": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "enum": [
                        "openai",
                        "gemini",
                        "perplexity",
                        "google_ai",
                        "anthropic",
                        "copilot"
                      ]
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Run queued, returns runId"
          },
          "402": {
            "description": "Payment required — pricing: $7.90 per pillar test, $29.90 for the full 4-pillar analysis"
          },
          "401": {
            "description": "Authentication required"
          }
        }
      }
    },
    "/api/checkout": {
      "post": {
        "summary": "Create a Stripe checkout session for a test",
        "description": "Pay-per-test pricing: $7.90 per selected pillar (SEO, AEO, GEO, AO), $29.90 for the full 4-pillar analysis. One payment unlocks exactly one run.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "pillars"
                ],
                "properties": {
                  "pillars": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 4
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Returns the Stripe checkout URL"
          },
          "401": {
            "description": "Authentication required"
          }
        }
      }
    },
    "/api/scan/quick": {
      "post": {
        "operationId": "quickScan",
        "summary": "Free GEO quick scan (no auth)",
        "description": "Runs Hafzen's evidence-weighted AI-readability audit on a public site: AI crawler access, schema.org, extractability, question headings, freshness. Rate-limited per IP; honeypot field `website` must stay empty.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "url"
                ],
                "properties": {
                  "url": {
                    "type": "string",
                    "example": "example.com"
                  },
                  "website": {
                    "type": "string",
                    "description": "Honeypot — leave empty"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Quick scan result (score, grade, top issues, percentile benchmark vs the measured-web distribution, citation-volatility evidence)"
          },
          "429": {
            "description": "Rate limited"
          }
        }
      }
    },
    "/api/benchmark": {
      "get": {
        "operationId": "benchmark",
        "summary": "Public benchmark distribution (anonymous aggregates)",
        "description": "Aggregate distribution of Hafzen GEO quick-scan scores over a rolling 180-day window: sample size, median, 25th/75th percentile. Aggregates only — individual scores are never exposed. Pass ?score=0-100 to benchmark one score against the distribution (percentile, tier). Returns available:false below the minimum sample instead of faking precision.",
        "parameters": [
          {
            "name": "score",
            "in": "query",
            "required": false,
            "schema": {
              "type": "integer",
              "minimum": 0,
              "maximum": 100
            },
            "description": "Optional score to benchmark against the current distribution"
          }
        ],
        "responses": {
          "200": {
            "description": "Distribution stats (and optional per-score benchmark)"
          }
        }
      }
    },
    "/api/mcp": {
      "post": {
        "operationId": "mcp",
        "summary": "Hafzen MCP server (Streamable HTTP, JSON-RPC 2.0)",
        "description": "Model Context Protocol endpoint for AI assistants. Methods: initialize, tools/list, tools/call. Six free tools, no auth: hafzen_geo_quick_scan (live AI-readability scan), hafzen_what_moves_citations (2026 evidence table), hafzen_citation_volatility (measured citation drift — why one-shot audits fail), hafzen_classify_bot (identify an AI bot from a user-agent), hafzen_check_robots (audit a robots.txt against 38 known AI bots), hafzen_freshness_check (sitemap lastmod → citation-risk triage). Add as custom connector in Claude/ChatGPT/Gemini/Grok, or `claude mcp add --transport http hafzen https://app.hafzen.com/api/mcp`.",
        "responses": {
          "200": {
            "description": "JSON-RPC 2.0 response"
          },
          "202": {
            "description": "Notification accepted"
          }
        }
      }
    },
    "/api/health": {
      "get": {
        "operationId": "health",
        "summary": "Liveness probe",
        "description": "Public health check for uptime monitors and the status page. Reports configuration health, never secrets. No auth. CORS-open to hafzen.com.",
        "responses": {
          "200": {
            "description": "Service alive",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "service": {
                      "type": "string"
                    },
                    "version": {
                      "type": "string"
                    },
                    "time": {
                      "type": "string",
                      "format": "date-time"
                    },
                    "supabase": {
                      "type": "string",
                      "enum": [
                        "configured",
                        "degraded"
                      ]
                    }
                  },
                  "required": [
                    "ok",
                    "service",
                    "time"
                  ]
                }
              }
            }
          }
        }
      }
    },
    "/api/leads": {
      "post": {
        "operationId": "createLead",
        "summary": "Free-scan email capture",
        "description": "Stores a scan lead (email, domain, score, marketing consent) and sends the branded report email. Honeypot field `website`. No auth. Email is best-effort; storage degrades gracefully.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "email",
                  "domain",
                  "score"
                ],
                "properties": {
                  "email": {
                    "type": "string",
                    "format": "email"
                  },
                  "domain": {
                    "type": "string"
                  },
                  "score": {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 100
                  },
                  "checksPassed": {
                    "type": "integer"
                  },
                  "checksTotal": {
                    "type": "integer"
                  },
                  "percentile": {
                    "type": "number",
                    "nullable": true
                  },
                  "benchmarkN": {
                    "type": "integer",
                    "nullable": true
                  },
                  "consent": {
                    "type": "boolean"
                  },
                  "website": {
                    "type": "string",
                    "description": "Honeypot — must be empty"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Lead processed",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": {
                      "type": "boolean"
                    },
                    "stored": {
                      "type": "boolean"
                    },
                    "emailed": {
                      "type": "boolean"
                    }
                  }
                }
              }
            }
          },
          "422": {
            "description": "Invalid email, domain or score"
          }
        }
      }
    },
    "/api/track": {
      "post": {
        "operationId": "trackEvent",
        "summary": "First-party funnel event",
        "description": "Records an anonymous funnel event (whitelist-enforced: scan_started, scan_completed, lead_captured, signup_completed, checkout_started, purchase_completed, monitoring_activated, pmf_answered). Honeypot field `website`. No PII: domain + small meta only.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "event"
                ],
                "properties": {
                  "event": {
                    "type": "string"
                  },
                  "domain": {
                    "type": "string"
                  },
                  "meta": {
                    "type": "object"
                  },
                  "website": {
                    "type": "string",
                    "description": "Honeypot — must be empty"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Recorded"
          },
          "202": {
            "description": "Ignored (honeypot/garbage)"
          },
          "422": {
            "description": "Unknown event"
          }
        }
      }
    },
    "/api/insights": {
      "post": {
        "operationId": "pageView",
        "summary": "Cookieless page-view beacon",
        "description": "Records an anonymous page view (path, referrer domain, UTM, A/B assignment). No IP, no cookies. CORS-open to hafzen.com. Honeypot field `website`.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": [
                  "path"
                ],
                "properties": {
                  "path": {
                    "type": "string"
                  },
                  "referrer": {
                    "type": "string",
                    "nullable": true
                  },
                  "utm_source": {
                    "type": "string",
                    "nullable": true
                  },
                  "utm_medium": {
                    "type": "string",
                    "nullable": true
                  },
                  "utm_campaign": {
                    "type": "string",
                    "nullable": true
                  },
                  "ab": {
                    "type": "string",
                    "nullable": true
                  },
                  "website": {
                    "type": "string",
                    "description": "Honeypot — must be empty"
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Recorded"
          },
          "202": {
            "description": "Ignored"
          },
          "422": {
            "description": "Invalid path"
          }
        }
      }
    },
    "/api/unsubscribe": {
      "post": {
        "operationId": "unsubscribe",
        "summary": "RFC 8058 one-click unsubscribe",
        "description": "Mailbox providers POST here (List-Unsubscribe=One-Click). Email via ?e= query param or form/JSON body. Idempotent, always 200. Kills marketing consent + drip schedule; transactional history unaffected.",
        "parameters": [
          {
            "name": "e",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "email"
            },
            "description": "Recipient email"
          }
        ],
        "responses": {
          "200": {
            "description": "Processed (always, by design — deliverability)"
          }
        }
      },
      "get": {
        "operationId": "unsubscribePage",
        "summary": "Unsubscribe confirmation page (human)",
        "description": "Branded confirmation page for the footer link in marketing emails.",
        "parameters": [
          {
            "name": "e",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "email"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "HTML confirmation page"
          }
        }
      }
    },
    "/api/badge/{token}": {
      "get": {
        "operationId": "badge",
        "summary": "Verified score badge (SVG/PNG)",
        "description": "Server-rendered live badge for a company token — the number cannot be self-typed. Options: ?theme=light, ?variant=compact.",
        "parameters": [
          {
            "name": "token",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "pattern": "^rs_[0-9a-f]{32}$"
            }
          },
          {
            "name": "theme",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "dark",
                "light"
              ]
            }
          },
          {
            "name": "variant",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "default",
                "compact"
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Badge image"
          },
          "404": {
            "description": "Unknown token"
          }
        }
      }
    }
  }
}