{
  "openapi": "3.1.0",
  "info": {
    "title": "Critique.Email Public API",
    "version": "1.1.0",
    "description": "Import .eml files for spot analysis and access your Critique.Email account's reports, brands, tags and analytics. Authenticate with a personal API key created in Account > API keys."
  },
  "servers": [
    { "url": "https://yfgssnyoqrvotgibdyua.supabase.co/functions/v1/api" }
  ],
  "security": [{ "ApiKeyAuth": [] }],
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": { "type": "apiKey", "in": "header", "name": "x-api-key" }
    }
  },
  "paths": {
    "/emails/import": {
      "post": {
        "operationId": "import_eml",
        "summary": "Import one .eml file and start spot analysis in the authenticated account.",
        "description": "Maximum decoded size 5 MB. Identical files deduplicate per account. Returns email_id and processing status; read GET /emails/{id} until completed. Retry the same import when status is failed. Imports do not trigger automated engagement.",
        "requestBody": {
          "required": true,
          "content": {
            "message/rfc822": { "schema": { "type": "string", "format": "binary" } },
            "multipart/form-data": { "schema": { "type": "object", "required": ["file"], "properties": { "file": { "type": "string", "format": "binary" } } } },
            "application/json": { "schema": { "oneOf": [
              { "type": "object", "additionalProperties": false, "required": ["eml"], "properties": { "eml": { "type": "string", "minLength": 1, "description": "Complete RFC 822 email contents." } } },
              { "type": "object", "additionalProperties": false, "required": ["eml_base64"], "properties": { "eml_base64": { "type": "string", "minLength": 1, "contentEncoding": "base64" } } }
            ] } }
          }
        },
        "responses": {
          "202": { "description": "Saved and processing. data includes email_id, status, duplicate, report_url and status_path." },
          "200": { "description": "An identical file was already analyzed." },
          "400": { "description": "Invalid email or import payload." },
          "401": { "description": "Missing or invalid API key." },
          "403": { "description": "Account unavailable for analysis." },
          "413": { "description": "File exceeds 5 MB." },
          "415": { "description": "Unsupported content type." }
        }
      }
    },
    "/emails": {
      "get": {
        "operationId": "list_emails",
        "summary": "List analyzed emails, newest first.",
        "parameters": [
          { "name": "brand", "in": "query", "schema": { "type": "string" }, "description": "Exact brand name filter." },
          { "name": "category", "in": "query", "schema": { "type": "string" }, "description": "Email type/category filter." },
          { "name": "since", "in": "query", "schema": { "type": "string", "format": "date" }, "description": "Only emails received on or after this date (YYYY-MM-DD)." },
          { "name": "until", "in": "query", "schema": { "type": "string", "format": "date" }, "description": "Only emails received on or before this date (YYYY-MM-DD)." },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "maximum": 200, "default": 50 } },
          { "name": "offset", "in": "query", "schema": { "type": "integer", "default": 0 } }
        ],
        "responses": { "200": { "description": "A page of emails with pagination totals." } }
      }
    },
    "/emails/{id}": {
      "get": {
        "operationId": "get_email_report",
        "summary": "Get one email's score and Under the Hood content, technical, compliance, tracking and construction checks.",
        "parameters": [
          { "name": "id", "in": "path", "required": true, "schema": { "type": "string" }, "description": "Email id from list_emails." }
        ],
        "responses": { "200": { "description": "Email and report fields, with status: processing, completed or failed. Analysis fields may be null until completed." }, "404": { "description": "Not found." } }
      }
    },
    "/brands": {
      "get": {
        "operationId": "list_brands",
        "summary": "List the brands tracked in this account.",
        "responses": { "200": { "description": "Brands." } }
      }
    },
    "/tags": {
      "get": {
        "operationId": "list_tags",
        "summary": "List the custom tags in this account.",
        "responses": { "200": { "description": "Tags." } }
      }
    },
    "/analytics/summary": {
      "get": {
        "operationId": "analytics_summary",
        "summary": "Account-wide totals: email count, average scores, category mix, top brands and email service provider breakdown.",
        "responses": { "200": { "description": "Summary analytics." } }
      }
    },
    "/credits": {
      "get": {
        "operationId": "get_credits",
        "summary": "Current credit balance, monthly allowance and recent credit activity.",
        "responses": { "200": { "description": "Credit status." } }
      }
    }
  }
}
