# custom_fields.insights

Uso de um campo customizado. As abas Uso e Onde é usado: `fillRate`, `fillCount` de `totalEntities` (contatos ou oportunidades da conta), `emptyCount`, `filledLast30Days`, `trend` (curva acumulada dos últimos 6 meses: `month` AAAA-MM, `filled`, `fillRate`), `numeric` (mínimo, média, máximo e soma, só campos numéricos), `distinctValues`, `topValues` (`label`, `count`, `share`) e `references` (segmentos, automações e agendas que citam o campo; é o que pararia ao inativar). `lastEditedAt` é o último preenchimento.

- Endpoint: `POST https://app.atomiks.com.br/api/v1/custom_fields.insights` (corpo JSON)
- Quem pode chamar: membro da conta
- Escopo da chave: `crm:read`
- Grava dados: não
- Pela chave de API: sim
- Ferramenta MCP: `custom_fields_insights`
- Ficha em JSON: `https://app.atomiks.com.br/api/v1/openapi.json` (operationId `custom_fields.insights`)

## Entrada

| Campo | Tipo | Obrigatório | Nota |
|---|---|---|---|
| `entityType` | "leads" \| "opportunities" | sim |  |
| `id` | string (uuid) | sim |  |

<details><summary>JSON Schema</summary>

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "entityType": {
      "type": "string",
      "enum": [
        "leads",
        "opportunities"
      ]
    },
    "id": {
      "type": "string",
      "format": "uuid",
      "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
    }
  },
  "required": [
    "entityType",
    "id"
  ]
}
```

</details>

## Exemplo

```bash
curl -X POST https://app.atomiks.com.br/api/v1/custom_fields.insights \
  -H "Authorization: Bearer $ATOMIKS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"entityType":"leads","id":"00000000-0000-0000-0000-000000000000"}'
```

CLI:

```bash
node cli/atomiks.mjs custom_fields.insights --json '{"entityType":"leads","id":"00000000-0000-0000-0000-000000000000"}'
```

MCP: ferramenta `custom_fields_insights` com a mesma entrada.

## Saída

Sucesso: `{"data": ...}` com o formato abaixo.

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "fillRate": {
      "type": "number"
    },
    "fillCount": {
      "type": "number"
    },
    "totalEntities": {
      "type": "number"
    },
    "emptyCount": {
      "type": "number"
    },
    "filledLast30Days": {
      "type": "number"
    },
    "trend": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "month": {
            "type": "string"
          },
          "filled": {
            "type": "number"
          },
          "fillRate": {
            "type": "number"
          }
        },
        "required": [
          "month",
          "filled",
          "fillRate"
        ],
        "additionalProperties": false
      }
    },
    "numeric": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "min": {
              "type": "number"
            },
            "max": {
              "type": "number"
            },
            "sum": {
              "type": "number"
            },
            "avg": {
              "type": "number"
            }
          },
          "required": [
            "min",
            "max",
            "sum",
            "avg"
          ],
          "additionalProperties": false
        },
        {
          "type": "null"
        }
      ]
    },
    "distinctValues": {
      "type": "number"
    },
    "topValues": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "label": {
            "type": "string"
          },
          "count": {
            "type": "number"
          },
          "share": {
            "type": "number"
          }
        },
        "required": [
          "label",
          "count",
          "share"
        ],
        "additionalProperties": false
      }
    },
    "references": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "kind": {
            "type": "string",
            "enum": [
              "segment",
              "flow",
              "schedule"
            ]
          },
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "meta": {
            "type": [
              "string",
              "null"
            ]
          },
          "parentId": {
            "type": [
              "string",
              "null"
            ]
          }
        },
        "required": [
          "kind",
          "id",
          "name",
          "meta",
          "parentId"
        ],
        "additionalProperties": false
      }
    },
    "lastEditedAt": {
      "type": [
        "string",
        "null"
      ]
    }
  },
  "required": [
    "fillRate",
    "fillCount",
    "totalEntities",
    "emptyCount",
    "filledLast30Days",
    "trend",
    "numeric",
    "distinctValues",
    "topValues",
    "references",
    "lastEditedAt"
  ],
  "additionalProperties": false
}
```

## Erros

`{"error": {"code", "message", "requestId"}}`. Códigos: `invalid_input` (400), `unauthorized` (401), `forbidden` / `account_pending` / `scope_missing` (403), `not_found` (404), `limit_reached` (409), `internal` (500).
