# WhatsApp templates

WhatsApp templates can be created for any account that belongs to one or more senders. The template endpoints are the same for both partner-initiated and ES senders. Any template in a project can be used by all senders in the same template.

## Create a template

When creating a template, it needs to have a unique name, a language, and a category. For new template categories, one of the following categories should be used: `MARKETING`, `OTP` and `TRANSACTIONAL`. When listing older templates it is possible that other values are returned and new languages for older templates should use the category of the existing template.

| Category | Description |
|  --- | --- |
| `MARKETING` | Send promotional offers, product announcements, and more to increase awareness and engagement. |
| `OTP` | Send one-time password codes that allow your customers to securely access their accounts. |
| `TRANSACTIONAL` | Send account updates, order updates, alerts, and more to share important information. |


Note:
If setting the status to `draft` the template will not be processed. A draft template will be saved and editable until an edit sets the status to `submit`.

```javascript
async function createTemplate() {
  const resp = await fetch(
    `https://provisioning.api.sinch.com/v1alpha1/projects/${PROJECT_ID}/whatsapp/templates`,
    {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Authorization:
          'Basic ' +
          Buffer.from(ACCESS_KEY + ':' + ACCESS_SECRET).toString('base64'),
      },
      body: JSON.stringify({
        name: 'example_template',
        status: 'draft',
        category: 'TRANSACTIONAL',
        language: 'en',
        components: [
          {
            type: 'BODY',
            text: 'Good morning {{1}}, your package has arrived at {{2}}.',
            examples: ['Steve'. 'your local post office'],
          },
        ],
      }),
    }
  );

  const data = await resp.json();
  return data;
}
```

## Listing templates

Use the list templates in a project to see which templates are in progress or can be used for sending templates with through Conversation API.

The list is paginated and will return a `nextPageToken` if there are more templates. If only approved templates are needed, for example to show which a user can use to send messages with, a filter can be added.

```javascript
async function getAllApprovedTemplates(pageToken: string) {
  const resp = await fetch(
    `https://provisioning.api.sinch.com/v1alpha1/projects/${PROJECT_ID}/whatsapp/templates?pageToken=${pageToken}&templateStatus=APPROVED`,
    {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
        Authorization:
          'Basic ' +
          Buffer.from(ACCESS_KEY + ':' + ACCESS_SECRET).toString('base64'),
      },
    }
  );

  let data = await resp.json();

  if (data.nextPageToken) {
    const nextData = await getAllApprovedTemplates(data.nextPageToken);

    return {
      ...data.templates,
      ...nextData.templates,
    };
  } else {
    return data.templates;
  }
}
```