# email_inbox.history_import

Importar o histórico de uma caixa de e-mail. Pede a importação das conversas recentes que já estavam na caixa (`days`: 7 ou 30). O worker lê em segundo plano e acompanha em `historyImport` (`total`, `processed`, `completedAt`) de `email_inbox.get`. Os e-mails entram com a data original, sem não lida, sem janela e sem disparar automações. Uma vez por caixa: repetir dá `limit_reached` com `EMAIL_HISTORY_ALREADY_REQUESTED`. Inclui a pasta Enviados se `syncSentFolder` estiver ligado.

- Endpoint: `POST https://app.atomiks.com.br/api/v1/email_inbox.history_import` (corpo JSON)
- Quem pode chamar: administrador da conta
- Escopo da chave: `channels:write`
- Grava dados: sim
- Pela chave de API: sim
- Ferramenta MCP: `email_inbox_history_import`
- Ficha em JSON: `https://app.atomiks.com.br/api/v1/openapi.json` (operationId `email_inbox.history_import`)

## Entrada

| Campo | Tipo | Obrigatório | Nota |
|---|---|---|---|
| `id` | string (uuid) | sim |  |
| `days` | 7 \| 30 | sim |  |

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

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "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)$"
    },
    "days": {
      "anyOf": [
        {
          "type": "number",
          "const": 7
        },
        {
          "type": "number",
          "const": 30
        }
      ]
    }
  },
  "required": [
    "id",
    "days"
  ]
}
```

</details>

## Exemplo

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

CLI:

```bash
node cli/atomiks.mjs email_inbox.history_import --json '{"id":"00000000-0000-0000-0000-000000000000","days":7}'
```

MCP: ferramenta `email_inbox_history_import` 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": {
    "channelInstanceId": {
      "type": "string"
    },
    "emailAddress": {
      "type": "string"
    },
    "imapHost": {
      "type": "string"
    },
    "imapPort": {
      "type": "number"
    },
    "imapSecure": {
      "type": "boolean"
    },
    "imapUsername": {
      "type": "string"
    },
    "smtpHost": {
      "type": "string"
    },
    "smtpPort": {
      "type": "number"
    },
    "smtpSecure": {
      "type": "boolean"
    },
    "smtpUsername": {
      "type": "string"
    },
    "lastSuccessAt": {
      "type": [
        "string",
        "null"
      ]
    },
    "failingSince": {
      "type": [
        "string",
        "null"
      ]
    },
    "lastErrorCode": {
      "type": [
        "string",
        "null"
      ]
    },
    "lastError": {
      "type": [
        "string",
        "null"
      ]
    },
    "consecutiveFailures": {
      "type": "number"
    },
    "fromName": {
      "type": [
        "string",
        "null"
      ]
    },
    "syncSentFolder": {
      "type": "boolean"
    },
    "signatureText": {
      "type": [
        "string",
        "null"
      ]
    },
    "signatureLogoUrl": {
      "type": [
        "string",
        "null"
      ]
    },
    "historyImport": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "since": {
              "type": "string"
            },
            "total": {
              "type": [
                "number",
                "null"
              ]
            },
            "processed": {
              "type": "number"
            },
            "completedAt": {
              "type": [
                "string",
                "null"
              ]
            }
          },
          "required": [
            "since",
            "total",
            "processed",
            "completedAt"
          ],
          "additionalProperties": false
        },
        {
          "type": "null"
        }
      ]
    },
    "createdAt": {
      "type": "string"
    }
  },
  "required": [
    "channelInstanceId",
    "emailAddress",
    "imapHost",
    "imapPort",
    "imapSecure",
    "imapUsername",
    "smtpHost",
    "smtpPort",
    "smtpSecure",
    "smtpUsername",
    "lastSuccessAt",
    "failingSince",
    "lastErrorCode",
    "lastError",
    "consecutiveFailures",
    "fromName",
    "syncSentFolder",
    "signatureText",
    "signatureLogoUrl",
    "historyImport",
    "createdAt"
  ],
  "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).
