Chilkat Online Tools

SQL Server / Braze Endpoints / Sending Messages Immediately via API Only

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.

    -- {
    --   "broadcast": "false",
    --   "external_user_ids": "external_user_identifiers",
    --   "user_aliases": {
    --     "alias_name": "example_name",
    --     "alias_label": "example_label"
    --   },
    --   "segment_id": "segment_identifier",
    --   "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"
    --         }
    --       }
    --     ]
    --   },
    --   "campaign_id": "campaign_identifier",
    --   "send_id": "send_identifier",
    --   "override_frequency_capping": "false",
    --   "recipient_subscription_state": "all",
    --   "messages": {
    --     "android_push": "(optional, Android Push Object)",
    --     "apple_push": "(optional, Apple Push Object)",
    --     "content_card": "(optional, Content Card Object)",
    --     "email": "(optional, Email Object)",
    --     "kindle_push": "(optional, Kindle/FireOS Push Object)",
    --     "web_push": "(optional, Web Push Object)",
    --     "windows_phone8_push": "(optional, Windows Phone 8 Push Object)",
    --     "windows_universal_push": "(optional, Windows Universal Push Object)"
    --   }
    -- }

    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, 'broadcast', 'false'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'external_user_ids', 'external_user_identifiers'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'user_aliases.alias_name', 'example_name'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'user_aliases.alias_label', 'example_label'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'segment_id', 'segment_identifier'
    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, 'campaign_id', 'campaign_identifier'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'send_id', 'send_identifier'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'override_frequency_capping', 'false'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'recipient_subscription_state', 'all'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'messages.android_push', '(optional, Android Push Object)'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'messages.apple_push', '(optional, Apple Push Object)'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'messages.content_card', '(optional, Content Card Object)'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'messages.email', '(optional, Email Object)'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'messages.kindle_push', '(optional, Kindle/FireOS Push Object)'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'messages.web_push', '(optional, Web Push Object)'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'messages.windows_phone8_push', '(optional, Windows Phone 8 Push Object)'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'messages.windows_universal_push', '(optional, Windows Universal Push Object)'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://rest.iad-01.braze.com/messages/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
	-d '{
  "broadcast": "false",
  "external_user_ids": "external_user_identifiers",
  "user_aliases": {
    "alias_name": "example_name",
    "alias_label": "example_label"
  },
  "segment_id": "segment_identifier",
  "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"
        }
      }
    ]
  },
  "campaign_id": "campaign_identifier",
  "send_id": "send_identifier",
  "override_frequency_capping": "false",
  "recipient_subscription_state": "all",
  "messages": {
    "android_push": "(optional, Android Push Object)",
    "apple_push": "(optional, Apple Push Object)",
    "content_card": "(optional, Content Card Object)",
    "email": "(optional, Email Object)",
    "kindle_push": "(optional, Kindle/FireOS Push Object)",
    "web_push": "(optional, Web Push Object)",
    "windows_phone8_push": "(optional, Windows Phone 8 Push Object)",
    "windows_universal_push": "(optional, Windows Universal Push Object)"
  }
}\''
https://rest.iad-01.braze.com/messages/send

Postman Collection Item JSON

{
  "name": "Sending Messages Immediately via API Only",
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"broadcast\": \"false\",\n  \"external_user_ids\": \"external_user_identifiers\",\n  \"user_aliases\": {\n    \"alias_name\": \"example_name\",\n    \"alias_label\": \"example_label\"\n  },\n  \"segment_id\": \"segment_identifier\",\n  \"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  \"campaign_id\": \"campaign_identifier\",\n  \"send_id\": \"send_identifier\",\n  \"override_frequency_capping\": \"false\",\n  \"recipient_subscription_state\": \"all\",\n  \"messages\": {\n    \"android_push\": \"(optional, Android Push Object)\",\n    \"apple_push\": \"(optional, Apple Push Object)\",\n    \"content_card\": \"(optional, Content Card Object)\",\n    \"email\": \"(optional, Email Object)\",\n    \"kindle_push\": \"(optional, Kindle/FireOS Push Object)\",\n    \"web_push\": \"(optional, Web Push Object)\",\n    \"windows_phone8_push\": \"(optional, Windows Phone 8 Push Object)\",\n    \"windows_universal_push\": \"(optional, Windows Universal Push Object)\"\n  }\n}'"
    },
    "url": {
      "raw": "https://{{instance_url}}/messages/send",
      "protocol": "https",
      "host": [
        "{{instance_url}}"
      ],
      "path": [
        "messages",
        "send"
      ]
    },
    "description": "This endpoint allows you send your messages using our API. Be sure to include Messaging Objects in your body to complete your requests.\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 .\n\n### Request Parameters\n\n| Parameter | Required | Data Type | Description |\n| --------- | ---------| --------- | ----------- |\n|`broadcast`|Optional|Boolean|See broadcast -- defaults to false on 8/31/17, must be set to true if \"recipients\" is omitted|\n|`external_user_ids` | Optional | Array of strings | See external identifier |\n|`user_aliases`|Optional|Array of user alias objects| See user alias object|\n|`segment_id `| Optional | String | See segment identifier |\n|`audience`|Optional|Connected audience object|See connected audience|\n|`campaign_id`|Optional*|String| *Required if you wish to track campaign stats (e.g. sends, clicks, bounces, etc) on the Braze dashboard. <br>See campaign identifier for more information|\n|`send_id`| Optional | String | See send identifier |\n|`override_frequency_capping`|Optional|Boolean|Ignore frequency_capping for campaigns, defaults to false |\n|`recipient_subscription_state`|Optional|String|Use this to send messages to only users who have opted in ('opted_in'), only users who have subscribed or are opted in ('subscribed') or to all users, including unsubscribed users ('all'), the latter being useful for transactional email messaging. Defaults to 'subscribed'|\n|`messages`| Optional | Messaging objects | See available messaging objects|\n\n### Request Components\n- [Broadcast](https://www.braze.com/docs/api/parameters/#broadcast)\n- [User Alias Object](https://www.braze.com/docs/api/objects_filters/user_alias_object/)\n- [Segment Identifier](https://www.braze.com/docs/api/identifier_types/)\n- [Connected Audience](https://www.braze.com/docs/api/objects_filters/connected_audience/)\n- [Campaign Identifier](https://www.braze.com/docs/api/identifier_types/)\n- [Recipients](https://www.braze.com/docs/api/objects_filters/recipient_object/)\n- [API Parameters](https://www.braze.com/docs/api/parameters)\n\n### Available Messaging Objects\n\n- [Android Objects](https://www.braze.com/docs/api/objects_filters/android_objects/)\n- [Apple Objects](https://www.braze.com/docs/api/objects_filters/apple_objects/)\n- [Content Cards Object](https://www.braze.com/docs/api/objects_filters/content_cards_object/)\n- [Email Object](https://www.braze.com/docs/api/objects_filters/email_object/)\n- [Kindle or FireOS Object](https://www.braze.com/docs/api/objects_filters/kindle_and_fireos_object/)\n- [SMS Object](https://www.braze.com/docs/api/objects_filters/sms_object/)\n- [Web Objects](https://www.braze.com/docs/api/objects_filters/web_objects/)\n- [Webhook Object](https://www.braze.com/docs/api/objects_filters/webhook_object/)\n- [Windows Objects](https://www.braze.com/docs/api/objects_filters/windows_objects/)\n\n\n## Response Details\n\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]({{site.baseurl}}/help/help_articles/data/dispatch_id/)."
  },
  "response": [
  ]
}