# attribution.export

Exportar leads e vendas atribuídos a anúncios (sem dado pessoal). Para o RealMetrics puxar a atribuição do Atomiks. `kind` `leads`: um item por contato cuja PRIMEIRA indicação de anúncio (com `source_id`) caiu na janela: `{contactId, firstAt, adId, ctwaClid, sourceType}`. `kind` `sales`: um item por venda com `sold_at` na janela, canceladas inclusive (`cancelledAt` preenchido), só de contatos com primeira indicação de anúncio; `adId` é o anúncio dessa primeira indicação: `{saleId, contactId, soldAt, valueCents, cancelledAt, adId}`. `adId` é o id do anúncio na Meta; `valueCents` em centavos de BRL; datas em ISO-8601 com fuso. `from` e `to` são datas `AAAA-MM-DD` (até 93 dias) no fuso da conta de anúncios (`timezone` na saída; padrão America/Sao_Paulo). Itens ordenados por `contactId` (leads) ou `saleId` (vendas). `limit` de 1 a 1000, padrão 500. Paginação: repita a chamada com `cursor` = `nextCursor` até ele vir nulo; página com menos itens que `limit` não significa fim, o fim é `nextCursor` nulo. Cursor adulterado ou de outro `kind` dá `invalid_input`. Nenhum nome, telefone ou e-mail sai por aqui.

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

## Entrada

| Campo | Tipo | Obrigatório | Nota |
|---|---|---|---|
| `kind` | "leads" \| "sales" | sim |  |
| `from` | string | sim |  |
| `to` | string | sim |  |
| `cursor` | string | não |  |
| `limit` | integer | não |  |

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

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "kind": {
      "type": "string",
      "enum": [
        "leads",
        "sales"
      ]
    },
    "from": {
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
    },
    "to": {
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
    },
    "cursor": {
      "type": "string",
      "minLength": 1,
      "maxLength": 500
    },
    "limit": {
      "type": "integer",
      "minimum": 1,
      "maximum": 1000
    }
  },
  "required": [
    "kind",
    "from",
    "to"
  ]
}
```

</details>

## Exemplo

```bash
curl -X POST https://app.atomiks.com.br/api/v1/attribution.export \
  -H "Authorization: Bearer $ATOMIKS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"kind":"leads","from":"2026-10-01","to":"2026-10-07"}'
```

CLI:

```bash
node cli/atomiks.mjs attribution.export --json '{"kind":"leads","from":"2026-10-01","to":"2026-10-07"}'
```

MCP: ferramenta `attribution_export` 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": {
    "items": {
      "type": "array",
      "items": {
        "anyOf": [
          {
            "type": "object",
            "properties": {
              "contactId": {
                "type": "string"
              },
              "firstAt": {
                "type": "string"
              },
              "adId": {
                "type": "string"
              },
              "ctwaClid": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "sourceType": {
                "type": [
                  "string",
                  "null"
                ]
              }
            },
            "required": [
              "contactId",
              "firstAt",
              "adId",
              "ctwaClid",
              "sourceType"
            ],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "saleId": {
                "type": "string"
              },
              "contactId": {
                "type": "string"
              },
              "soldAt": {
                "type": "string"
              },
              "valueCents": {
                "type": "integer",
                "exclusiveMinimum": 0,
                "maximum": 9007199254740991
              },
              "cancelledAt": {
                "type": [
                  "string",
                  "null"
                ]
              },
              "adId": {
                "type": "string"
              }
            },
            "required": [
              "saleId",
              "contactId",
              "soldAt",
              "valueCents",
              "cancelledAt",
              "adId"
            ],
            "additionalProperties": false
          }
        ]
      }
    },
    "nextCursor": {
      "type": [
        "string",
        "null"
      ]
    },
    "timezone": {
      "type": "string"
    }
  },
  "required": [
    "items",
    "nextCursor",
    "timezone"
  ],
  "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).
