# Sending a template via API

Sending a template is described in [the API documentation](https://my.1msg.io/documentation/1msg) in the /sendTemplate method.

You can also create a template from the 'Dev Toolkit →[ 'Test Requests'](https://my.1msg.io/testing/test-requests) section in your personal account.

To send a template, you need to know the **namespace, name and language** of the template. To get this information, call the 'Get list of templates' method (/templates).

Some properties that can cause problems in the /sendTemplate method.

<table><thead><tr><th width="197">Свойства</th><th>Описание</th></tr></thead><tbody><tr><td>template</td><td>It's the name of the template</td></tr><tr><td>language</td><td><p>policy: write "deterministiс" </p><p>code: language from the /templates method</p></td></tr><tr><td>Object with type "body"</td><td>Text message. Can be omitted if there are no variables in the text.</td></tr><tr><td>Object with type "button"</td><td>You can display only buttons with sub_type ‘url’ or ‘catalog’ in the body. Other types of buttons can be omitted when sending the template</td></tr></tbody></table>

## How to send Marketing or Utility templates

In requests, you should only send the objects that you have specified in the created template. For example, if there is no HEADER in the template, do not send it.  Objects are also omitted if:

* Header object of type Text without variable;&#x20;
* Body without variable;&#x20;
* Quick reply buttons and number buttons are always omitted;&#x20;
* URL button without variable.

Header objects of type `IMAGE`, `VIDEO`, `DOCUMENT` always contain the full link to the media document. Note that the link to the media document must contain the format of the document.

<details>

<summary>Example of sending a Document with URL button</summary>

Structure:

* Header of document type
* Body contains 2 variables
* Two URL buttons, the second of which contains a variable.

If a URL button does not have a variable, omit it from the request. For example, in this request, the first URL button with index 0 is omitted, but the second button is output. Note also that the index of the second button is already 1.

```json
{
  "token": "{{token}}",
  "namespace": "{{namespace}}",
  "template": "delivery_tracking",
  "language": {
    "policy": "deterministic",
    "code": "ru"
  },
  "params": [
    {
      "type": "header",
      "parameters": [
        {
          "type": "document", // document object
          "document": {
            "link": "https://test.com/reyshi.pdf" //link to the document. Please note that the link must specify the format of the document
          }
        }
      ]
    },
    {
      "type": "body",
      "parameters": [
        {
          "type": "text",
          "text": "Андрей" // first variable in BODY
        },
        {
          "type": "text",
          "text": "№003478" // second variable in BODY
        }
      ]
    },
    {
      "type": "button", // second URL button
      "sub_type": "url",
      "index":1, // index is numbered starting from zero, the second button has index 1
      "parameters": [
        {
          "type": "text",
          "text": "/reyshi" // link variable
        }
      ]
    }
  ],
  "phone": "{{phone}}"
}
```

</details>

<details>

<summary>Example of sending a Text Header with all types of buttons</summary>

Structure:&#x20;

* Header of Text type with one variable
* The body of the message does not contain a variable. Therefore, it is omitted in the query.
* 4 buttons. The first button is quick reply, the second is phone button, the third is URL button with variable, the fourth is URL button without variable.

```json
{
  "token": "{{token}}",
  "namespace": "{{namespace}}",
  "template": "utility_all_buttons_2024_05",
  "language": {
    "policy": "deterministic",
    "code": "en"
  },
  "params": [
    {
      "type": "header",
      "parameters": [
        {
          "type": "text",
          "text": "Anna" // header variable
        }
      ]
    },
    {
      "type": "button", // third button, a URL button with a variable
      "sub_type": "url",
      "index": 2, // third button index
      "parameters": [
        {
          "type": "text",
          "text": "inbox" // link variable
        }
      ]
    }
  ],
  "phone": "{{phone}}"
}
```

&#x20;

</details>

## How to send a Carousel template

{% hint style="info" %}
The Carousel template is only supported in the Cloud API.
{% endhint %}

Carousel template:

* Has its own type - CAROUSEL
* An object with type CAROUSEL contains an array of cards. It is used only for this template type
* Each card contains card\_index - the order of appearance of cards in the card carousel. 0 - means the first card, 1 - means the second card, etc.

<details>

<summary>Example of how to send the ‘Carousel’ template</summary>

An example of a 2-card template. Each card contains a quick reply button and a url button. The media header is an image.

```json
POST https://api.1msg.io/{{channel}}/sendTemplate
Content-Type: application/json
Body: {
  "token": "{{token}}",
  "namespace": "{{namespace}}", // Call the list of templates via the /templates method and find the namespace for the required template
  "template": "marceting_carousel_01_test", // name template
  "language": {
    "policy": "deterministic",
    "code": "en" // language from the /templates method
  },
  "params": [
    {
   /* /* Common text message. Can be omitted if there are no variables in the text. */ */
      "type": "body", 
      "parameters": [
        {
          "type": "text",
          "text": "21OFF" // variable {{1}}
        },
        {
          "type": "text",
          "text": "21" // variable {{2}}
        }
      ]
    },
    {
      "type": "carousel",
      "cards": [
        /* The first card */
        {
          "card_index": 0,
          "components": [
            {
              "type": "header",
              "parameters": [
                {
                  "type": "image",
                  "image": {
                    "link": "https://img.freepik.com/free-photo/top-view-citric-fruit-slices-table_23-2148495686.jpg"
                  }
                }
              ]
            },
            {
              "type": "button",
              "sub_type": "url",
              "index": 1,  // button position, starting from 0. Here url-button is the second in the sequence, so index=1
              "parameters": [
                {
                  "type": "text",
                  "text": "summer" // parameter in url link
                }
              ]
            },
            {
             /* Card text. Can be omitted if there are no variables in the text. */
              "parameters": [
                {
                  "type": "text",
                  "text": "10F" // variable {{1}} in the first card
                },
                {
                  "type": "text",
                  "text": "10" // variable {{2}} in the first card
                }
              ]
            }
          ]
        },
        /* Вторая карточка */
        {
          "card_index": 1, 
          "components": [
            {
              "type": "header",
              "parameters": [
                {
                  "type": "image",
                  "image": {
                    "link": "https://img.freepik.com/free-photo/delicious-background-orange-slices_23-2148231462.jpg"
                  }
                }
              ]
            },
            {
              "type": "button",
              "sub_type": "url",
              "index": 1, // button position, starting from 0. Here url-button is the second in the sequence, so index=1
              "parameters": [
                {
                  "type": "text",
                  "text": "winter" // parameter in url link
                }
              ]
            },
            {
               /* Card text. Can be omitted if there are no variables in the text. */
              "type": "body",
              "parameters": [
                {
                  "type": "text",
                  "text": "30F" // variable {{1}} in the second card
                },
                {
                  "type": "text",
                  "text": "30" // variable {{2}} in the second card
                }
              ]
            }
          ]
        }
      ]
    }
  ],
  "phone": "+556123122026"
}
```

</details>

## How to send the ‘Catalog’ template

{% hint style="info" %}
The ‘Catalog’  template is only supported in the Cloud API.
{% endhint %}

Before you send a catalogue, check if you have it enabled. You can check in your personal account in 360 Dialog, or by calling the /commerce method.

Catalogue template:

* In the button object, you need to write CATALOG in sub\_type . The name of the button is not controllable.
* In thumbnail\_product\_retailer\_id you can write the article of the product specified in Meta. In this case, the thumbnail of this product will be used as the header image of the post. Otherwise, the product image of the first product in your catalogue will be used.

Example code part when no thumbnail\_product\_retailer\_id is specified.

```json
 {
      "type": "button",
      "sub_type": "CATALOG",
      "index": 0,
      "parameters": [
        {
          "type": "action",
          "action": {
            "thumbnail_product_retailer_id": ""
          }
        }
      ]
    }
```

<details>

<summary>Example of sending the ‘Catalog’ template</summary>

Because the template did not contain a variable description in the text, object with type "BODY" is omitted when it is sent.

```json
POST https://api.1msg.io/{{channel}}/sendTemplate
Content-Type: application/json
Body: {
  "token": "{{token}}",
  "namespace": "{{namespace}}",
  "template": "catalog_services",
  "language": {
    "policy": "deterministic",
    "code": "en"
  },
  "params": [
    {
      "type": "button",
      "sub_type": "CATALOG",
      "index": 0,
      "parameters": [
        {
          "type": "action",
          "action": {
            "thumbnail_product_retailer_id": "0002" // A thumbnail of this product will be sent. You can send 'thumbnail_product_retailer_id': ''. The product image of the first product in your catalogue will be used.
        }
      ]
    }
  ],
  "phone": "+556123122026"
}
      
```

</details>

## How to send the "Authentication" template

<details>

<summary>Example of sending an authentication template</summary>

Example of sending a code copy button

```json
{
  "token": "{{token}}",
  "namespace": "{{namespace}}",
  "template": "auth_2",
  "language": {
    "policy": "deterministic",
    "code": "ru"
  },
  "params": [
        {
      "type": "body",
      "parameters": [
        {
          "type": "text",
          "text": "05425" // code displayed in the message body
        }
      ]
    },
    {
      "type": "BUTTON",
      "sub_type": "URL",
      "index": 0,
      "parameters": [
        {
          "type": "TEXT",
          "text": "05425" // the code that will be copied
        }
      ]
    }
  ],
  "phone": "+556123122026"
}
```

</details>

```json
  
```
