Snippets

Run in Postman
Import the SparkPost API as a Postman collection

Snippets are short reusable pieces of email content. Snippets can be imported into any template content or transmission content using substitution syntax.

For example, if you have a snippet with the ID ourfooter and the following HTML content:

<footer>
    <p>Our HTML footer</p>
</footer>

The snippet can be imported into an HTML email's content using the render_snippet macro call.

<html>
    <p>Our body content</p>
    {{ render_snippet( "ourfooter" ) }}
</html>

The resulting rendered HTML content would look like:

<html>
    <p>Our body content</p>
    <footer><p>Our HTML footer</p></footer>
</html>

See the Template Language documentation for a more detailed explanation of the render_snippet macro and more complicated examples.

Snippet Object

Attributes

id string

Unique ID used to reference the snippet.

name string

Display name

content object

HTML, AMPHTML, and/or text content.

shared_with_subaccounts boolean

Whether this snippet can be used by subaccounts.

subaccount_id number

Subaccount the snippet belongs to. Only present on snippets that belong to a subaccount.

Request

POST /api/labs/snippets
{
  "id": "header",
  "name": "Header snippet",
  "content": {
    "html": "<b>Our cool header snippet for {{name}}</b>"
  }
}

Response

{
  "results": {
    "id": "header"
  }
}
{
  "errors": [
    {
      "message": "Error while compiling part text: line 1: substitution statement missing ending '}}'"
    }
  ]
}

Create a Snippet

POST/api/labs/snippets

Request Body

id string required

Unique ID used to reference the snippet.

name string

Display name

content object required

HTML, AMPHTML, and/or text content. Substitution syntax is supported with the exception of the render_dynamic_content() and render_snippet() macro calls. In other words, snippets may not reference other snippets.

shared_with_subaccounts boolean required, default is false

Whether this snippet can be used by subaccounts.

Request

GET /api/labs/snippets/ourfooter

Response

{
  "results": {
    "id": "ourfooter",
    "name": "Footer",
    "content": {
      "html": "<b>Our standard footer</b>",
      "text": "Our standard footer",
      "amp_html": "<b>Our standard footer</b>"
    },
    "created_at": "2018-10-11T19:13:29.548Z",
    "updated_at": "2018-10-11T19:14:50.181Z",
    "subaccount_id": 273
  }
}

Retrieve a Snippet

GET/api/labs/snippets/{id}

Parameters

id string required

Request: Update

PUT /api/labs/snippets/ourfooter
{
  "content": {
    "html": "<b>Our updated footer</b>",
    "text": "Our updated footer",
    "amp_html": "<b>Our updated footer</b>"
  }
}

Response

// Empty response body

Request: Update to Remove the HTML Part

PUT /api/labs/snippets/ourfooter
{
  "content": {
    "html": "",
    "amp_html": "<b>Our updated footer</b>"
  }
}

Response

// Empty response body

Update a Snippet

PUT/api/labs/snippets/{id}

Parameters

id string required

Unlike the Templates API, if a content object is provided in the update request, it only needs to include the fields that are being updated. Fields that are not included will keep their current values.

To remove a content field that was specified previously, simply set that field to an empty string in the update request.

Request Body

name string

Display name

content object

HTML, AMPHTML, and/or text content. Substitution syntax is supported with the exception of the render_dynamic_content() and render_snippet() macro calls. In other words, snippets may not reference other snippets.

shared_with_subaccounts boolean

Whether this snippet can be used by subaccounts.

Request

DELETE /api/labs/snippets/ourfooter

Response

// Empty response body

Delete a Snippet

DELETE/api/labs/snippets/{id}

Parameters

id string required

Request

GET /api/labs/snippets

Response

{
  "results": [
    {
      "id": "header",
      "name": "Header",
      "shared_with_subaccount": false,
      "created_at": "2018-10-11T19:13:29.548Z",
      "updated_at": "2018-10-11T19:14:50.181Z"
    },
    {
      "id": "footer",
      "name": "Footer",
      "shared_with_subaccount": true,
      "created_at": "2018-10-05T20:21:04.853Z",
      "updated_at": "2018-10-09T19:23:53.022Z"
    }
  ]
}

List all Snippets

GET/api/labs/snippets

Parameters

shared_with_subaccounts boolean

If true, returns only shared snippets. If false, returns only non-shared snippets. When not provided, returns both shared and non-shared snippets.

Returns an array with all your Snippets.