Chilkat Online Tools

SQL Server / Infobip WhatsApp / Create WhatsApp Template

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    -- Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    -- The following JSON is sent in the request body.

    -- {
    --   "name": "example_template_name_{{$randomInt}}_{{$randomInt}}",
    --   "language": "en",
    --   "category": "ACCOUNT_UPDATE",
    --   "structure": {
    --     "body": "body {{1}} content",
    --     "type": "TEXT"
    --   }
    -- }

    DECLARE @json int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'name', 'example_template_name_{{$randomInt}}_{{$randomInt}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'language', 'en'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'category', 'ACCOUNT_UPDATE'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'structure.body', 'body {{1}} content'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'structure.type', 'TEXT'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Authorization', 'App {{INFOBIP_API_KEY}}'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/whatsapp/1/senders/:sender/templates', 'application/json', @json
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    DECLARE @sbResponseBody int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @resp

    -- Sample JSON response:
    -- (Sample code for parsing the JSON response is shown below)

    -- {
    --   "id": "1072288653542716",
    --   "businessAccountId": 2125987241021795,
    --   "name": "example_template_name_767_413",
    --   "language": "en",
    --   "status": "PENDING",
    --   "category": "ACCOUNT_UPDATE",
    --   "structure": {
    --     "body": "body {{1}} content",
    --     "type": "TEXT"
    --   }
    -- }

    -- Sample code for parsing the JSON response...
    -- Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    DECLARE @id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'id'
    DECLARE @businessAccountId int
    EXEC sp_OAMethod @jResp, 'IntOf', @businessAccountId OUT, 'businessAccountId'
    DECLARE @name nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @name OUT, 'name'
    DECLARE @language nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @language OUT, 'language'
    DECLARE @status nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @status OUT, 'status'
    DECLARE @category nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @category OUT, 'category'
    DECLARE @Body nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Body OUT, 'structure.body'
    DECLARE @v_Type nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @v_Type OUT, 'structure.type'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

curl -X POST
	-H "Authorization: App {{INFOBIP_API_KEY}}"
	-H "Content-Type: application/json"
	-d '{
    "name": "example_template_name_{{$randomInt}}_{{$randomInt}}",
    "language": "en",
    "category": "ACCOUNT_UPDATE",
    "structure": {
        "body": "body {{1}} content",
        "type": "TEXT"
    }
}'
https://domain.com/whatsapp/1/senders/:sender/templates

Postman Collection Item JSON

{
  "name": "Create WhatsApp Template",
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "exec": [
          ""
        ],
        "type": "text/javascript"
      }
    },
    {
      "listen": "test",
      "script": {
        "exec": [
          "pm.test(\"Status code is 201, template created\", function () {",
          "    pm.response.to.have.status(201);",
          "});"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"name\": \"example_template_name_{{$randomInt}}_{{$randomInt}}\",\n    \"language\": \"en\",\n    \"category\": \"ACCOUNT_UPDATE\",\n    \"structure\": {\n        \"body\": \"body {{1}} content\",\n        \"type\": \"TEXT\"\n    }\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{baseUrl}}/whatsapp/1/senders/:sender/templates",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "whatsapp",
        "1",
        "senders",
        ":sender",
        "templates"
      ],
      "variable": [
        {
          "key": "sender",
          "value": "{{senderNumber}}",
          "description": "(Required) Registered WhatsApp sender number. Must be in international format."
        }
      ]
    },
    "description": "Create WhatsApp template. Created template will be submitted for WhatsApp's review and approval. Once approved, template can be sent to end-users. Refer to [template guidelines](https://www.infobip.com/docs/whatsapp/message-types#guidelines-amp-best-practices) for additional info."
  },
  "response": [
    {
      "name": "Text template simple",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"name\": \"example_template_name_{{$randomInt}}_{{$randomInt}}\",\n    \"language\": \"en\",\n    \"category\": \"ACCOUNT_UPDATE\",\n    \"structure\": {\n        \"body\": \"body {{1}} content\",\n        \"type\": \"TEXT\"\n    }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/whatsapp/1/senders/:sender/templates",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "whatsapp",
            "1",
            "senders",
            ":sender",
            "templates"
          ],
          "variable": [
            {
              "key": "sender",
              "value": "{{senderNumber}}",
              "description": "(Required) Registered WhatsApp sender number. Must be in international format."
            }
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Vary",
          "value": "Origin"
        },
        {
          "key": "Vary",
          "value": "Access-Control-Request-Method"
        },
        {
          "key": "Vary",
          "value": "Access-Control-Request-Headers"
        },
        {
          "key": "Date",
          "value": "Mon, 08 Nov 2021 10:36:58 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Server",
          "value": "SMS API"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        },
        {
          "key": "X-Request-Id",
          "value": "1636367818279895837"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"id\": \"1072288653542716\",\n    \"businessAccountId\": 2125987241021795,\n    \"name\": \"example_template_name_767_413\",\n    \"language\": \"en\",\n    \"status\": \"PENDING\",\n    \"category\": \"ACCOUNT_UPDATE\",\n    \"structure\": {\n        \"body\": \"body {{1}} content\",\n        \"type\": \"TEXT\"\n    }\n}"
    },
    {
      "name": "Complex template with buttons and image",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"name\": \"example_template_name_{{$randomInt}}_{{$randomInt}}\",\n    \"language\": \"en\",\n    \"category\": \"ACCOUNT_UPDATE\",\n    \"structure\": {\n        \"header\": {\n            \"format\": \"IMAGE\"\n        },\n        \"body\": \"body {{1}} content\",\n        \"footer\": \"this is footer\",\n        \"buttons\": [\n            {\n                \"text\": \"Yes\",\n                \"type\": \"QUICK_REPLY\"\n            },\n            {\n                \"text\": \"No\",\n                \"type\": \"QUICK_REPLY\"\n            }\n        ],\n        \"type\": \"MEDIA\"\n    }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/whatsapp/1/senders/:sender/templates",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "whatsapp",
            "1",
            "senders",
            ":sender",
            "templates"
          ],
          "variable": [
            {
              "key": "sender",
              "value": "{{senderNumber}}",
              "description": "(Required) Registered WhatsApp sender number. Must be in international format."
            }
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Vary",
          "value": "Origin"
        },
        {
          "key": "Vary",
          "value": "Access-Control-Request-Method"
        },
        {
          "key": "Vary",
          "value": "Access-Control-Request-Headers"
        },
        {
          "key": "Date",
          "value": "Mon, 08 Nov 2021 10:50:20 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Server",
          "value": "SMS API"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        },
        {
          "key": "X-Request-Id",
          "value": "1636368620308994838"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"id\": \"452107629693501\",\n    \"businessAccountId\": 2125987241021795,\n    \"name\": \"example_template_name_225_786\",\n    \"language\": \"en\",\n    \"status\": \"PENDING\",\n    \"category\": \"ACCOUNT_UPDATE\",\n    \"structure\": {\n        \"header\": {\n            \"format\": \"IMAGE\"\n        },\n        \"body\": \"body {{1}} content\",\n        \"footer\": \"this is footer\",\n        \"buttons\": [\n            {\n                \"text\": \"Yes\",\n                \"type\": \"QUICK_REPLY\"\n            },\n            {\n                \"text\": \"No\",\n                \"type\": \"QUICK_REPLY\"\n            }\n        ],\n        \"type\": \"MEDIA\"\n    }\n}"
    }
  ]
}