# Message groups and templates

Create templates, place them in a message group, and use that group when a job is activated. A group can optionally reference a project-scoped policy group.

## Authentication and project scope

Every route requires a Bearer access token for an active admitted `member`,
`user`, `admin`, or `root` principal, `project_id` (UUID), and the route's exact
capability through active membership in that project. Organization requests also
apply current verified Management grants. See [project access](https://m7.org/docs/api/api.post.m7.org/projects.md).
Resources and name lookups are scoped to the selected project. Do not send
`owner`, `project`, `principal_id`, or `actor_id` overrides.

## Message groups

| Route | Required fields | Optional fields and behavior |
| --- | --- | --- |
| `POST /v1/members/message/group/get` | `project_id`; `id` or `name` | Returns one project-scoped group, its linked templates, and its linked policy group. |
| `POST /v1/members/message/group/list` | `project_id`; no other fields | Exact filters: `id`, `name`, `label`, `policy_group`; supports [shared pagination](https://m7.org/docs/api/api.post.m7.org/README.md#pagination). |
| `POST /v1/members/message/group/save` | `project_id`; `name`, `label` | `id` updates a project-scoped group; `description`, `policy_group`, and `metadata` are optional. |
| `POST /v1/members/message/group/delete` | `project_id`; `id` or `name` | Deletes the group and its template links, but does not delete templates. |

### Save a message group

| Field | Required | Type | Description |
| --- | --- | --- | --- |
| `project_id` | Yes | UUID | Authorized project scope for every request. |
| `id` | No | UUID | Existing project-scoped group to update. |
| `name` | Yes | String | Stable project-scoped group key. |
| `label` | Yes | String | Human-readable group label. |
| `description` | No | String or `null` | Optional group description. |
| `policy_group` | No | UUID | Project-scoped policy group to associate with the group. Omit or send an empty value for no association. |
| `metadata` | No | Object or `null` | Application-defined group data. |

Save supplies the complete editable group record: omitted optional fields are stored as empty or `null` values. `policy_group`, when present, must belong to the selected project.

```bash
curl -sS https://api.post.service.m7.org/v1/members/message/group/save \
  -H 'Authorization: Bearer ACCESS_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
    "project_id": "PROJECT_UUID",
    "name": "email-proof",
    "label": "Email proof messages",
    "description": "Templates used for recipient verification"
  }'
```

```json
{
  "status": 1,
  "comment": "OK",
  "data": {
    "saved": true,
    "item": {
      "id": "MESSAGE_GROUP_UUID",
      "name": "email-proof",
      "label": "Email proof messages",
      "template_ids": [],
      "templates": [],
      "policy_group_item": null
    }
  }
}
```

Store the group ID for template links and jobs. `get` and `list` return these same relationship fields.

## Message templates

| Route | Required fields | Optional fields and behavior |
| --- | --- | --- |
| `POST /v1/members/message/template/get` | `project_id`; `id`, or `channel` and `name` | Returns one project-scoped template and its linked groups. |
| `POST /v1/members/message/template/list` | `project_id`; no other fields | Exact filters: `id`, `channel`, `name`, `label`, `status`, `is_default`; supports [shared pagination](https://m7.org/docs/api/api.post.m7.org/README.md#pagination). |
| `POST /v1/members/message/template/save` | `project_id`; `channel`, `name`, `label`, `body_text` | `id` updates a project-scoped template; see the field table. |
| `POST /v1/members/message/template/delete` | `project_id`; `id`, or `channel` and `name` | Deletes the template and its template-group links. |

### Save a template

| Field | Required | Type | Description |
| --- | --- | --- | --- |
| `project_id` | Yes | UUID | Authorized project scope for every request. |
| `id` | No | UUID | Existing project-scoped template to update. |
| `channel` | Yes | String | `email` or `sms`. |
| `name` | Yes | String | Stable template key, unique for the project and channel. |
| `label` | Yes | String | Human-readable label. |
| `body_text` | Yes | String | Plain-text message content. |
| `status` | No | String | `active` (default) or `disabled`. |
| `is_default` | No | Boolean | Defaults to `false`. |
| `subject` | No | String or `null` | Optional email subject. |
| `body_html` | No | String or `null` | Optional HTML message body. |
| `metadata` | No | Object or `null` | Application-defined template data. |

Save supplies the complete editable template record: omitted optional fields use their defaults or become `null` on update.

```bash
curl -sS https://api.post.service.m7.org/v1/members/message/template/save \
  -H 'Authorization: Bearer ACCESS_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
    "project_id": "PROJECT_UUID",
    "channel": "email",
    "name": "proof-code",
    "label": "Email proof code",
    "subject": "Your confirmation code",
    "body_text": "Use the code sent to you to continue."
  }'
```

The successful response contains `data.saved: true` and `data.item`. The item includes `id`, the saved template fields, `group_ids`, and `groups`.

## Link a template to a group

The template-link routes manage the relationship between a project-scoped message group and a project-scoped template.

| Route | Required fields | Optional fields and behavior |
| --- | --- | --- |
| `POST /v1/members/message/group/template/get` | `project_id`; `id`, or `message_group` and `message_template` | Returns one project-scoped relationship. |
| `POST /v1/members/message/group/template/list` | `project_id`; `message_group` | Optional exact filters: `id`, `message_template`; supports [shared pagination](https://m7.org/docs/api/api.post.m7.org/README.md#pagination). |
| `POST /v1/members/message/group/template/save` | `project_id`; `message_group`, `message_template` | Creates the relationship, or returns the existing relationship unchanged. |
| `POST /v1/members/message/group/template/delete` | `project_id`; `id`, or `message_group` and `message_template` | Deletes the relationship only. |

Both `message_group` and `message_template` are UUIDs. The group must belong to the selected project, and the template must be in the same project or be a service-provided shared template.

```bash
curl -sS https://api.post.service.m7.org/v1/members/message/group/template/save \
  -H 'Authorization: Bearer ACCESS_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
    "project_id": "PROJECT_UUID",
    "message_group": "MESSAGE_GROUP_UUID",
    "message_template": "MESSAGE_TEMPLATE_UUID"
  }'
```

```json
{
  "status": 1,
  "comment": "OK",
  "data": {
    "saved": true,
    "item": {
      "id": "TEMPLATE_LINK_UUID",
      "message_group": "MESSAGE_GROUP_UUID",
      "message_template": "MESSAGE_TEMPLATE_UUID"
    }
  }
}
```

The returned item also includes `message_group_item` and `message_template_item`. Link an `email` template before activating any job that delivers email or requires email proof.

## Errors and project scope

- `get`, `delete`, and update operations return a not-found result when the requested resource is absent or does not belong to the selected project.
- A message group cannot reference a policy group in another project.
- Deleting a message group or template removes only its relationship links; the linked template or group remains available.
