Cadenya TypeScript SDK
@cadenya/cadenya is generated from the Cadenya API contract, so method names, field names and enum values match the REST API: client.<resource>.<method>(...). Exact signatures are in node_modules/@cadenya/cadenya/api.md, and every type is exported from the package root. Wire conventions shared by every client (IDs, external IDs, field masks, error codes) are in the cadenya-api skill, and its references/resources.md has the request body of every resource; SDK params use the same camelCase keys.
Install and import
npm install @cadenya/cadenya
ESM only, no require. It has no dependencies and uses the global fetch.
import Cadenya, { APIError, APIConnectionError, Webhooks } from '@cadenya/cadenya';
import type { Agent, ObjectiveEvent, ToolSetCreateParams } from '@cadenya/cadenya';
API key and workspace
- No account yet: give your user https://app.cadenya.com/signup (the Free plan needs no card). Keys are at https://app.cadenya.com/account/apiKeys.
- Put the key in
CADENYA_API_KEY. Never hardcode it, and never ship it to a browser. The SDK reads only its options and the environment, never the CLI's stored login. (await client.profiles.whoami()).defaultWorkspace?.idis the default workspace, the value forCADENYA_WORKSPACE_ID.- For setup that must be repeatable, keep Tool Sets, Agents, variations, memory layers and Widgets as YAML in
.cadenya/and apply them withcadenya config apply(format). Use the SDK for runtime work.
Client
const client = new Cadenya({ workspaceId: 'development', maxRetries: 2 }); // apiKey from CADENYA_API_KEY
| Option | Env fallback | Default |
|---|---|---|
apiKey |
CADENYA_API_KEY |
required; missing everywhere throws at construction |
workspaceId |
CADENYA_WORKSPACE_ID |
none; every workspace-scoped call needs one |
baseURL |
CADENYA_BASE_URL |
https://api.cadenya.com |
webhookSecret |
CADENYA_WEBHOOK_SECRET |
used by client.webhooks.unwrap |
timeout |
60000 ms; 0 or less disables it | |
maxRetries |
0 | |
logLevel, logger |
'warn', console |
|
fetch, defaultHeaders |
global fetch, none |
- An option passed explicitly but blank throws
<name> must not be blank when provided explicitly; omit it to use the environment instead. workspaceIdtakes aworkspace_…ID or the workspace's external ID. Every params object also takesworkspaceIdfor one call. With neither, the call rejects withMissing 'workspaceId': pass it in params, set it on the client, or set the CADENYA_WORKSPACE_ID environment variable.
Method and type names
- The method is the operation verb without the resource words;
Getisretrieve. - Positional arguments: parent IDs, then the resource's own ID, then one params object (body and query together), then
RequestOptions.client.agents.variations.retrieve(agentId, id, params?, options?) - Params types are
<Singular><Op>Params(AgentCreateParams). Enums are string literal unions ('STATE_PUBLISHED').
The full accessor and method list is in references/resources.md.
Request bodies
- Keys are camelCase, as on the wire. Resources take
{ metadata, spec }; objective create is flat. - Unions carry a
typetag that names the populated key:{ type: 'mcp', mcp: { … } }. undefinedfields are dropped.Paramtypes strip read-only fields, so a fetched resource can be edited and sent back.- Durations are strings:
inactivityTimeout: '3600s'. - Tool Set adapters:
{ type: 'mcp', mcp: { url } },{ type: 'openapi', openapi: { type: 'url', url } }(or{ type: 'uploadId', uploadId }),{ type: 'http', http: { baseUrl, headers } },{ type: 'bare', bare: {} }. An HTTP Tool Set has notoolApprovals; create each tool withtoolSets.tools.create(toolSetId, { metadata, spec: { requiresApproval, parameters, config: { type: 'http', http: { requestMethod: 'GET', path, query } } } }), where templates read arguments as{{ arguments.<name> }}. - Secrets interpolate only as
{{ secrets.NAME }}, from a workspace secret or the Tool Set's ownspec.secrets. The package's doc comments mention${SECRET_NAME}; that form is rejected withlegacy dollar references are not supported. - Tools attach to variations, not Agents.
addAssignmenttakes the union itself as params:addAssignment(agentId, variationId, { type: 'toolSetId', toolSetId }). Memory layers attach to variations too:addMemoryLayer(agentId, variationId, { memoryLayerId, position: 0 }). Repeating either is 409. - Rotate a Tool Set secret with
toolSets.secrets.update(toolSetId, 'MCP_TOKEN', { spec: { value } }); the secret's ID accepts its uppercased name. models.swapOnVariations({ modelSwaps: [{ currentModelId, nextModelId }] })changes every variation in the workspace on that model. For one Agent,agents.variations.updateeach variation withupdateMask: 'spec.modelConfig.modelId'.
Workflow: provision and run
- Trust the Tool Set's host, unless Cadenya hosts it:
trustedEndpoints.create({ metadata: { name }, spec: { host } })(409 if it exists), thentrustedEndpoints.verify({ url })returnsresult: 'RESULT_TRUSTED'. A Tool Set on an untrusted host is rejected at create. - Create the Tool Set, with its secret in
spec.secrets. - Pick an enabled model with
models.list. Do not hardcode a model ID: the catalog differs per workspace and provider key. - Create the Agent and its default variation, with the Tool Set assigned, in one call.
- Publish the Agent. Objectives on a
STATE_DRAFTAgent are rejected. - Run an objective with
createAndStream, and handle its events. - Verify: the stream ended with a
stateChangedtoOBJECTIVE_STATE_WAITINGor a terminal state.
import Cadenya from '@cadenya/cadenya';
const client = new Cadenya({ workspaceId: 'development' });
// MCP and OpenAPI creates check the source live and must find at least one tool.
// Passing the secret here makes a bad URL or token fail now, not at run time.
const toolSet = await client.toolSets.create({
metadata: { name: 'Support MCP', externalId: 'support-mcp' },
spec: {
adapter: {
type: 'mcp',
mcp: {
url: 'https://mcp.example.com/mcp',
headers: { Authorization: 'Bearer {{ secrets.MCP_TOKEN }}' },
toolApprovals: { type: 'always', always: true }, // every tool call waits for approval
},
},
secrets: [{ name: 'MCP_TOKEN', value: process.env.MCP_TOKEN! }],
},
});
// Narrow with { query } (name contains) or { prefix } (start of name, external ID or ID).
const model = (await client.models.list({ state: 'STATE_ENABLED', limit: 1 })).items[0];
if (!model) throw new Error('no enabled model in this workspace');
const agent = await client.agents.create({
metadata: { name: 'Support Triager', externalId: 'support-triager' },
spec: {}, // variationSelectionMode defaults to WEIGHTED
defaultVariation: {
metadata: { name: 'default', externalId: 'support-triager-default' },
spec: {
systemPromptTemplate: 'You triage support tickets for {{ system_prompt_data.customer_name }}.',
modelConfig: { modelId: model.metadata.id }, // or '<provider-key-external-id>.<model-external-id>'
constraints: { maxToolCalls: 20, inactivityTimeout: '3600s' },
assignments: [{ type: 'toolSetId', toolSetId: toolSet.metadata.id }],
},
},
});
await client.agents.publish(agent.metadata.id);
agents.create returns the Agent only (info.variationCount, no variation ID). Get the variation with agents.variations.list(agentId).
Re-running this script fails with 409 resource with the same identifier already exists, because the external IDs are taken. Prefer cadenya config apply for repeatable setup; in a script, retrieve('external_id:…') first and create only on 404.
Streaming objective events
Start and watch an objective with createAndStream:
- It needs
metadata.externalId, the idempotency key. A repeat of the request, including the SDK's automatic reconnect, attaches to the existing objective instead of starting another. - The first frame is
{ type: 'objective', objective }, and every later frame is{ type: 'event', event }. - The stream ends by itself once the objective is
OBJECTIVE_STATE_WAITING(the turn is over) or terminal (OBJECTIVE_STATE_FINALIZED,OBJECTIVE_STATE_FAILED,OBJECTIVE_STATE_CANCELLED,OBJECTIVE_STATE_TIMED_OUT).
import { APIError, type ObjectiveEvent } from '@cadenya/cadenya';
const SETTLED = new Set(['OBJECTIVE_STATE_WAITING', 'OBJECTIVE_STATE_FINALIZED', 'OBJECTIVE_STATE_FAILED', 'OBJECTIVE_STATE_CANCELLED', 'OBJECTIVE_STATE_TIMED_OUT']);
// Returns true once the objective has settled.
async function handle(objectiveId: string, event: ObjectiveEvent): Promise<boolean> {
const data = event.data; // discriminated on data.type; the payload is under the same-named key
switch (data.type) {
case 'toolApprovalRequested':
try {
await client.objectives.approveToolCall(objectiveId, data.toolApprovalRequested.toolCallId);
} catch (err) {
// A replayed request for a call already approved fails with 400 `not waiting for approval`.
if (!(err instanceof APIError && err.status === 400)) throw err;
}
return false;
case 'assistantMessage':
if (data.assistantMessage.content) console.log(data.assistantMessage.content);
return false;
case 'stateChanged':
return SETTLED.has(data.stateChanged.toState);
default:
return false;
}
}
const stream = await client.objectives.createAndStream({
agentId: 'external_id:support-triager',
metadata: { externalId: 'ticket-4821' },
systemPromptData: { customer_name: 'Ada' },
firstUserMessage: 'Summarize the open tickets.',
});
let objectiveId = '';
let settled = false;
for await (const frame of stream) {
if (frame.type === 'objective') {
objectiveId = frame.objective.metadata.id;
settled = SETTLED.has(frame.objective.state); // a repeat that attaches to a settled objective gets no events
} else if (await handle(objectiveId, frame.event)) settled = true;
}
let lastEventId = stream.lastEventId; // checkpoint for the next turn
if (settled && !lastEventId) {
// externalId reused from a settled run: checkpoint at its newest event
lastEventId = (await client.objectives.listEvents(objectiveId, { sortOrder: 'desc', limit: 1 })).items[0]?.metadata.id;
}
if (!settled) await watch(); // cut before the turn ended, or attached to a run still working
// Follows the objective from the checkpoint until it settles.
async function watch(): Promise<void> {
if (!lastEventId) {
// No checkpoint: a live-only stream never shows a settled objective or a pending approval, so replay first.
let done = false;
for await (const event of await client.objectives.listEvents(objectiveId, { sortOrder: 'asc' })) {
const s = await handle(objectiveId, event);
if (event.data.type === 'stateChanged') done = s; // the latest state change decides
lastEventId = event.metadata.id;
}
if (done) return;
}
for (;;) {
const turn = await client.objectives.streamEvents(objectiveId, undefined, { lastEventId });
try {
for await (const event of turn) {
if (await handle(objectiveId, event)) return;
}
} finally {
lastEventId = turn.lastEventId;
await turn.close();
}
}
}
To send the next turn, wait until the current one has settled, call continue, then watch() again:
await client.objectives.continue(objectiveId, { message: 'Which one is most urgent?' });
await watch();
streamEvents attaches to an existing objective. Code around these rules:
- Without
lastEventIdit delivers only events that arrive after it connects. WithlastEventIdit replays everything after that event, then goes live. With no checkpoint,watch()replayslistEvents(id, { sortOrder: 'asc' })first and streams only if the lateststateChangedhas not settled.OBJECTIVE_STATE_INTERRUPTINGis not settled. - The server never closes this stream. Break on a settling
stateChanged, thenclose(). - Only persisted
objevt_IDs are resume points. The stream also yields heartbeat (hb_) and delta (dlt_) events, and resuming from one of their IDs fails with 400Last-Event-ID must be a persisted objevt_ event ID.stream.lastEventIdholds only persisted IDs, so checkpoint with it, not withevent.metadata.id. - A transport drop mid-stream reconnects from
stream.lastEventId, up to 5 attempts per outage, then the loop throwsAPIConnectionError. Pass{ reconnect: false }to get it at once and handle drops yourself. createAndStream(params, { lastEventId })resumes an attach from a checkpoint, likestreamEvents, and both streams haveclose().- Resuming a checkpoint stored by an earlier process can hang
streamEventswhen the objective settled at or before it:retrievethe objective first, and ifstateis settled, readlistEvents(id, { sortOrder: 'asc', sinceEventId })instead. An approval requested before the checkpoint is not replayed; find it withlistToolCalls(id, { status: 'TOOL_CALL_STATUS_WAITING_FOR_APPROVAL' }). - Connections are cut after about 10 minutes. A clean end does not reconnect, and the loop ends without a settling event.
watch()above reopens from the checkpoint. - A
Streamcan be iterated once.stream.events()yields{ event, data, id }(the SSE event name and id alongside the decoded data) instead of the data alone.
Reads, pagination and updates
List methods return Promise<Page<T>> with items, nextCursor, hasNextPage(), getNextPage(), and an async iterator over every page. Filters mirror the API query (limit, cursor, prefix, query, labels, state, sortOrder, includeInfo).
for await (const agent of await client.agents.list({ labels: 'env=prod', limit: 50 })) console.log(agent.metadata.id);
update sends metadata, spec and updateMask (comma-separated camelCase paths). Only masked paths change, and maps and lists are replaced whole. Verify with retrieve afterwards.
await client.agents.update(agentId, { metadata: { name: 'Support Triager v2' }, updateMask: 'metadata.name' });
Errors, retries and options
Every error class extends Error; check with instanceof.
| Class | When | Fields |
|---|---|---|
APIError |
non-2xx after retries | status (HTTP), code (google.rpc), message, details |
APIConnectionError |
fetch threw |
cause |
APITimeoutError |
timeout elapsed |
|
APIUserAbortError |
your signal aborted |
|
APIRequestError |
body not serializable; nothing sent | |
APIResponseError |
2xx with empty, null or malformed JSON |
status |
- 400 with
messagevalidation failed:detailsholds agoogle.rpc.BadRequestwhosefieldViolations[]give the proto path (spec.model_config.model_id) and adescription. - 403: the key lacks a scope;
err.details?.[0]?.reasonnames it (for exampleSCOPE_MISSING). Errors from stream endpoints carry onlymessage, with nodetails. - 404 on an
external_id:lookup: wrong workspace, or the external ID was never set.
RequestOptions, the last argument: timeout, maxRetries, signal, headers, lastEventId, reconnect.
- Retries are off by default (
maxRetries: 0). When enabled, they cover GET, HEAD, PUT and DELETE on 408, 409, 429, 500, 502, 503, 504 and connection errors, and honorRetry-After. A POST or PATCH retries only when that call passes{ maxRetries: n }. - For streams,
timeoutbounds only getting the response headers; the body runs until yoursignalaborts. awaitgives the value;.withResponse()gives{ data, response };.asResponse(), called before anyawait, gives the rawResponse. List methods return plain promises.logLevel: 'debug'logs one line per request and response (method, path, status, duration), never headers or bodies.
Webhooks
app.post('/cadenya', express.raw({ type: 'application/json' }), async (req, res) => {
const event = await client.webhooks.unwrap(req.body.toString('utf8'), req.headers); // or new Webhooks(secret).unwrap
switch (event.data.objectiveEvent.data.type) { // every event kind, the same union as the stream
case 'toolApprovalRequested': /* event.data.objective.id is the objective */ break;
}
res.sendStatus(200);
});
- Verification is Standard Webhooks: headers
webhook-id,webhook-timestampandwebhook-signature, HMAC-SHA256, 300-second tolerance. Failures throwWebhookVerificationError. - Pass the raw body. A re-serialized JSON body fails verification.
- The secret starts with
whsec_and comes from(await client.accounts.retrieve()).info.webhookEventsHmacSecret. Put it inCADENYA_WEBHOOK_SECRET. event.type('objective_event.tool_approval_requested', …) is typed for only 14 kinds, so comparing it with'objective_event.finalized'is a type error although that delivery arrives. Switch onevent.data.objectiveEvent.data.type.
Gotchas
| Symptom | Cause | Fix |
|---|---|---|
400 validation failed on agent_id: agent must be published to create an objective (from createAndStream: validation failed alone, since stream errors carry no details) |
Agent is STATE_DRAFT |
client.agents.publish(id) |
400 on spec.model_config.model_id |
wrong form, or model disabled | a model_… ID from models.list({ state: 'STATE_ENABLED' }), or <provider-key-external-id>.<model-external-id> |
429 the account has reached its plan's limit of N for agent_variations (ENTITLEMENT_LIMIT_REACHED) |
the plan caps variations of non-archived Agents across the whole account; defaultVariation counts |
delete unused variations, archive an unused Agent, or upgrade the plan (an unclaimed temporary account gets REQUIRES_CLAIM instead) |
409 resource with the same identifier already exists |
the external ID is taken | retrieve('external_id:…') and update, or cadenya config apply |
400 metadata.externalId is required from createAndStream |
no idempotency key | set metadata.externalId |
streamEvents yields nothing for a settled objective |
no lastEventId |
replay with listEvents and stream only if it has not settled, as watch() does |
streamEvents never ends |
that stream never closes | break on a settling stateChanged and close() |
stream already consumed |
iterated a Stream twice |
open a new stream with { lastEventId: stream.lastEventId } |
| POST not retried | retries cover idempotent methods only | pass { maxRetries: n } on that call |
Related skills
cadenya-api: wire conventions and per-resource request shapes.cadenya-cli: the same operations from a terminal.cadenya … --dry-runprints the JSON body a request sends.cadenya-widgets: minting Widget Sessions with this SDK for a browser chat.
