JavaScript-SDK
@spacivapp/ivy-sdk is a typed client for the Ivy API, generated from the same OpenAPI document as the API reference. It has one function per operation, named after its operationId (for example listEntries or listConversations), and TypeScript types for every request and response. It runs in Node.js 22 or later and in other runtimes with fetch.
Install
The package is published to GitHub Packages. Point the @spacivapp scope at its registry in your project’s .npmrc, with a GitHub token that has the read:packages scope:
@spacivapp:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}
Then install it:
pnpm add @spacivapp/ivy-sdk
Configure the client
import { client } from "@spacivapp/ivy-sdk";
client.setConfig({
baseUrl: process.env.IVY_API_URL,
headers: { Authorization: `Bearer ${process.env.IVY_API_KEY}` },
});
IVY_API_KEY is a service identity’s API key from your secrets vault; see Authentication. To pick up a rotated key without restarting, set the header per request instead:
client.interceptors.request.use(async (request) => {
request.headers.set("Authorization", `Bearer ${await readApiKey()}`);
return request;
});
Call an operation
Every function returns { data, error, response }. error is the API’s ErrorBody:
import { listConversations } from "@spacivapp/ivy-sdk";
const { data, error, response } = await listConversations({
path: { organisation_id: organisationId, space_id: spaceId },
query: { limit: 50 },
});
if (error) {
throw new Error(`${response.status} ${error.error.code}: ${error.error.message}`);
}
for (const conversation of data.items) console.log(conversation.id, conversation.title);
Stream a reply
streamAgentMessage sends a message and yields the assistant’s reply as typed events. It needs a key with conversations:write. If the connection drops, it reconnects to the same run with Last-Event-ID, so the message isn’t sent twice and no event is lost.
import { AgentStreamError, createConversation, streamAgentMessage } from "@spacivapp/ivy-sdk";
const { data: conversation } = await createConversation({
path: { organisation_id: organisationId, space_id: spaceId },
});
try {
for await (const event of streamAgentMessage({
organisationId,
spaceId,
conversationId: conversation!.id,
body: { text: "Summarise last week’s support tickets" },
onRun: (runId) => console.log("run", runId),
})) {
if (event.event === "text_delta") process.stdout.write(event.data.text);
if (event.event === "done") console.log("\n", event.data.message.id);
}
} catch (error) {
if (error instanceof AgentStreamError) console.error(error.status, error.code);
else throw error;
}
To follow a reply from another process, or to pick it up again later, use followAgentRun with the run ID and, optionally, the last event ID you processed. cancelAgentRun stops a run; the stream then ends with an error event whose code is cancelled.
Event streams lists every event and its data.
Read and write a collection’s records
records offers a space’s collections the way window.ivy.records offers them to a page app. It needs a key with records:read, and records:write to change records. In an app’s process, the app token reaches only the collections its manifest declares, and a collection declared with access = "read" only reads.
import { records, RecordsError } from "@spacivapp/ivy-sdk";
const contacts = records({ organisationId, spaceId });
const page = await contacts.query("contacts", {
filter: { all: [{ field: "stage", op: "eq", value: "lead" }] },
limit: 50,
});
for (const contact of page.items) {
try {
await contacts.update(
"contacts",
contact.id,
{ stage: "customer" },
{ ifMatch: contact.revision },
);
} catch (error) {
if (error instanceof RecordsError && error.code === "precondition_failed") continue;
throw error;
}
}
schema, get, create, delete (with ifMatch too) and comment work the same way. A call that fails throws a RecordsError with the API’s status, code and details.