# Authenticate users with a WhatsApp template message

This tutorial will show you how to send a simple authorization message using Infobip WhatsApp API templates.

## What is a WhatsApp template message? [#what-is-whatsapp-template]

As opposed to regular messages that need to be user-initiated and can only happen within a 24-hour window, template messages can be sent and delivered at any time. Check out our [product documentation](https://www.infobip.com/docs/whatsapp/message-types-and-templates#message-templates) for more details.

## Prerequisites [#prerequisites]

* [Infobip account](https://portal.infobip.com/login/?callback=https%3A%2F%2Fportal.infobip.com%2F). If you do not have one, create a [free trial account](https://www.infobip.com/signup).
* Infobip API key with the `whatsapp:message:send` scope. Find out more about scopes in [API Authorization](https://www.infobip.com/docs/essentials/api-essentials/api-authorization).
* Your favorite HTTP client. In this example, we use `curl`. You can also choose an official [Infobip SDK](https://www.infobip.com/docs/sdk) in your preferred programming language.
* Sender. If in a free trial period, you can use Infobip test sender, **447860099299**.
   If you want to configure a custom sender, request a new number or an alphanumeric sender through your [Infobip account](https://portal.infobip.com/channels-and-numbers/channels/sms/overview) or use the [Infobip Numbers API](https://www.infobip.com/docs/api/platform/numbers).
* Destination - a phone number to which the message will be sent. In a free trial period, you can only send messages to a [verified phone number](https://www.infobip.com/docs/essentials/getting-started/free-trial#free-trial-limitations).

## Summary of the steps [#summary-of-steps]

* Create and register WhatsApp template.
* Send a WhatsApp template message.

## Create a WhatsApp template [#create-whatsapp-template]

Use the [Create a WhatsApp template](https://www.infobip.com/docs/api/channels/whatsapp/whatsapp-service-management/create-whatsapp-template) endpoint to create your template.

```bash
curl -L -g 'https://{baseUrl}/whatsapp/2/senders/{sender}/templates' \
-H 'Authorization: {authorization}' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-d '{
  "name": "authentication_template_with_copy_code_button",
  "language": "en",
  "category": "AUTHENTICATION",
  "allowCategoryChange": false,
  "structure": {
    "body": {
      "addSecurityRecommendation": true
    },
    "footer": {
      "codeExpirationMinutes": 5
    },
    "buttons": [
      {
        "text": "Copy code",
        "otpType": "COPY_CODE"
      }
    ]
  }
}'
```

__Key points:__

* Once you create a template, Infobip sends it for the WhatsApp validation process. You can edit the template, following the  editing rules.
* For `category`, use the `AUTHENTICATION` value. This will adapt further field options in the API docs.
* For `buttons`, use `COPY_CODE`. This will adapt further field options in the API docs.

### Register a WhatsApp template [#register-whatsapp-template]

Infobip takes care of your template registration. During the process, you can edit your template following the editing rules. You can also set up a webhook to get the updates about your template registration process.

## Send a WhatsApp template message [#send-whatsapp-template]

Once registered, use the template to contact users for authentication.

__Key points:__

* Use the international number formatting when adding a number to the `to` field. For example, for British numbers, the value will look like this: `44754322576`.
* For `buttons`, use `URL`. This is to adhere to Meta's convention for button types.

```bash
curl -L -g 'https://{baseUrl}/whatsapp/1/message/template' \
-H 'Authorization: {authorization}' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-d '{
  "messages": [
    {
      "from": "441134960000",
      "to": "441134960001",
      "messageId": "a28dd97c-1ffb-4fcf-99f1-0b557ed381da",
      "content": {
        "templateName": "template_name",
        "templateData": {
          "body": {
            "placeholders": [
              "Authentication Code Value"
            ]
          },
          "buttons": [
            {
              "type": "URL",
              "parameter": "Authentication Code Value"
            }
          ]
        },
        "language": "en_GB"
      },
      "callbackData": "Callback data",
      "notifyUrl": "https://www.example.com/whatsapp",
      "urlOptions": {
        "shortenUrl": true,
        "trackClicks": true,
        "trackingUrl": "https://example.com/click-report",
        "removeProtocol": true,
        "customDomain": "example.com"
      }
    }
  ]
}'
```

## Track message delivery [#track-whatsapp-template]

Once you send the request, you should receive a WhatsApp message on the handset you've specified in  the `to` field and see a `200 OK` response.

```json
{
  "to": "441134960001",
  "messageCount": 1,
  "messageId": "a28dd97c-1ffb-4fcf-99f1-0b557ed381da",
  "status": {
    "groupId": 1,
    "groupName": "PENDING",
    "id": 7,
    "name": "PENDING_ENROUTE",
    "description": "Message sent to next instance"
  }
}
```

The response informs you that the message has successfully been queued within the Infobip platform and will be then sent to Meta depending on throughput.

If you want to know whether it's been delivered to the recipient, use the [Delivery Report](https://www.infobip.com/docs/api/channels/whatsapp/receive-whatsapp-delivery-reports) feature.

## Troubleshoot and analyze your message [#troubleshoot-whatsapp-template]

For troubleshooting and analytics, Use the auto-generated `messageId` to view the message and its details.