{
    "openapi": "3.0.3",
    "info": {
        "title": "Nex Group CRM API",
        "version": "1.0.0",
        "description": "API pública del CRM de Nex Group (incluida gratis en Nex Starter). Conecta tu CRM con Make, n8n, Zapier, Aircall, WhatsApp, intranets y otros CRM.",
        "contact": {
            "name": "Nex Group Agency",
            "url": "https://www.nexgroup.agency/api-crm",
            "email": "web@nexgroup.agency"
        }
    },
    "servers": [
        {
            "url": "https://www.nexgroup.agency/api/v1"
        }
    ],
    "components": {
        "securitySchemes": {
            "ApiKey": {
                "type": "apiKey",
                "in": "header",
                "name": "X-Api-Key"
            }
        },
        "schemas": {
            "Lead": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "integer"
                    },
                    "name": {
                        "type": "string"
                    },
                    "email": {
                        "type": "string"
                    },
                    "phone": {
                        "type": "string"
                    },
                    "message": {
                        "type": "string"
                    },
                    "source": {
                        "type": "string"
                    },
                    "stage": {
                        "type": "string",
                        "enum": [
                            "nuevo",
                            "contactado",
                            "propuesta",
                            "ganado",
                            "perdido"
                        ]
                    },
                    "value_amount": {
                        "type": "number",
                        "nullable": true
                    },
                    "notes": {
                        "type": "string"
                    },
                    "created_at": {
                        "type": "string"
                    },
                    "updated_at": {
                        "type": "string"
                    }
                }
            },
            "Task": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "integer"
                    },
                    "lead_id": {
                        "type": "integer",
                        "nullable": true
                    },
                    "title": {
                        "type": "string"
                    },
                    "due_at": {
                        "type": "string",
                        "nullable": true
                    },
                    "done": {
                        "type": "boolean"
                    }
                }
            }
        }
    },
    "security": [
        {
            "ApiKey": []
        }
    ],
    "paths": {
        "/me": {
            "get": {
                "summary": "Datos de la cuenta",
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object"
                                }
                            }
                        }
                    }
                }
            }
        },
        "/leads": {
            "get": {
                "summary": "Listar contactos",
                "parameters": [
                    {
                        "name": "stage",
                        "in": "query",
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "since",
                        "in": "query",
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "q",
                        "in": "query",
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "email",
                        "in": "query",
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "phone",
                        "in": "query",
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "limit",
                        "in": "query",
                        "schema": {
                            "type": "string"
                        }
                    },
                    {
                        "name": "page",
                        "in": "query",
                        "schema": {
                            "type": "string"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "array",
                                    "items": {
                                        "$ref": "#/components/schemas/Lead"
                                    }
                                }
                            }
                        }
                    }
                }
            },
            "post": {
                "summary": "Crear contacto (dedupe=1 actualiza si ya existe por email o teléfono)",
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "name": {
                                        "type": "string"
                                    },
                                    "email": {
                                        "type": "string"
                                    },
                                    "phone": {
                                        "type": "string"
                                    },
                                    "message": {
                                        "type": "string"
                                    },
                                    "source": {
                                        "type": "string"
                                    },
                                    "stage": {
                                        "type": "string"
                                    },
                                    "value_amount": {
                                        "type": "number"
                                    },
                                    "dedupe": {
                                        "type": "boolean"
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "201": {
                        "description": "Creado"
                    }
                }
            }
        },
        "/leads/{id}": {
            "parameters": [
                {
                    "name": "id",
                    "in": "path",
                    "required": true,
                    "schema": {
                        "type": "integer"
                    }
                }
            ],
            "get": {
                "summary": "Ver contacto",
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Lead"
                                }
                            }
                        }
                    }
                }
            },
            "patch": {
                "summary": "Actualizar contacto",
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "name": {
                                        "type": "string"
                                    },
                                    "email": {
                                        "type": "string"
                                    },
                                    "phone": {
                                        "type": "string"
                                    },
                                    "stage": {
                                        "type": "string"
                                    },
                                    "value_amount": {
                                        "type": "number"
                                    },
                                    "notes": {
                                        "type": "string"
                                    },
                                    "append_note": {
                                        "type": "string"
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Lead"
                                }
                            }
                        }
                    }
                }
            },
            "delete": {
                "summary": "Borrar contacto",
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object"
                                }
                            }
                        }
                    }
                }
            }
        },
        "/tasks": {
            "get": {
                "summary": "Listar tareas",
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "array",
                                    "items": {
                                        "$ref": "#/components/schemas/Task"
                                    }
                                }
                            }
                        }
                    }
                }
            },
            "post": {
                "summary": "Crear tarea",
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "title": {
                                        "type": "string"
                                    },
                                    "lead_id": {
                                        "type": "integer"
                                    },
                                    "due_at": {
                                        "type": "string"
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "201": {
                        "description": "Creada"
                    }
                }
            }
        },
        "/tasks/{id}": {
            "parameters": [
                {
                    "name": "id",
                    "in": "path",
                    "required": true,
                    "schema": {
                        "type": "integer"
                    }
                }
            ],
            "patch": {
                "summary": "Actualizar tarea",
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "title": {
                                        "type": "string"
                                    },
                                    "due_at": {
                                        "type": "string"
                                    },
                                    "done": {
                                        "type": "boolean"
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Task"
                                }
                            }
                        }
                    }
                }
            },
            "delete": {
                "summary": "Borrar tarea",
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object"
                                }
                            }
                        }
                    }
                }
            }
        },
        "/events": {
            "get": {
                "summary": "Actividad reciente",
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "array"
                                }
                            }
                        }
                    }
                }
            }
        },
        "/webhooks": {
            "get": {
                "summary": "Listar webhooks",
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "array"
                                }
                            }
                        }
                    }
                }
            },
            "post": {
                "summary": "Suscribir un webhook (REST hooks para Make, n8n y Zapier)",
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "url": {
                                        "type": "string"
                                    },
                                    "events": {
                                        "type": "array",
                                        "items": {
                                            "type": "string",
                                            "enum": [
                                                "*",
                                                "lead.created",
                                                "lead.updated",
                                                "lead.deleted",
                                                "task.created",
                                                "task.updated",
                                                "call.logged",
                                                "message.received",
                                                "order.created",
                                                "cart.abandoned"
                                            ]
                                        }
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "201": {
                        "description": "Creado; devuelve secret para validar la firma X-Nex-Signature"
                    }
                }
            }
        },
        "/webhooks/{id}": {
            "parameters": [
                {
                    "name": "id",
                    "in": "path",
                    "required": true,
                    "schema": {
                        "type": "integer"
                    }
                }
            ],
            "delete": {
                "summary": "Cancelar webhook",
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object"
                                }
                            }
                        }
                    }
                }
            }
        },
        "/webhooks/{id}/test": {
            "parameters": [
                {
                    "name": "id",
                    "in": "path",
                    "required": true,
                    "schema": {
                        "type": "integer"
                    }
                }
            ],
            "post": {
                "summary": "Enviar evento de prueba",
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object"
                                }
                            }
                        }
                    }
                }
            }
        },
        "/hooks/{source}": {
            "parameters": [
                {
                    "name": "source",
                    "in": "path",
                    "required": true,
                    "schema": {
                        "type": "string",
                        "example": "aircall"
                    }
                }
            ],
            "post": {
                "summary": "Entrada universal: Aircall, WhatsApp Cloud API, Make, n8n, Zapier, formularios, intranets u otro CRM. Crea o actualiza el contacto por email o teléfono.",
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object"
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "OK",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}