Create and publish from code
Create a template, write its TSX, save a version, and publish it with the SDK or REST calls, then send it by slug without Git or the CLI.
A template project is a Git repository, and the workspace API edits it without a checkout. The calls
run in this order: create, open, patch, check, save, publish, render. Every call after open carries
the workspace revision you last read as expectedRevision, so a stale write fails with a conflict
instead of overwriting newer files. For the same flow from a terminal, see
Authoring in TSX.
Create the template
templates.create takes a name and an optional slug. Without slug, Samva derives a unique
handle from the name. The project starts from the canonical starter, whose one entry is
templates/welcome.tsx with its defineTemplate id set to the template's slug.
import { createClient } from "samva";
const samva = createClient({ apiKey: process.env.SAMVA_API_KEY! });
const template = await samva.templates.create({ name: "Order shipped", slug: "order-shipped" });
console.log(template.id, template.slug, template.projectId, template.entryPath);The response carries the values the later calls need: id (tmpl_...), slug, projectId, and
entryPath (templates/welcome.tsx). Over REST, send Idempotency-Key and authenticate with the
X-API-Key header:
curl -X POST https://api.samva.dev/v1/templates \
-H "X-API-Key: $SAMVA_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: create-order-shipped" \
-d '{ "name": "Order shipped", "slug": "order-shipped" }'Write the files
openWorkspace returns the effective file tree and the workspace snapshot. A new workspace has
revision "0". patchWorkspace replaces whole files (content is the new text) or deletes them
(content: null) and returns the new view; each patch moves revision forward, so read it from
every response. Keep id in defineTemplate equal to the template's slug.
import { readFile } from "node:fs/promises";
const opened = await samva.templates.openWorkspace({ id: template.id });
const patched = await samva.templates.patchWorkspace({
id: template.id,
expectedRevision: opened.workspace.revision,
patches: [{ path: template.entryPath, content: await readFile("order-shipped.tsx", "utf8") }],
});
const checked = await samva.templates.checkWorkspace({
id: template.id,
expectedRevision: patched.workspace.revision,
});
console.log(checked.diagnostics, checked.incompatibilities);checkWorkspace builds the entry from the current files, including unsaved patches, and returns
declaration (null when the entry did not build), diagnostics, and incompatibilities. Nothing is
written. Fix each finding by patching again with the latest revision, then check again. The TSX
itself follows the static profile.
curl -X POST https://api.samva.dev/v1/templates/$TEMPLATE_ID/workspace \
-H "X-API-Key: $SAMVA_API_KEY"
curl -X PATCH https://api.samva.dev/v1/templates/$TEMPLATE_ID/workspace \
-H "X-API-Key: $SAMVA_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: patch-1" \
-d "$(jq -n --rawfile content order-shipped.tsx \
'{ expectedRevision: "0", patches: [{ path: "templates/welcome.tsx", content: $content }] }')"
curl -X POST https://api.samva.dev/v1/templates/$TEMPLATE_ID/workspace/check \
-H "X-API-Key: $SAMVA_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "expectedRevision": "1" }'To keep the template in a file other than templates/welcome.tsx, patch the new file in, delete
the old one with content: null, and point the template at the new path before you save and publish:
const renamed = await samva.templates.update({
id: template.id,
data: { entryPath: "templates/order-shipped.tsx" },
});Publish with renamed.entryPath in place of template.entryPath: a publish whose entryPath
differs from the registered one returns a ConflictError. Publishing also fails while the
registered entry path does not exist at the commit. Over REST the same
call is PATCH /v1/templates/$TEMPLATE_ID with { "data": { "entryPath": "templates/order-shipped.tsx" } }.
Save a version
saveWorkspace commits the files as one Git commit on the project's default branch. It takes the
current expectedRevision, the expectedHead (the 40-character workspace.remoteHead from the
latest view), and a commit message. It validates every template and declared fixture in the
project first, and a failure writes nothing.
const saved = await samva.templates.saveWorkspace({
id: template.id,
expectedRevision: patched.workspace.revision,
expectedHead: patched.workspace.remoteHead,
message: "Order shipped email",
});
console.log(saved.commit, saved.revision);curl -X POST https://api.samva.dev/v1/templates/$TEMPLATE_ID/workspace/save \
-H "X-API-Key: $SAMVA_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: save-1" \
-d "{ \"expectedRevision\": \"1\", \"expectedHead\": \"$REMOTE_HEAD\", \"message\": \"Order shipped email\" }"The receipt's commit is the saved commit and its revision is the workspace revision after the
save.
Publish
A publication pins one exact commit and entry path. publish takes the project, the saved commit,
the entry path, and the workspace revision the save returned. Publishing never uses unsaved patches.
const publication = await samva.templates.publish({
id: template.id,
projectId: template.projectId,
entryPath: template.entryPath,
commitSha: saved.commit,
expectedWorkspaceRevision: saved.revision,
});
const rendered = await samva.templates.renderPublication({
id: template.id,
publicationId: publication.id,
data: { name: "Ada", orderId: "A-1042" },
});
console.log(rendered.subject);curl -X POST https://api.samva.dev/v1/templates/$TEMPLATE_ID/publications \
-H "X-API-Key: $SAMVA_API_KEY" \
-H "Content-Type: application/json" \
-d "{ \"projectId\": \"$PROJECT_ID\", \"entryPath\": \"templates/welcome.tsx\", \"commitSha\": \"$COMMIT\", \"expectedWorkspaceRevision\": \"2\" }"Publishing the same commit again makes the existing publication current instead of recording
another. renderPublication validates data against the publication's input schema and returns the
subject, preheader, HTML, and text a send would produce; nothing is sent.
Send it
Send by slug through the SDK, which takes templateSlug and templateData at the top level:
await samva.email.send({
from: { email: "orders@acme.com" },
to: [{ email: "ada@example.com" }],
templateSlug: template.slug,
templateData: { name: "Ada", orderId: "A-1042" },
});POST /v1/messages nests the same fields under email:
curl -X POST https://api.samva.dev/v1/messages \
-H "X-API-Key: $SAMVA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"channel": "email",
"from": { "email": "orders@acme.com" },
"to": [{ "email": "ada@example.com" }],
"email": {
"templateSlug": "order-shipped",
"templateData": { "name": "Ada", "orderId": "A-1042" }
}
}'templateSlug takes the template's slug, the defineTemplate id unless samva templates add --slug
chose another handle. templateId takes the tmpl_... id. A send names exactly one of them and
omits subject, html, and text. from is an address on a verified domain or a hosted mailbox address; a send without it uses the template's default sender or the organization's, and fails with FromAddressRequiredError when neither is set. See Send an email for
pinning a publication, locale, and time zone.