Schema as code
A workspace's structure can live in files: custom Kinds, traits, pipelines, programs, segments, the workspace's own event types and its webhook endpoints. You keep the files in a repository, review a dry run, and apply them with the kennra CLI or an AI agent. This page covers the token you need, the blueprint format and how changes land.
The token
Schema work needs a token that may propose changes to the data model. Create a developer token with the preset Build the data model under Settings → Tokens, or with the CLI:
kennra tokens create --name "Data model" --preset schema
pbpaste | kennra login --token-stdin
kennra whoami| Preset | Data writes | The data model |
|---|---|---|
Read customer data (read) | None | No changes |
Read and write customer data (read-write) | Companies, people, traits, deals, tasks and programs | No changes |
Build the data model (schema) | As read-write | Proposes custom Kinds, traits, pipelines, programs and segments |
Only a schema owner can create a Build the data model token, so it never proposes what its creator couldn't publish. Anyone else sees the preset turned off, and the API refuses it with SCHEMA_OWNER_ONLY.
kennra whoami has a Schema line that says what the token can do: publishes changes, proposes changes, or no changes. A command that needs schema access stops with exit code 4 when the token has none, and the message names the preset to create.
How changes land
A token, an agent or an app proposes. It never publishes.
| Workspace | What happens to a proposal |
|---|---|
| Test, development or demo | Kennra publishes it at once. The audit log shows the proposal and the publish |
| Staging or production | It waits for a schema owner, who reviews it under Settings → Kinds, Pipelines or Programs |
A person with schema rights (kennra:schema:write or kennra:schema:publish) who signs in with their own token publishes directly. A segment draft waits for a person with kennra:segment:write.
Identifiers never change once they are published. A Kind keeps its name, a relation keeps its name and target, a trait field is deprecated, never renamed. A pipeline or program stage that has deals or enrollments can only go when you map them to another stage (stageMapping, or --stage-mapping old=new in the CLI).
A blueprint
A blueprint is one YAML or JSON file, or a directory of them. Each top-level section is a list in the same shape as the matching apply -f file. A directory is read in file name order and merged.
| Section | What it holds | Same shape as |
|---|---|---|
kinds | Custom Kinds and their relations | kennra kinds apply -f |
traits | Traits with their fields, for any Kind | kennra traits apply -f |
pipelines | Deal pipelines with stages | kennra pipelines apply -f |
programs | Programs, the workflows that are not sales | kennra programs apply -f |
segments | Segment definitions | kennra segments apply -f |
events | The workspace's own event types: rules and emitted types | kennra events define -f |
data | Demo records: companies, people, deals, records and enrollments | |
webhooks | Workspace webhook endpoints, found again by name. Secrets are never in a blueprint | kennra webhooks create |
A machine dealer, as one file:
kinds:
- name: custom.machine
label: Machine
labelPlural: Machines
description: A machine a customer owns.
icon: gear
relations:
- { name: owner, target: organization, required: true, label: Owner, inverseLabel: Machines }
traits:
- name: custom.machine_details
appliesTo: [custom.machine]
description: Model, serial number and year of a machine.
fields:
model: { primitive: text, description: The model., piiClass: none }
serialNumber: { primitive: identifier, description: The serial number., piiClass: none }
year: { primitive: number, description: The year it was built., piiClass: none }
pipelines:
- id: machine_sales
label: Machine sales
transitions: any
stages:
- { id: lead, label: Lead, category: open, probability: 0.1 }
- { id: quote, label: Quote, category: open, probability: 0.6 }
- { id: won, label: Won, category: won }
- { id: lost, label: Lost, category: lost }
segments:
- id: seg_machine_owners
name: Machine owners
kind: organization
purpose: sales.b2b
definition:
root: { type: hasRecord, kind: custom.machine, relation: owner, where: { type: all, conditions: [] } }
data:
organizations:
- { key: nordbygg, name: Nordbygg Entreprenør AS, organizationTypes: [customer] }
records:
- key: hx220
kind: custom.machine
title: HX220 (SN-1042)
links: { owner: nordbygg }
values:
custom.machine_details: { model: HX220, serialNumber: SN-1042, year: 2021 }In data, a key names a record inside the blueprint, so other records can point at it. Kennra finds an existing record by its natural identity (org number or name, email, company and deal name, Kind and title), so running the blueprint again creates nothing twice.
Apply it
kennra apply -f workspace/ --dry-run # one combined review of everything that would change
kennra apply -f workspace/ # applies the layers in order
kennra export --blueprint -o current.yaml- Checked first. Kennra checks the whole blueprint before it writes anything. Every item needs an identifier, nothing may be listed twice, and every reference must resolve in the blueprint or the workspace.
- In order. The layers run as
kinds,traits,pipelines,programs,segments,events,data,webhooks. A run stops at the first layer that fails, and the result names it. - Idempotent. Every step can run again. Unchanged items report as
unchanged, and each item reportscreate,update,unchanged,proposal,blocked,errororskipped. - Waiting proposals. In staging and production, an item that uses a proposal waits for it as
blocked. Runapplyagain once a schema owner has published it. - Data.
datais written in test, development and demo workspaces. Elsewhere it needs--allow-data. - Secrets. A webhook endpoint that a run creates prints its signing secret once.
kennra export --blueprint writes the current structure in the same format, so you can compare two workspaces or copy one to another.
One layer at a time
| Command | Use |
|---|---|
kennra kinds apply -f kinds.yaml | Publish custom Kinds, or propose them when your token can't publish |
kennra kinds draft -f kinds.yaml | Save custom Kinds as proposals and print the review links |
kennra kinds export -o kinds.yaml | Write the custom Kinds as a Kinds file |
kennra traits plan, traits apply, traits draft | Show the diff with the registry, write it, or save drafts for review |
kennra pipelines apply -f pipelines.yaml | Publish pipelines, with --stage-mapping old=new for removed stages |
kennra programs apply -f programs.yaml | Define programs, a new version when something changed |
kennra segments apply -f segments.yaml | Create or update segments, --publish publishes the drafts |
kennra events define -f events.yaml | Define the workspace's own event types. See Your own events |
Every apply takes --dry-run. traits plan --detailed-exitcode exits with code 10 when there are changes, so CI can fail on drift.
From an AI agent
An agent connected over MCP gets the same through tools. Each takes dryRun: true for a review first.
| Tool | What it does |
|---|---|
propose_kind | Proposes a custom Kind with its relations and traits, in one call |
propose_trait | Proposes a trait, or its next version |
propose_pipeline, propose_program | Proposes a pipeline or a program with its stages. Removing a stage that has deals or enrollments needs stageMapping |
define_segment | Proposes a segment as a draft |
propose_event | Proposes one of the workspace's own event types |
apply_blueprint | Applies a whole blueprint, with the same checks and layer order as kennra apply. Demo data only in a test, development or demo workspace |
The response says which way it went: status: "applied" when Kennra published it at once, or status: "pending" with a review link while it waits for a schema owner.
In your own code
The CLI and the MCP server use @kennra/blueprint, an MIT-licensed package on npm. It validates a blueprint, plans it against a workspace, applies it and exports one, with persisted operations only.
bun add @kennra/blueprintWith npm: npm install @kennra/blueprint.
import { readFile } from 'node:fs/promises';
import { applyBlueprint, type BlueprintRunner, parseBlueprint } from '@kennra/blueprint';
// Runs one persisted operation by name with your token.
const runner: BlueprintRunner = {
async run(name, variables, opts) {
const res = await fetch('https://api.kennra.com/graphql', {
method: 'POST',
headers: {
authorization: `Bearer ${process.env.KENNRA_TOKEN}`,
'content-type': 'application/json',
...(opts?.purpose ? { 'kennra-purpose': opts.purpose } : {}),
},
body: JSON.stringify({ operationName: name, variables }),
});
const body = await res.json();
const errors = (body.errors ?? []).map((e: { message: string; extensions?: { code?: string } }) => ({
message: e.message,
code: e.extensions?.code ?? null,
}));
return { data: body.data ?? null, errors };
},
};
const blueprint = parseBlueprint(JSON.parse(await readFile('workspace.json', 'utf8')));
const result = await applyBlueprint(runner, blueprint, { dryRun: true, mode: 'auto' });
console.log(result.summary);mode: 'auto' publishes where your token may and proposes where it may not. mode: 'propose' always proposes. parseBlueprint takes the parsed object, so read YAML with the parser you already use.
Next steps
- Data model: types, links and scopes that traits and Kinds use.
- CLI: every command and its flags.
- MCP: how agents propose schema changes.