> ## Documentation Index
> Fetch the complete documentation index at: https://docs.leavo.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Definições de Campos

> Criar e gerenciar definições de campos personalizados

## Criar Definição

<Card>
  <strong>POST</strong> `/backend/custom-fields`
</Card>

### Request Body

<ParamField body="field_key" type="string" required>
  Chave única do campo (usada em variáveis)
</ParamField>

<ParamField body="field_label" type="string" required>
  Rótulo exibido na interface
</ParamField>

<ParamField body="field_description" type="string">
  Descrição do campo
</ParamField>

<ParamField body="field_type" type="string" required>
  Tipo: `text`, `number`, `date`, `boolean`, `select`
</ParamField>

<ParamField body="options" type="array">
  Opções disponíveis (obrigatório para tipo `select`)
</ParamField>

<ParamField body="required" type="boolean" default="false">
  Se o campo é obrigatório
</ParamField>

<ParamField body="default_value" type="any">
  Valor padrão
</ParamField>

<ParamField body="display_order" type="integer">
  Ordem de exibição
</ParamField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST "https://api.leavo.ai/backend/custom-fields" \
    -H "Authorization: Bearer sua_chave_aqui" \
    -H "Content-Type: application/json" \
    -d '{
      "field_key": "origem_lead",
      "field_label": "Origem do Lead",
      "field_description": "Como o lead chegou até nós",
      "field_type": "select",
      "options": ["Site", "Indicação", "Evento", "Anúncio", "Outro"],
      "required": false,
      "default_value": "Site",
      "display_order": 1
    }'
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch('https://api.leavo.ai/backend/custom-fields', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer sua_chave_aqui',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      field_key: 'origem_lead',
      field_label: 'Origem do Lead',
      field_description: 'Como o lead chegou até nós',
      field_type: 'select',
      options: ['Site', 'Indicação', 'Evento', 'Anúncio', 'Outro'],
      required: false,
      default_value: 'Site',
      display_order: 1
    })
  });
  ```
</RequestExample>

<ResponseExample>
  ```json 201 Created theme={null}
  {
    "id": "uuid",
    "tenant_id": "uuid",
    "field_key": "origem_lead",
    "field_label": "Origem do Lead",
    "field_description": "Como o lead chegou até nós",
    "field_type": "select",
    "options": ["Site", "Indicação", "Evento", "Anúncio", "Outro"],
    "required": false,
    "default_value": "Site",
    "display_order": 1,
    "is_active": true,
    "created_at": "2024-01-01T00:00:00Z"
  }
  ```
</ResponseExample>

***

## Listar Definições

<Card>
  <strong>GET</strong> `/backend/custom-fields`
</Card>

### Query Parameters

<ParamField query="active_only" type="boolean" default="false">
  Filtrar apenas campos ativos
</ParamField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X GET "https://api.leavo.ai/backend/custom-fields?active_only=true" \
    -H "Authorization: Bearer sua_chave_aqui"
  ```
</RequestExample>

***

## Obter Variáveis

<Card>
  <strong>GET</strong> `/backend/custom-fields/variables`
</Card>

Retorna todas as variáveis disponíveis para uso em prompts e mensagens.

<ResponseExample>
  ```json 200 OK theme={null}
  {
    "standard_variables": [
      {"key": "{{LEAD_NAME}}", "description": "Nome completo do lead"},
      {"key": "{{LEAD_FIRSTNAME}}", "description": "Primeiro nome do lead"},
      {"key": "{{LEAD_NUMBER}}", "description": "Telefone do lead"},
      {"key": "{{COMPANY_NAME}}", "description": "Nome da empresa"}
    ],
    "custom_variables": [
      {"key": "{{LEAD_ORIGEM_LEAD}}", "label": "Origem do Lead", "type": "select"}
    ]
  }
  ```
</ResponseExample>

***

## Atualizar/Deletar Definição

<Card>
  <strong>PUT</strong> `/backend/custom-fields/{id}`
</Card>

<Card>
  <strong>DELETE</strong> `/backend/custom-fields/{id}`
</Card>

<ParamField path="id" type="string" required>
  UUID da definição do campo
</ParamField>

<Warning>
  Deletar uma definição remove todos os valores salvos para esse campo em todos os leads.
</Warning>
