{
  "openapi": "3.1.0",
  "info": {
    "title": "WhatsCRM Public API",
    "version": "1.0.0",
    "description": "Public Developer API for WhatsCRM. Authentication via per-user `api_key` passed as `token` query parameter. See https://wcrm.itninja.co.il/api-docs.md for the full reference.",
    "contact": { "url": "https://wcrm.itninja.co.il/api.html" },
    "license": { "name": "Proprietary" }
  },
  "servers": [
    { "url": "https://wcrm.itninja.co.il", "description": "Production" }
  ],
  "tags": [
    { "name": "help", "description": "Discovery / documentation index" },
    { "name": "qr", "description": "WhatsApp via Baileys (QR scan)" },
    { "name": "v1", "description": "Meta Cloud API send/receive" }
  ],
  "components": {
    "securitySchemes": {
      "ApiKeyQuery": {
        "type": "apiKey",
        "in": "query",
        "name": "token",
        "description": "User API key obtained via /api/user/generate_api_keys"
      }
    },
    "schemas": {
      "HelpIndex": {
        "type": "object",
        "properties": {
          "success": { "type": "boolean" },
          "title": { "type": "string" },
          "url": { "type": "string", "format": "uri-reference" },
          "language": { "type": "string" },
          "api_docs": {
            "type": "object",
            "properties": {
              "title": { "type": "string" },
              "html": { "type": "string" },
              "markdown": { "type": "string" }
            }
          }
        }
      },
      "SendMessageRequest": {
        "type": "object",
        "required": ["uniqueId", "receiverMobile", "messageType"],
        "properties": {
          "uniqueId": { "type": "string", "description": "Connected Baileys instance id" },
          "receiverMobile": { "type": "string", "description": "E.164 without '+' (e.g. 972541234567)" },
          "messageType": { "type": "string", "enum": ["text", "image", "video", "document", "audio"] },
          "message": { "type": "string", "description": "Text body or caption" },
          "fileUrl": { "type": "string", "format": "uri", "description": "Public URL for media types" }
        }
      },
      "SendMessageResponse": {
        "type": "object",
        "properties": {
          "success": { "type": "boolean" },
          "msg": { "type": "string" },
          "data": { "type": "object" }
        }
      }
    }
  },
  "paths": {
    "/api/help": {
      "get": {
        "tags": ["help"],
        "summary": "Documentation discovery index",
        "description": "Returns a JSON index of all public documentation URLs. No authentication required.",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HelpIndex" }
              }
            }
          }
        }
      }
    },
    "/api/qr/rest/send_message": {
      "post": {
        "tags": ["qr"],
        "summary": "Send a WhatsApp message via a connected Baileys instance",
        "security": [{ "ApiKeyQuery": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/SendMessageRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Sent",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/SendMessageResponse" }
              }
            }
          },
          "401": { "description": "Invalid api_key" },
          "402": { "description": "Plan limit exceeded" }
        }
      }
    },
    "/api/v1/send_message": {
      "post": {
        "tags": ["v1"],
        "summary": "Send a WhatsApp message via Meta Cloud API",
        "security": [{ "ApiKeyQuery": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/SendMessageRequest" }
            }
          }
        },
        "responses": {
          "200": { "description": "OK" },
          "401": { "description": "Invalid api_key" }
        }
      }
    }
  },
  "externalDocs": {
    "description": "Full Markdown reference",
    "url": "https://wcrm.itninja.co.il/api-docs.md"
  }
}
