{
  "openapi": "3.0.3",
  "info": {
    "title": "MoraBe Imob — Recebimento de leads",
    "version": "1.0.0",
    "description": "Guia público: https://morabe.com.br/api/. Integração entre servidores; somente leads:create. Ajuste o servidor antes de usar. Guarde o token no servidor. 60 chamadas/minuto por conexão e 120/minuto por origem de rede. Não lista, edita ou exclui atendimentos.",
    "contact": {
      "name": "MoraBe por Human2Be",
      "url": "https://morabe.com.br/"
    }
  },
  "servers": [
    {
      "url": "https://{workspace}/api/v1",
      "description": "Substitua pelo host do MoraBe da empresa.",
      "variables": {
        "workspace": {
          "default": "morabe.sua-imobiliaria.example",
          "description": "Host HTTPS do MoraBe, sem protocolo nem caminhos."
        }
      }
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "tags": [
    {
      "name": "Leads",
      "description": "Recebimento de formulários externos."
    }
  ],
  "paths": {
    "/leads/schema": {
      "get": {
        "operationId": "getLeadConnection",
        "tags": [
          "Leads"
        ],
        "summary": "Consultar configuração da conexão",
        "description": "Não lista leads. ready verifica módulo e etapa inicial; a distribuição é conferida ao criar.",
        "responses": {
          "200": {
            "description": "Configuração privada desta conexão.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "version",
                    "connection",
                    "site_url",
                    "pipeline",
                    "initial_stage",
                    "ready",
                    "scope",
                    "max_body_bytes",
                    "required"
                  ],
                  "properties": {
                    "version": {
                      "type": "string",
                      "enum": [
                        "1"
                      ]
                    },
                    "connection": {
                      "type": "string"
                    },
                    "site_url": {
                      "type": "string",
                      "format": "uri"
                    },
                    "pipeline": {
                      "type": "object",
                      "properties": {
                        "key": {
                          "type": "string"
                        },
                        "name": {
                          "type": "string"
                        }
                      }
                    },
                    "initial_stage": {
                      "type": "object",
                      "nullable": true,
                      "properties": {
                        "key": {
                          "type": "string",
                          "enum": [
                            "new"
                          ]
                        },
                        "name": {
                          "type": "string"
                        }
                      }
                    },
                    "ready": {
                      "type": "boolean"
                    },
                    "scope": {
                      "type": "string",
                      "enum": [
                        "leads:create"
                      ]
                    },
                    "max_body_bytes": {
                      "type": "integer",
                      "enum": [
                        32768
                      ]
                    },
                    "required": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      }
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "HTTPS obrigatório."
          },
          "401": {
            "description": "Chave ausente, inválida ou revogada."
          },
          "429": {
            "description": "Limite temporário; aguarde Retry-After.",
            "headers": {
              "Retry-After": {
                "description": "Segundos até nova tentativa.",
                "schema": {
                  "type": "integer"
                }
              }
            }
          }
        }
      }
    },
    "/leads/validate": {
      "post": {
        "operationId": "validateLead",
        "tags": [
          "Leads"
        ],
        "summary": "Validar sem criar contato ou atendimento",
        "description": "Não consome rodízio; distribuição avaliada somente no envio real.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/LeadInput"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Validação aprovada; nenhum lead criado.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "status": {
                      "type": "string",
                      "enum": [
                        "valid"
                      ]
                    },
                    "pipeline": {
                      "type": "string"
                    },
                    "creates_lead": {
                      "type": "boolean",
                      "enum": [
                        false
                      ]
                    },
                    "routing": {
                      "type": "string",
                      "enum": [
                        "evaluated_on_create"
                      ]
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "HTTPS obrigatório."
          },
          "401": {
            "description": "Chave ausente, inválida ou revogada."
          },
          "413": {
            "description": "JSON acima de 32 KiB."
          },
          "415": {
            "description": "Use Content-Type application/json."
          },
          "422": {
            "description": "Campos inválidos; confira errors por campo."
          },
          "429": {
            "description": "Limite temporário; aguarde Retry-After.",
            "headers": {
              "Retry-After": {
                "description": "Segundos até nova tentativa.",
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "503": {
            "description": "Módulo, etapa ou distribuição indisponível. Preserve o envio para nova tentativa."
          }
        }
      }
    },
    "/leads": {
      "post": {
        "operationId": "createLead",
        "tags": [
          "Leads"
        ],
        "summary": "Receber lead com proteção contra duplicação",
        "description": "external_id estável por envio. Repetições idênticas confirmam sem avançar o rodízio; conteúdo alterado recebe 409. Nova oportunidade para cada novo external_id. Não envia mensagens automaticamente.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/LeadInput"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Criado e confirmado.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Receipt"
                }
              }
            }
          },
          "200": {
            "description": "Mesmo envio já recebido; status duplicate.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Receipt"
                }
              }
            }
          },
          "400": {
            "description": "HTTPS obrigatório."
          },
          "401": {
            "description": "Chave ausente, inválida ou revogada."
          },
          "409": {
            "description": "external_id já utilizado com conteúdo diferente."
          },
          "413": {
            "description": "JSON acima de 32 KiB."
          },
          "415": {
            "description": "Use Content-Type application/json."
          },
          "422": {
            "description": "Campos inválidos; confira errors por campo."
          },
          "429": {
            "description": "Limite temporário; aguarde Retry-After.",
            "headers": {
              "Retry-After": {
                "description": "Segundos até nova tentativa.",
                "schema": {
                  "type": "integer"
                }
              }
            }
          },
          "503": {
            "description": "Módulo, etapa ou distribuição indisponível. Preserve o envio para nova tentativa."
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "Chave privada gerada no painel do MoraBe. Não incluída neste arquivo."
      }
    },
    "schemas": {
      "LeadInput": {
        "type": "object",
        "required": [
          "external_id",
          "name",
          "page_url",
          "consent"
        ],
        "anyOf": [
          {
            "required": [
              "phone"
            ],
            "properties": {
              "phone": {
                "type": "string",
                "nullable": false
              }
            }
          },
          {
            "required": [
              "email"
            ],
            "properties": {
              "email": {
                "type": "string",
                "nullable": false
              }
            }
          }
        ],
        "properties": {
          "external_id": {
            "type": "string",
            "maxLength": 120,
            "pattern": "^[A-Za-z0-9_.:-]+$"
          },
          "name": {
            "type": "string",
            "minLength": 2,
            "maxLength": 150
          },
          "phone": {
            "type": "string",
            "nullable": true,
            "pattern": "^\\+[1-9][0-9]{7,14}$"
          },
          "email": {
            "type": "string",
            "nullable": true,
            "format": "email",
            "maxLength": 254
          },
          "page_url": {
            "type": "string",
            "format": "uri",
            "maxLength": 2048,
            "pattern": "^https://",
            "description": "Domínio autorizado, com ou sem www. Sem usuário, senha ou porta. Parâmetros e fragmentos removidos ao salvar."
          },
          "interest": {
            "type": "string",
            "nullable": true,
            "maxLength": 500,
            "description": "Interesse textual, sem vínculo automático com imóvel interno."
          },
          "message": {
            "type": "string",
            "nullable": true,
            "maxLength": 3000
          },
          "pipeline": {
            "type": "string",
            "enum": [
              "triage",
              "sale",
              "rent",
              "capture",
              "international"
            ],
            "description": "Opcional; deve ser o mesmo da conexão."
          },
          "consent": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "contact",
              "at",
              "notice_version"
            ],
            "properties": {
              "contact": {
                "type": "boolean",
                "enum": [
                  true
                ]
              },
              "analytics": {
                "type": "boolean",
                "default": false
              },
              "at": {
                "type": "string",
                "format": "date-time",
                "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}[+-]\\d{2}:\\d{2}$",
                "description": "Autorização real, com segundos e fuso ±HH:MM. Até 5 minutos no futuro."
              },
              "notice_version": {
                "type": "string",
                "minLength": 1,
                "maxLength": 100
              }
            }
          },
          "utm": {
            "type": "object",
            "additionalProperties": false,
            "description": "Descartado quando consent.analytics não for true.",
            "properties": {
              "utm_source": {
                "type": "string",
                "nullable": true,
                "maxLength": 120
              },
              "utm_medium": {
                "type": "string",
                "nullable": true,
                "maxLength": 120
              },
              "utm_campaign": {
                "type": "string",
                "nullable": true,
                "maxLength": 120
              },
              "utm_term": {
                "type": "string",
                "nullable": true,
                "maxLength": 120
              },
              "utm_content": {
                "type": "string",
                "nullable": true,
                "maxLength": 120
              },
              "utm_id": {
                "type": "string",
                "nullable": true,
                "maxLength": 120
              }
            }
          }
        },
        "description": "Pelo menos phone ou email não nulo. Campos de controle assigned_to, kind, stage, stage_id, property_id e agreed_value são proibidos. Corpo máximo 32768 bytes.",
        "example": {
          "external_id": "form-550e8400-e29b-41d4-a716-446655440000",
          "name": "Contato de exemplo",
          "phone": "+12025550100",
          "email": "contato@example.com",
          "page_url": "https://www.example.com/imovel-exemplo",
          "interest": "Nome ou referência do imóvel de interesse",
          "message": "Gostaria de receber informações.",
          "consent": {
            "contact": true,
            "analytics": false,
            "at": "2026-11-19T10:30:00-03:00",
            "notice_version": "aviso-exemplo-v1"
          }
        }
      },
      "Receipt": {
        "type": "object",
        "required": [
          "status",
          "reference",
          "pipeline"
        ],
        "properties": {
          "status": {
            "type": "string",
            "enum": [
              "created",
              "duplicate"
            ]
          },
          "reference": {
            "type": "string",
            "format": "uuid"
          },
          "pipeline": {
            "type": "string",
            "enum": [
              "triage",
              "sale",
              "rent",
              "capture",
              "international"
            ]
          }
        }
      }
    }
  }
}
