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.
Create or update
Section titled “Create or update”PUT creates the template if the ID is new, or replaces it:
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.
Don’t overwrite someone else’s change
Section titled “Don’t overwrite someone else’s change”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 get412with thecurrent_version, and nothing is overwritten.If-None-Match: *only creates. If the ID already exists, you get412.
# 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>" }'Check before you publish
Section titled “Check before you publish”Render a draft without saving it, inside one of your layouts:
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
Section titled “Layouts”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).