Chilkat Online Tools

C# / Braze Endpoints / Create Email Template

Back to Collection Items

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

Chilkat.Http http = new Chilkat.Http();
bool success;

// 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"
//   ]
// }

Chilkat.JsonObject json = new Chilkat.JsonObject();
json.UpdateString("template_name","email_template_name");
json.UpdateString("subject","Welcome to my email template!");
json.UpdateString("body","This is the text within my email body and https://www.braze.com/ here is a link to Braze.com.");
json.UpdateString("plaintext_body","This is the text within my email body and here is a link to https://www.braze.com/.");
json.UpdateString("preheader","My preheader is pretty cool.");
json.UpdateString("tags[0]","Tag1");
json.UpdateString("tags[1]","Tag2");

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

Chilkat.HttpResponse resp = http.PostJson3("https://rest.iad-01.braze.com/templates/email/create","application/json",json);
if (http.LastMethodSuccess == false) {
    Debug.WriteLine(http.LastErrorText);
    return;
}

Debug.WriteLine(Convert.ToString(resp.StatusCode));
Debug.WriteLine(resp.BodyStr);

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": [
  ]
}