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

# Automated groups

> Open a WhatsApp group for each ticket, order, or booking, and close it when the case ends.

A common flow: something happens in your system, a group opens for it, the right people join, and the group closes when the case is done.\

You run the steps from your own code. Groups give you the building blocks.

Before you start:

* The number [can use groups](https://docs.hookmyapp.com/whatsapp/groups/overview.md#who-can-use-groups).
* You have an approved [group invite template](https://docs.hookmyapp.com/whatsapp/groups/send.md#invite-people-with-a-template).
* You subscribed to `whatsapp.group.updated` with an [event subscription](https://docs.hookmyapp.com/whatsapp/event-subscriptions.md).

## The flow

**1. A ticket opens: create the group**

Create the group and keep the `requestId` next to your ticket.

```bash theme={null}
curl -X POST https://api.hookmyapp.com/channels/ch_AbCdEf12/whatsapp/groups \
  -H "Authorization: Bearer hmok_..." \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: ticket-1042" \
  -d '{"subject":"Ticket 1042","joinApprovalMode":"auto_approve"}'
```

The `Idempotency-Key` protects against network retries. Send the same key again within 24 hours and you get the original answer, not a second group.

**2. Wait for the created event**

Find the `whatsapp.group.updated` event whose `requestId` matches, with `change` set to `created`.\\

Save its `groupId` on the ticket. If `change` is `create_failed`, read `error`, then create again with a new key, such as `ticket-1042-2`. The old key would replay the failed attempt.

**3. Send the invite 1:1**

Send the [invite template](https://docs.hookmyapp.com/whatsapp/groups/send.md#invite-people-with-a-template) to each person, with the `groupId` as the `group_id` parameter.\\

People join through the link, so you never add them yourself.

**4. Greet people when they join**

On `member_joined`, send a message to the group.

```bash theme={null}
curl -X POST "https://gateway.hookmyapp.com/meta/v22.0/${WHATSAPP_PHONE_NUMBER_ID}/messages" \
  -H "Authorization: Bearer ${WHATSAPP_ACCESS_TOKEN}" \
  -H "Content-Type: application/json" \
  -d '{
  "messaging_product": "whatsapp",
  "recipient_type": "group",
  "to": "GROUP_ID",
  "type": "text",
  "text": { "body": "Welcome. Someone from the team will reply here." }
}'
```

The event's `member` tells you who joined.

**5. The ticket closes: delete the group**

Delete the group. Its members are removed.

```bash theme={null}
curl -X DELETE https://api.hookmyapp.com/channels/ch_AbCdEf12/whatsapp/groups/GROUP_ID \
  -H "Authorization: Bearer hmok_..."
```

The call returns `202` with a `requestId`. A `deleted` event with the same `requestId` and `succeeded: true` confirms it.

## Tips

* Use `approval_required` when only some people should join. Approve them on `join_requested`. See [join requests](https://docs.hookmyapp.com/whatsapp/groups/manage.md#join-requests).
* Events can arrive more than once. Drop duplicates by the event `id`.
* Keep the ticket, `requestId`, and `groupId` together so you can find the group again when the case closes.
* A group holds 8 members. Plan for that when you invite.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.