Reusing content with snippets

Snippets are common chunks of content that you can share across your emails, text messages, and more.

Creating Snippets

You can create snippets through our API or in the Customer.io UI. To simplify things, let’s create your first snippet in the UI.

  1. Go to Content > Snippets.
    Snippets in the left navigation menu
    Snippets in the left navigation menu
  2. Click Create New Snippet.
  3. Enter a Name and Value for the snippet. The liquid tag you’ll use to reference the snippet automatically updates as you enter name and value.
    Create a new Snippet
    Create a new Snippet

     Avoid spaces in snippet names

    While snippet names can contain spaces, we recommend using underscores or hyphens to separate words, so they’re easier to use in messages and less prone to errors.

  4. Click Save.
List Snippets
List Snippets

Using Snippets

You can use snippets anywhere that you can use liquid, including layouts, email bodies, subjects, slack messages, and more. Reference a snippet using {{snippets.<name_of_your_snippet>}}. If your Snippet name contains spaces, you can use bracket notation instead:

{{snippets.address}} {{snippets["main address"]}}

Using Snippets
Using Snippets

 It can take a few minutes for us to process changes to your snippet.

This means your customers may receive outdated content if, for instance, your campaign is active, you update a snippet in an email, then 30 seconds later we send that email. This could also happen if the message is at the beginning of your workflow, you update a snippet in the message, then immediately activate a campaign or trigger a broadcast, newsletter, or transactional message that contains it.

Advanced Example

You can store JSON data in a snippet, and use liquid to iterate over the JSON. For example, here’s a snippet {{snippets.random_greetings}} containing an array of objects, each object representing a random greeting:

[
  {
    "version": "0",
    "greeting":"How is your day going?"
  },
  {
    "version": "1",
    "greeting":"Hope your week is going well."
  },
  {
    "version": "2",
    "greeting":"Hey there!"
  },
  {
    "version": "3",
    "greeting":"Hope your day is going great so far."
  }
]

You can use JSON dot notation to access variables in your snippet. You can also display a random greeting from the snippet in your message using the following liquid code:

{% capture randomize%}{% random 3 %}{% endcapture %}
{% for g in snippets.random_greetings%}
  {% if g.version == randomize %}
    {{g.greeting}}
  {% endif %}
{% endfor %}

Frequently asked questions

  1. Can I use Liquid within a Snippet? Yes, liquid within a Snippet will render in your message normally. For example, adding {{customer.id}} to your Snippet content will show a person’s id attribute wherever you use your snippet. You can even store liquid in JSON objects and strings, helping you store variable information. (You cannot store liquid in a JSON array in a snippet. Liquid inside a JSON array will simply render as text when you use your snippet.)

     Snippets can’t reference variables from a message body

    While snippets can contain liquid, you cannot reference variables that you set in the message body. Message body variables are out of the snippet scope and won’t code be parsed.

  2. Can I change the name of a Snippet? No, you cannot change the name of a snippet. To use a different name, copy the snippet body and create a new snippet.
  3. Can I use HTML in a Snippet? Yes! HTML in a snippet renders normally in anywhere where HTML is allowed. Just make sure that you intend to use a snippet in a place that renders HTML first. For example, HTML in a subject line will not render. If you use a snippet including HTML in your subject line, your audience will see raw code.
  4. Is there a size limit for a snippet? Yes, the default max size for a snippet is 16kb, which can be increased on a case-by-case basis. Contact win@customer.io to request an increase.
Copied to clipboard!
  Contents
Is this page helpful?