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

# Gerenciar Templates

> Listar, consultar e sincronizar templates do WhatsApp Business

## Listar Templates

```bash theme={null}
GET /backend/templates
```

### Filtros (Query Parameters)

| Parâmetro        | Tipo   | Descrição                                                        |
| ---------------- | ------ | ---------------------------------------------------------------- |
| `gupshup_app_id` | string | Filtrar por app Gupshup                                          |
| `status`         | string | Filtrar por status (`APPROVED`, `PENDING`, `REJECTED`)           |
| `category`       | string | Filtrar por categoria (`MARKETING`, `UTILITY`, `AUTHENTICATION`) |
| `language`       | string | Filtrar por idioma (ex: `pt_BR`, `en`)                           |

### Exemplo

<CodeGroup>
  ```bash cURL theme={null}
  curl -X GET "https://api.leavo.ai/backend/templates?status=APPROVED&language=pt_BR" \
    -H "Authorization: Bearer sua_chave_aqui"
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch(
    'https://api.leavo.ai/backend/templates?status=APPROVED&language=pt_BR',
    {
      headers: { 'Authorization': 'Bearer sua_chave_aqui' }
    }
  );
  const templates = await response.json();
  ```
</CodeGroup>

**Resposta (200):**

```json theme={null}
[
  {
    "id": "uuid",
    "tenant_id": "uuid",
    "gupshup_app_id": "app_12345",
    "template_name": "boas_vindas_v1",
    "template_id": "gupshup_id",
    "language": "pt_BR",
    "status": "APPROVED",
    "category": "MARKETING",
    "components": [...],
    "last_synced_at": "2024-01-15T10:00:00Z"
  }
]
```

## Obter Template

```bash theme={null}
GET /backend/templates/{id}
```

Retorna os detalhes completos de um template, incluindo todos os componentes.

## Excluir Template

```bash theme={null}
DELETE /backend/templates/{id}
```

<Warning>
  Ao excluir um template, todas as associações com cadências são removidas automaticamente.
</Warning>

**Resposta:** 204 No Content

## Sincronizar Templates

```bash theme={null}
POST /backend/templates/sync
```

Endpoint chamado pelo meta-service para sincronizar templates do Gupshup.

**Body:**

```json theme={null}
{
  "app_id": "app_12345",
  "templates": [
    {
      "id": "gupshup_template_id",
      "name": "boas_vindas_v1",
      "language": "pt_BR",
      "status": "APPROVED",
      "category": "MARKETING",
      "components": [
        {
          "type": "BODY",
          "text": "Olá {{1}}, bem-vindo à {{2}}!"
        }
      ]
    }
  ]
}
```

**Resposta (200):**

```json theme={null}
{
  "synced_count": 5,
  "app_id": "app_12345"
}
```

<Note>
  Templates existentes são atualizados (upsert). Templates novos são criados automaticamente. A chave de unicidade é `(tenant_id, gupshup_app_id, template_name, language)`.
</Note>
