> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hustapp.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Abrir atendimento

> Abra um atendimento ou recupere o atendimento em aberto de um contato

```http theme={null}
POST https://api.hustapp.com/called
```

Abre um atendimento para um contato. Se o contato já tiver um atendimento em aberto, a rota devolve esse atendimento em vez de criar outro.

O atendimento é o ponto de partida para operações vinculadas a ele, como [enviar um template no atendimento](/chat/atendimentos/enviar-template).

## Corpo da requisição

| Campo        | Tipo     | Obrigatório | Descrição                                                                                                     |
| ------------ | -------- | ----------- | ------------------------------------------------------------------------------------------------------------- |
| `contact`    | `object` | Sim         | `{ id }` ou `{ phone }` do contato. O `phone` é normalizado como em [Enviar mensagem](/chat/enviar-mensagem). |
| `connection` | `object` | Sim         | `{ id }` ou `{ uuid }` da conexão.                                                                            |
| `department` | `object` | Sim         | `{ id }` do departamento.                                                                                     |

## Atendente

Quando o atendimento devolvido ainda não tem atendente, o usuário dono do token assume e o atendimento passa para o status de atendimento.

Quando já existe um atendimento em aberto com outro atendente, ele é devolvido como está, sem troca. Verifique o campo `user` da resposta se a sua integração precisa ser a responsável pelo atendimento.

## Exemplo

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST https://api.hustapp.com/called \
    -H "Authorization: Bearer SEU_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
      "contact": { "phone": "5541988887777" },
      "connection": { "uuid": "9db04f68-9e7d-4297-ba41-5f4d4c44779a" },
      "department": { "id": 169070 }
    }'
  ```

  ```javascript Node.js theme={null}
  const res = await fetch("https://api.hustapp.com/called", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${token}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      contact: { phone: "5541988887777" },
      connection: { uuid: "9db04f68-9e7d-4297-ba41-5f4d4c44779a" },
      department: { id: 169070 },
    }),
  });

  const called = await res.json();
  ```

  ```python Python theme={null}
  import requests

  res = requests.post(
      "https://api.hustapp.com/called",
      headers={"Authorization": f"Bearer {token}"},
      json={
          "contact": {"phone": "5541988887777"},
          "connection": {"uuid": "9db04f68-9e7d-4297-ba41-5f4d4c44779a"},
          "department": {"id": 169070},
      },
  )

  called = res.json()
  ```

  ```php PHP theme={null}
  <?php
  $ch = curl_init('https://api.hustapp.com/called');
  curl_setopt_array($ch, [
      CURLOPT_POST => true,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_HTTPHEADER => [
          'Authorization: Bearer ' . $token,
          'Content-Type: application/json',
      ],
      CURLOPT_POSTFIELDS => json_encode([
          'contact' => ['phone' => '5541988887777'],
          'connection' => ['uuid' => '9db04f68-9e7d-4297-ba41-5f4d4c44779a'],
          'department' => ['id' => 169070],
      ]),
  ]);

  $called = json_decode(curl_exec($ch), true);
  curl_close($ch);
  ```

  ```pascal Delphi theme={null}
  uses
    System.Net.HttpClient, System.Classes, System.JSON, System.SysUtils;

  const
    Body =
      '{' +
      '  "contact": { "phone": "5541988887777" },' +
      '  "connection": { "uuid": "9db04f68-9e7d-4297-ba41-5f4d4c44779a" },' +
      '  "department": { "id": 169070 }' +
      '}';

  var
    HttpClient: THTTPClient;
    Response: IHTTPResponse;
    Called: TJSONObject;
    CalledId: Integer;
  begin
    HttpClient := THTTPClient.Create;
    try
      HttpClient.CustomHeaders['Authorization'] := 'Bearer ' + Token;
      HttpClient.ContentType := 'application/json';
      Response := HttpClient.Post('https://api.hustapp.com/called',
        TStringStream.Create(Body, TEncoding.UTF8));

      Called := TJSONObject.ParseJSONValue(Response.ContentAsString) as TJSONObject;
      try
        CalledId := Called.GetValue<Integer>('id');
      finally
        Called.Free;
      end;
    finally
      HttpClient.Free;
    end;
  end;
  ```
</CodeGroup>

## Resposta

Retorna `200` com o atendimento.

| Campo        | Tipo      | Descrição                                                                |
| ------------ | --------- | ------------------------------------------------------------------------ |
| `id`         | `integer` | ID do atendimento. Usado nas demais operações de atendimento.            |
| `date`       | `string`  | Data de abertura, em ISO 8601.                                           |
| `status`     | `string`  | Status do atendimento.                                                   |
| `finished`   | `boolean` | `true` se o atendimento já foi encerrado.                                |
| `contact`    | `object`  | O contato.                                                               |
| `connection` | `object`  | A conexão.                                                               |
| `department` | `object`  | O departamento.                                                          |
| `user`       | `object`  | O atendente responsável. Pode ser outro usuário que não o dono do token. |

## Erros

Qualquer falha responde `400` com o corpo `false`. As causas possíveis são contato, conexão ou departamento ausentes, ou não encontrados na sua empresa.
