Skip to content

Managing templates from code

The Management API lets you keep templates and layouts next to your code and publish them from CI. It uses a management key (vm_…) from the dashboard’s API keys page.

PUT creates the template if the ID is new, or replaces it:

Terminal window
curl -X PUT https://api.velumrelay.email/v1/templates/welcome \
-H "Authorization: Bearer $VELUM_MANAGEMENT_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "Welcome email",
"subject": "Welcome, {{name}}!",
"html": "<h1>Hello {{name}}</h1>",
"layout": "default"
}'
{ "id": "welcome", "version": 1, "created": true }

The response’s ETag header holds the new version ("1"). Changes reach the sending API within a few seconds.

If templates are also edited in the dashboard, make updates conditional:

  • If-Match: "3" only updates if the current version is 3. If someone changed it since, you get 412 with the current_version, and nothing is overwritten.
  • If-None-Match: * only creates. If the ID already exists, you get 412.
Terminal window
# Read the current version from the ETag...
curl -sI https://api.velumrelay.email/v1/templates/welcome -H "Authorization: Bearer $VELUM_MANAGEMENT_KEY" | grep -i etag
# ...and update only if it hasn't changed.
curl -X PUT https://api.velumrelay.email/v1/templates/welcome \
-H "Authorization: Bearer $VELUM_MANAGEMENT_KEY" \
-H 'If-Match: "3"' \
-H "Content-Type: application/json" \
-d '{ "subject": "Welcome aboard, {{name}}!", "html": "<h1>Hello {{name}}</h1>" }'

Render a draft without saving it, inside one of your layouts:

Terminal window
curl https://api.velumrelay.email/v1/preview \
-H "Authorization: Bearer $VELUM_MANAGEMENT_KEY" \
-H "Content-Type: application/json" \
-d '{ "subject": "Hi {{name}}", "html": "<p>Hello {{name}}</p>", "layout": "default", "data": { "name": "Ada" } }'

A template that isn’t valid Mustache, or that uses an include, is rejected with 422 and a message saying why, both when you preview and when you save.

Layouts work the same way under /v1/layouts. Create layouts before the templates that use them: assigning a layout that doesn’t exist yet is rejected with 422. A layout that templates use can’t be deleted (409, with the templates listed in used_by).