Chilkat Online Tools

SQL Server / Braze Endpoints / Sending Canvas Messages via API Triggered Delivery

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.

    -- {
    --   "canvas_id": "canvas_identifier",
    --   "canvas_entry_properties": {
    --     "product_name": "shoes",
    --     "product_price": 79.99
    --   },
    --   "broadcast": false,
    --   "audience": {
    --     "AND": [
    --       {
    --         "custom_attribute": {
    --           "custom_attribute_name": "eye_color",
    --           "comparison": "equals",
    --           "value": "blue"
    --         }
    --       },
    --       {
    --         "custom_attribute": {
    --           "custom_attribute_name": "favorite_foods",
    --           "comparison": "includes_value",
    --           "value": "pizza"
    --         }
    --       },
    --       {
    --         "OR": [
    --           {
    --             "custom_attribute": {
    --               "custom_attribute_name": "last_purchase_time",
    --               "comparison": "less_than_x_days_ago",
    --               "value": 2
    --             }
    --           },
    --           {
    --             "push_subscription_status": {
    --               "comparison": "is",
    --               "value": "opted_in"
    --             }
    --           }
    --         ]
    --       },
    --       {
    --         "email_subscription_status": {
    --           "comparison": "is_not",
    --           "value": "subscribed"
    --         }
    --       },
    --       {
    --         "last_used_app": {
    --           "comparison": "after",
    --           "value": "2019-07-22T13:17:55+0000"
    --         }
    --       }
    --     ]
    --   },
    --   "recipients": {
    --     "user_alias": {
    --       "alias_name": "example_name",
    --       "alias_label": "example_label"
    --     },
    --     "external_user_id": "user_identifier",
    --     "trigger_properties": "",
    --     "canvas_entry_properties": "",
    --     "send_to_existing_only": true,
    --     "attributes": {
    --       "first_name": "Alex"
    --     }
    --   }
    -- }

    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, 'canvas_id', 'canvas_identifier'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'canvas_entry_properties.product_name', 'shoes'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'canvas_entry_properties.product_price', '79.99'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'broadcast', 0
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'audience.AND[0].custom_attribute.custom_attribute_name', 'eye_color'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'audience.AND[0].custom_attribute.comparison', 'equals'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'audience.AND[0].custom_attribute.value', 'blue'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'audience.AND[1].custom_attribute.custom_attribute_name', 'favorite_foods'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'audience.AND[1].custom_attribute.comparison', 'includes_value'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'audience.AND[1].custom_attribute.value', 'pizza'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'audience.AND[2].OR[0].custom_attribute.custom_attribute_name', 'last_purchase_time'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'audience.AND[2].OR[0].custom_attribute.comparison', 'less_than_x_days_ago'
    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'audience.AND[2].OR[0].custom_attribute.value', 2
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'audience.AND[2].OR[1].push_subscription_status.comparison', 'is'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'audience.AND[2].OR[1].push_subscription_status.value', 'opted_in'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'audience.AND[3].email_subscription_status.comparison', 'is_not'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'audience.AND[3].email_subscription_status.value', 'subscribed'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'audience.AND[4].last_used_app.comparison', 'after'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'audience.AND[4].last_used_app.value', '2019-07-22T13:17:55+0000'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'recipients.user_alias.alias_name', 'example_name'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'recipients.user_alias.alias_label', 'example_label'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'recipients.external_user_id', 'user_identifier'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'recipients.trigger_properties', ''
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'recipients.canvas_entry_properties', ''
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'recipients.send_to_existing_only', 1
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'recipients.attributes.first_name', 'Alex'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
    -- Adds the "Authorization: Bearer {{api_key}}" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '{{api_key}}'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://rest.iad-01.braze.com/canvas/trigger/send', '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

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @resp


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json


END
GO

Curl Command

curl -X POST
	-H "Content-Type: application/json"
	-H "Authorization: Bearer {{api_key}}"
	-d '{
  "canvas_id": "canvas_identifier",
  "canvas_entry_properties": {"product_name" : "shoes", "product_price" : 79.99},
  "broadcast": false,
	"audience": {
    "AND": [
      {
        "custom_attribute": {
          "custom_attribute_name": "eye_color",
          "comparison": "equals",
          "value": "blue"
        }
      },
      {
        "custom_attribute": {
          "custom_attribute_name": "favorite_foods",
          "comparison": "includes_value",
          "value": "pizza"
        }
      },
      {
        "OR": [
          {
            "custom_attribute": {
              "custom_attribute_name": "last_purchase_time",
              "comparison": "less_than_x_days_ago",
              "value": 2
            }
          },
          {
            "push_subscription_status": {
              "comparison": "is",
              "value": "opted_in"
            }
          }
        ]
      },
      {
        "email_subscription_status": {
          "comparison": "is_not",
          "value": "subscribed"
        }
      },
      {
        "last_used_app": {
          "comparison": "after",
          "value": "2019-07-22T13:17:55+0000"
        }
      }
    ]
  },
  "recipients": {
  	"user_alias": {
  		"alias_name" : "example_name",
  		"alias_label" : "example_label"
  	},
  	"external_user_id": "user_identifier",
  	"trigger_properties": "",
  	"canvas_entry_properties": "",
  	"send_to_existing_only": true,
    "attributes": {
        "first_name" : "Alex"
    }
    }
}'
https://rest.iad-01.braze.com/canvas/trigger/send

Postman Collection Item JSON

{
  "name": "Sending Canvas Messages via API Triggered Delivery",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Authorization",
        "type": "text",
        "value": "Bearer {{api_key}}"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"canvas_id\": \"canvas_identifier\",\n  \"canvas_entry_properties\": {\"product_name\" : \"shoes\", \"product_price\" : 79.99},\n  \"broadcast\": false,\n\t\"audience\": {\n    \"AND\": [\n      {\n        \"custom_attribute\": {\n          \"custom_attribute_name\": \"eye_color\",\n          \"comparison\": \"equals\",\n          \"value\": \"blue\"\n        }\n      },\n      {\n        \"custom_attribute\": {\n          \"custom_attribute_name\": \"favorite_foods\",\n          \"comparison\": \"includes_value\",\n          \"value\": \"pizza\"\n        }\n      },\n      {\n        \"OR\": [\n          {\n            \"custom_attribute\": {\n              \"custom_attribute_name\": \"last_purchase_time\",\n              \"comparison\": \"less_than_x_days_ago\",\n              \"value\": 2\n            }\n          },\n          {\n            \"push_subscription_status\": {\n              \"comparison\": \"is\",\n              \"value\": \"opted_in\"\n            }\n          }\n        ]\n      },\n      {\n        \"email_subscription_status\": {\n          \"comparison\": \"is_not\",\n          \"value\": \"subscribed\"\n        }\n      },\n      {\n        \"last_used_app\": {\n          \"comparison\": \"after\",\n          \"value\": \"2019-07-22T13:17:55+0000\"\n        }\n      }\n    ]\n  },\n  \"recipients\": {\n  \t\"user_alias\": {\n  \t\t\"alias_name\" : \"example_name\",\n  \t\t\"alias_label\" : \"example_label\"\n  \t},\n  \t\"external_user_id\": \"user_identifier\",\n  \t\"trigger_properties\": \"\",\n  \t\"canvas_entry_properties\": \"\",\n  \t\"send_to_existing_only\": true,\n    \"attributes\": {\n        \"first_name\" : \"Alex\"\n    }\n    }\n}"
    },
    "url": {
      "raw": "https://{{instance_url}}/canvas/trigger/send",
      "protocol": "https",
      "host": [
        "{{instance_url}}"
      ],
      "path": [
        "canvas",
        "trigger",
        "send"
      ]
    },
    "description": "API Triggered Delivery allows you to house message content inside of the Braze dashboard while dictating when a message is sent, and to whom via your API.\n\nThis endpoint allows you to send Canvas messages via API Triggered delivery, allowing you to decide what action should trigger the message to be sent. Please note that to send messages with this endpoint, you must have a Canvas ID, created when you build a [Canvas](https://www.braze.com/api/identifier_types/#canvas-api-identifier).\n\n### Request Parameters\n| Parameter | Required | Data Type | Description |\n| --------- | ---------| --------- | ----------- |\n|`canvas_id`|Required|String|See canvas identifier|\n|`canvas_entry_properties`|Optional|Object|Personalization key value pairs that will apply to all users in this request|\n|`broadcast`|Optional|Boolean|See broadcast -- defaults to false on 8/31/17, must be set to true if \"recipients\" is omitted|\n|`audience`|Optional|Connected audience object|See connected audience|\n|`recipients`|Optional|Array|If not provided and broadcast is not set to 'false', message will send to the entire segment targeted by the Canvas|\n\n### Request Components\n- [Canvas Identifier](https://www.braze.com/docs/api/identifier_types/)\n- [Broadcast](https://www.braze.com/docs/api/parameters/#broadcast)\n- [Connected Audience](https://www.braze.com/docs/api/objects_filters/connected_audience/)\n- [Recipients](https://www.braze.com/docs/api/objects_filters/recipient_object/)\n- [Canvas Entry Properties](https://www.braze.com/docs/api/objects_filters/canvas_entry_properties_object/)\n- [User Alias Object](https://www.braze.com/docs/api/objects_filters/user_alias_object/)\n- [User Attributes Object](https://www.braze.com/docs/api/objects_filters/user_attributes_object/)\n- [API Parameters](https://www.braze.com/docs/api/parameters)\n\nThe `recipients` array may contain up to 50 objects, with each object containing a single `external_user_id` string and `canvas_entry_properties` object.\n\nCustomers using the API for server-to-server calls may need to whitelist the appropriate API URL if they're behind a firewall.\n\nIf you include both specific users in your API call and a target segment in the dashboard, the message will send to specifically the user profiles that are in the API call *and* qualify for the segment filters.\n\n### Response Details\nMessage sending endpoint responses will include the message’s `dispatch_id` for reference back to the dispatch of the message. The `dispatch_id` is the id of the message dispatch (unique id for each ‘transmission’ sent from the Braze platform). For more information on `dispatch_id` checkout out our [documentation](https://www.braze.com/docs/help/help_articles/data/dispatch_id/).\n\n## Create Send Endpoint\n\n__Using the Attributes Object in Canvas__\n\nBraze has a Messaging Object called `Attributes` that allows you to add, create, or update attributes and values for a user before sending them an API Triggered Canvas using the `canvas/trigger/send` endpoint as this API call will process the User Attributes object before it processes and sends the Canvas. This helps minimize the risk of there being issues caused by race conditions."
  },
  "response": [
  ]
}