> ## 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.

# Criar Cadência

> Cria uma nova sequência automatizada de mensagens

## Request Body

<ParamField body="name" type="string" required>
  Nome da cadência
</ParamField>

<ParamField body="description" type="string">
  Descrição da cadência
</ParamField>

<ParamField body="steps" type="array" required>
  Lista de etapas da cadência
</ParamField>

<Expandable title="Propriedades de Step">
  <ParamField body="name" type="string" required>
    Nome da etapa
  </ParamField>

  <ParamField body="order" type="integer" required>
    Ordem de execução (começando em 1)
  </ParamField>

  <ParamField body="interval" type="string" required>
    Intervalo até esta etapa (ISO 8601: P1D = 1 dia, P2D = 2 dias)
  </ParamField>

  <ParamField body="messages" type="array" required>
    Lista de mensagens a enviar nesta etapa
  </ParamField>
</Expandable>

<Expandable title="Propriedades de Message">
  <ParamField body="type" type="string" required>
    Tipo: `text`, `image`, `video`, `audio`, `document`
  </ParamField>

  <ParamField body="content" type="string">
    Conteúdo da mensagem (para text) ou legenda (para mídia)
  </ParamField>

  <ParamField body="url" type="string">
    URL da mídia (obrigatório para tipos diferentes de text)
  </ParamField>

  <ParamField body="delay" type="integer" default="0">
    Delay em segundos antes de enviar esta mensagem
  </ParamField>
</Expandable>

<ParamField body="delivery_config" type="object">
  Configuração de janelas de envio
</ParamField>

<ParamField body="rate_limit" type="object">
  Configuração de limite de envios
</ParamField>

<RequestExample>
  ```bash cURL theme={null}
  curl -X POST "https://api.leavo.ai/backend/cadences" \
    -H "Authorization: Bearer sua_chave_aqui" \
    -H "Content-Type: application/json" \
    -d '{
      "name": "Follow-up 7 dias",
      "description": "Sequência de acompanhamento",
      "steps": [
        {
          "name": "Dia 1 - Introdução",
          "order": 1,
          "interval": "P1D",
          "messages": [
            {
              "type": "text",
              "content": "Olá {{LEAD_FIRSTNAME}}! Tudo bem?",
              "delay": 0
            }
          ]
        },
        {
          "name": "Dia 3 - Follow-up",
          "order": 2,
          "interval": "P2D",
          "messages": [
            {
              "type": "text",
              "content": "{{LEAD_FIRSTNAME}}, conseguiu avaliar nossa proposta?"
            }
          ]
        },
        {
          "name": "Dia 7 - Última tentativa",
          "order": 3,
          "interval": "P4D",
          "messages": [
            {
              "type": "text",
              "content": "{{LEAD_FIRSTNAME}}, ainda temos interesse em ajudar você!"
            }
          ]
        }
      ],
      "delivery_config": {
        "day_windows": {
          "1": {"start_hour": 9, "end_hour": 18},
          "2": {"start_hour": 9, "end_hour": 18},
          "3": {"start_hour": 9, "end_hour": 18},
          "4": {"start_hour": 9, "end_hour": 18},
          "5": {"start_hour": 9, "end_hour": 18}
        },
        "time_zone": "America/Sao_Paulo"
      },
      "rate_limit": {
        "enabled": true,
        "limit_type": "daily",
        "max_leads": 100
      }
    }'
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch('https://api.leavo.ai/backend/cadences', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer sua_chave_aqui',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      name: 'Follow-up 7 dias',
      description: 'Sequência de acompanhamento',
      steps: [
        {
          name: 'Dia 1 - Introdução',
          order: 1,
          interval: 'P1D',
          messages: [
            {
              type: 'text',
              content: 'Olá {{LEAD_FIRSTNAME}}! Tudo bem?',
              delay: 0
            }
          ]
        },
        {
          name: 'Dia 3 - Follow-up',
          order: 2,
          interval: 'P2D',
          messages: [
            {
              type: 'text',
              content: '{{LEAD_FIRSTNAME}}, conseguiu avaliar nossa proposta?'
            }
          ]
        }
      ],
      delivery_config: {
        day_windows: {
          '1': { start_hour: 9, end_hour: 18 },
          '2': { start_hour: 9, end_hour: 18 },
          '3': { start_hour: 9, end_hour: 18 },
          '4': { start_hour: 9, end_hour: 18 },
          '5': { start_hour: 9, end_hour: 18 }
        },
        time_zone: 'America/Sao_Paulo'
      },
      rate_limit: {
        enabled: true,
        limit_type: 'daily',
        max_leads: 100
      }
    })
  });
  ```
</RequestExample>

<ResponseExample>
  ```json 201 Created theme={null}
  {
    "id": "uuid",
    "tenant_id": "uuid",
    "name": "Follow-up 7 dias",
    "description": "Sequência de acompanhamento",
    "is_active": false,
    "steps": [...],
    "delivery_config": {...},
    "rate_limit": {...},
    "created_at": "2024-01-01T00:00:00Z",
    "updated_at": "2024-01-01T00:00:00Z"
  }
  ```
</ResponseExample>

## Intervalos (ISO 8601)

| Valor   | Significado   |
| ------- | ------------- |
| `P0D`   | Imediatamente |
| `P1D`   | 1 dia         |
| `P2D`   | 2 dias        |
| `P1W`   | 1 semana      |
| `PT1H`  | 1 hora        |
| `PT30M` | 30 minutos    |

## Janelas de Envio

Os números das chaves em `day_windows` representam dias da semana:

* `1` = Segunda-feira
* `2` = Terça-feira
* `3` = Quarta-feira
* `4` = Quinta-feira
* `5` = Sexta-feira
* `6` = Sábado
* `0` = Domingo

<Note>
  Mensagens agendadas fora das janelas de envio serão enviadas no próximo horário disponível.
</Note>
