Chilkat Online Tools

SQL Server / Braze Endpoints / Create Email 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.

    -- {
    --   "template_name": "email_template_name",
    --   "subject": "Welcome to my email template!",
    --   "body": "This is the text within my email body and https://www.braze.com/ here is a link to Braze.com.",
    --   "plaintext_body": "This is the text within my email body and here is a link to https://www.braze.com/.",
    --   "preheader": "My preheader is pretty cool.",
    --   "tags": [
    --     "Tag1",
    --     "Tag2"
    --   ]
    -- }

    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, 'template_name', 'email_template_name'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'subject', 'Welcome to my email template!'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'body', 'This is the text within my email body and https://www.braze.com/ here is a link to Braze.com.'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'plaintext_body', 'This is the text within my email body and here is a link to https://www.braze.com/.'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'preheader', 'My preheader is pretty cool.'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'tags[0]', 'Tag1'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'tags[1]', 'Tag2'

    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/templates/email/create', '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 '{
  "template_name": "email_template_name",
  "subject": "Welcome to my email template!",
  "body": "This is the text within my email body and https://www.braze.com/ here is a link to Braze.com.",
  "plaintext_body": "This is the text within my email body and here is a link to https://www.braze.com/.",
  "preheader": "My preheader is pretty cool.",
  "tags": ["Tag1", "Tag2"]
}'
https://rest.iad-01.braze.com/templates/email/create

Postman Collection Item JSON

{
  "name": "Create Email Template",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json",
        "type": "text"
      },
      {
        "key": "Authorization",
        "value": "Bearer {{api_key}}",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n  \"template_name\": \"email_template_name\",\n  \"subject\": \"Welcome to my email template!\",\n  \"body\": \"This is the text within my email body and https://www.braze.com/ here is a link to Braze.com.\",\n  \"plaintext_body\": \"This is the text within my email body and here is a link to https://www.braze.com/.\",\n  \"preheader\": \"My preheader is pretty cool.\",\n  \"tags\": [\"Tag1\", \"Tag2\"]\n}"
    },
    "url": {
      "raw": "https://{{instance_url}}/templates/email/create",
      "protocol": "https",
      "host": [
        "{{instance_url}}"
      ],
      "path": [
        "templates",
        "email",
        "create"
      ]
    },
    "description": "Use the Template REST APIs to programmatically manage the email templates that you have stored on the Braze dashboard, on the Templates & Media page. Braze provides two endpoints for creating and updating your email templates.\n\nUsers' email subscription status can be updated and retrieved via Braze using a RESTful API. You can use the API to set up bi-directional sync between Braze and other email systems or your own database. All API requests are made over HTTPS.\n\nUse the endpoints below to create email templates on the Braze dashboard. These templates will be available on the Templates and Media page. The response from this endpoint will include a field for `email_template_id`, which can be used to update the template in subsequent API calls.\n\n### Request Parameters\n\n| Parameter | Required | Data Type | Description |\n| --------- | ---------| --------- | ----------- |\n|`template_name`|Optional|String|The name of your email template|\n|`subject`|Optional|String|The email template subject line|\n|`body`|Optional|String|The email template body that may include HTML|\n|`plaintext_body`|Optional|String|A plaintext version of the email template body|\n|`preheader`|Optional|String|The email preheader used to generate previews in some clients|\n|`tags`|Optional|String|Tags must already exist|\n|`should_inline_css`|Optional|Boolean|Enables or disables the 'inline_css' feature per template.  If  not provided, Braze will use the default setting for the AppGroup.  One of 'true' or 'false' is expected|\n\n### Possible Errors\n- `Template Name is required`\n\n- `Tags must be an array.`\n\n- `All Tags must be Strings.`\n\n- `Some Tags could not be found.` - A tag was specified which doesn't exist in this environment.\n\n- `Email must have valid content block names.` - The email contains Content Blocks which do not exist in this environment.\n\n- `\"Invalid value for 'should_inline_css'.  One of 'true' or 'false' was expected\"` - 'should_inline_css' accepts boolean characters only.  The error likely is being shown as the value is being sent as a 'string'."
  },
  "response": [
  ]
}