Chilkat Online Tools

PowerBuilder / Braze Endpoints / Sending Campaign Messages via API Triggered Delivery

Back to Collection Items

integer li_rc
oleobject loo_Http
integer li_Success
oleobject loo_Json
oleobject loo_Resp

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat_9_5_0.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

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

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

// {
//   "campaign_id": "campaign_identifier",
//   "send_id": "send_identifier",
//   "trigger_properties": "",
//   "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": "external_user_identifier",
//     "trigger_properties": "",
//     "send_to_existing_only": true,
//     "attributes": ""
//   }
// }

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat_9_5_0.JsonObject")

loo_Json.UpdateString("campaign_id","campaign_identifier")
loo_Json.UpdateString("send_id","send_identifier")
loo_Json.UpdateString("trigger_properties","")
loo_Json.UpdateBool("broadcast",0)
loo_Json.UpdateString("audience.AND[0].custom_attribute.custom_attribute_name","eye_color")
loo_Json.UpdateString("audience.AND[0].custom_attribute.comparison","equals")
loo_Json.UpdateString("audience.AND[0].custom_attribute.value","blue")
loo_Json.UpdateString("audience.AND[1].custom_attribute.custom_attribute_name","favorite_foods")
loo_Json.UpdateString("audience.AND[1].custom_attribute.comparison","includes_value")
loo_Json.UpdateString("audience.AND[1].custom_attribute.value","pizza")
loo_Json.UpdateString("audience.AND[2].OR[0].custom_attribute.custom_attribute_name","last_purchase_time")
loo_Json.UpdateString("audience.AND[2].OR[0].custom_attribute.comparison","less_than_x_days_ago")
loo_Json.UpdateInt("audience.AND[2].OR[0].custom_attribute.value",2)
loo_Json.UpdateString("audience.AND[2].OR[1].push_subscription_status.comparison","is")
loo_Json.UpdateString("audience.AND[2].OR[1].push_subscription_status.value","opted_in")
loo_Json.UpdateString("audience.AND[3].email_subscription_status.comparison","is_not")
loo_Json.UpdateString("audience.AND[3].email_subscription_status.value","subscribed")
loo_Json.UpdateString("audience.AND[4].last_used_app.comparison","after")
loo_Json.UpdateString("audience.AND[4].last_used_app.value","2019-07-22T13:17:55+0000")
loo_Json.UpdateString("recipients.user_alias.alias_name","example_name")
loo_Json.UpdateString("recipients.user_alias.alias_label","example_label")
loo_Json.UpdateString("recipients.external_user_id","external_user_identifier")
loo_Json.UpdateString("recipients.trigger_properties","")
loo_Json.UpdateBool("recipients.send_to_existing_only",1)
loo_Json.UpdateString("recipients.attributes","")

loo_Http.SetRequestHeader("Content-Type","application/json")
// Adds the "Authorization: Bearer {{api_key}}" header.
loo_Http.AuthToken = "{{api_key}}"

loo_Resp = loo_Http.PostJson3("https://rest.iad-01.braze.com/campaigns/trigger/send","application/json",loo_Json)
if loo_Http.LastMethodSuccess = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Json
    return
end if

Write-Debug string(loo_Resp.StatusCode)
Write-Debug loo_Resp.BodyStr
destroy loo_Resp


destroy loo_Http
destroy loo_Json

Curl Command

curl -X POST
	-H "Content-Type: application/json"
	-H "Authorization: Bearer {{api_key}}"
	-d '{
	"campaign_id": "campaign_identifier",
	"send_id": "send_identifier",
	"trigger_properties": "",
	"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": "external_user_identifier",
	"trigger_properties": "",
	"send_to_existing_only": true,
    "attributes": ""
    }
}'
https://rest.iad-01.braze.com/campaigns/trigger/send

Postman Collection Item JSON

{
  "name": "Sending Campaign 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\t\"campaign_id\": \"campaign_identifier\",\n\t\"send_id\": \"send_identifier\",\n\t\"trigger_properties\": \"\",\n\t\"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\t\"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\": \"external_user_identifier\",\n\t\"trigger_properties\": \"\",\n\t\"send_to_existing_only\": true,\n    \"attributes\": \"\"\n    }\n}\n"
    },
    "url": {
      "raw": "https://{{instance_url}}/campaigns/trigger/send",
      "protocol": "https",
      "host": [
        "{{instance_url}}"
      ],
      "path": [
        "campaigns",
        "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\nThe send endpoint allows you to send immediate, ad-hoc messages to designated users. If you are targeting a segment, a record of your request will be stored in the Developer Console. Please note that to send messages with this endpoint, you must have a Campaign ID, created when you build an API Triggered Campaign.\n\n### Request Parameters\n| Parameter | Required | Data Type | Description |\n| --------- | ---------| --------- | ----------- |\n|`campaign_id`|Required|String|See campaign identifier|\n|`send_id`| Optional | String | See send identifier |\n|`trigger_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 campaign|\n\n### Request Components\n- [Campaign 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- [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 `trigger_properties` object.\n\nWhen `send_to_existing_only` is `true`, Braze will only send the message to existing users. When `send_to_existing_only` is `false` and a user with the given `id` does not exist, Braze will create a user with that id and attributes before sending the message.\n\nFor more information on the \"broadcast\" flag, check out [Broadcast](https://www.braze.com/docs/api/parameters/#broadcast) within our [API Parameters](https://www.braze.com/docs/api/parameters) documentation.\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). \n\n## Create Send Endpoint\n\n__Using the Attributes Object in Campaigns__\n\nBraze has a Messaging Object called `Attributes` that will allow you to add, create, or update attributes and values for a user before you send them an API Triggered Campaigns using the `campaign/trigger/send` endpoint as this API call will process the User Attributes object before it processes and sends the campaign. This helps minimize the risk of there being issues caused by race conditions."
  },
  "response": [
  ]
}