How Variables Work
When you send a transactional email through the AutoSend Email API, you can include variables as key-value pairs in your request payload. AutoSend automatically replaces these placeholders with their corresponding values in the email template before delivery.Example API Payload:
Example Template:
Resulting Email:
Supported Variable Syntax
AutoSend uses double curly braces{{variable_name}} to identify variables inside email templates.
Variable names can include letters, numbers, and underscores, but must start with a letter (e.g.,
{{ first_name }}, {{ orderTotal }}).Using Variables in Email Templates
You can use variables anywhere in the email:- Subject line
- Body text
- Button URLs
- Links or call-to-actions
Default and Custom Variables
Default Variables
Some system-level variables are automatically available when sending emails through AutoSend. For example:{{email}}— recipient’s email address{{date}}— current date
Custom Variables
You can define your own custom variables when sending emails via the API. These can be anything specific to your workflow. Eg.{{order_id}}, {{invoice_amount}}, or {{reset_link}}.
Nested Variables
AutoSend supports nested variables, allowing you to organize your data in a more structured way. You can access nested properties using dot notation in your templates. For example, instead of flat variables like{{firstName}}, you can use nested structures like {{userData.firstName}} or {{order.id}}.
Example API Payload with Nested Data:
Example Template with Nested Variables:
Resulting Email:
Nested variables are only available with transactional emails. They are not supported in email
campaigns or automation emails.
Default Values
Not every recipient will have a value for every variable. You can use the pipe operator (|) to provide a default value that renders when the variable is missing.
What this does:
- If
firstNameisJohan→Hello Johan! - If
firstNameis missing or empty →Hello there!
- Not included in your API payload
null- An empty string
- Only whitespace
0 or false are treated as real values and render as-is.
Rules for default values
- Wrap the default in quotes. Both double quotes (
") and single quotes (') work, and smart quotes added by rich-text editors are handled automatically. - Use one pipe per variable. Multiple pipes like
{{ name | "a" | "b" }}are not supported. - Whitespace is flexible.
{{firstName|"there"}}and{{ firstName | "there" }}behave the same.
Example:
Using Variables with Conditions
For richer conditional logic, such as showing or hiding entire blocks of content, you can useif/else in your email templates. For example, you can show a different message depending on whether a firstName is available.
What this does:
- If
firstNameexists →Hello Johan! - If it’s missing/empty →
Hello there!
{{#if}}{{else}}{{/if}} when you need to switch larger pieces of content.
Best Practices
- Always test your templates before sending live emails to ensure variables are replaced correctly.
- Provide default values with the pipe operator in case a variable is missing (e.g.,
{{ name | "there" }}renders “there” ifnameis not provided). - Keep variable names descriptive for better clarity and maintainability.
Troubleshooting
If a variable doesn't render correctly:
If a variable doesn't render correctly:
- Check that your variable name in the API payload exactly matches the placeholder in your template.
- Ensure that the variable is passed under the
"variables"object in your API request. - Preview the email in your AutoSend dashboard to confirm the replacement works as expected.
If a default value isn't showing:
If a default value isn't showing:
- Make sure the default is wrapped in quotes:
{{ firstName | "there" }}. An unquoted default is ignored. - Check that the variable is actually empty. Values like
0orfalsecount as real values and render instead of the default. - Use only one pipe per variable.