---
docId: doc_f70639093b2b4ad9a528b64d5c72648a
title: "Agent versions"
description: "Edit and publish an agent, restore the draft or return to a previous publication, then read the history."
sidebarTitle: "Agent versions"
---

# Agent versions

Edit and publish an agent. Restore a version into the draft or return to an earlier publication, then check the changes in the history.

## Before you start

- A Platica API key in `PLATIC_API_KEY`.
- Node.js 20.3 or newer and `npm install platic-sdk`.

<span id="what-it-does"></span>

## Draft and publication changes

- Creates an agent with `v1` as its draft and published version.
- Updates the draft to `v2`. The agent keeps replying with the published version, `v1`.
- Publishes the draft. From the next message the agent answers with `v2`.
- Restores `v1` into the draft and creates `v3`. The published version remains `v2`.
- Returns to `v1` with rollback because it was previously published.
- Tries to roll back to `v3`, which was never published: a `ConflictError` with `NOT_PREVIOUSLY_PUBLISHED`, and nothing changes.
- Lists the versions and the publication history with `iterate`.

> **Important:** This example keeps the agent so you can look at its history. Delete it when you are done with `platic.agents.delete(agentId)`.

## The code

```ts
import assert from "node:assert/strict";
import { ConflictError, Platic } from "platic-sdk";

const apiKey = process.env.PLATIC_API_KEY;
if (!apiKey) {
  console.error("Set PLATIC_API_KEY to a Platica API key (pl_key_…).");
  process.exit(1);
}

const platic = new Platic({ apiKey });

// 1. Create. The agent is born published: v1 is both its draft and its publication.
const agent = await platic.agents.create({ name: `SDK example ${new Date().toISOString()}`, instructions: "You are the sales assistant." });
assert.equal(agent.publication.versionId, "v1");

// 2. Change the draft. This saves v2; production keeps answering with v1.
const saved = await platic.agents.draft.update(agent.id, { instructions: "You are the sales assistant. Answer in English.", note: "First prompt" });
assert.equal(saved.version.versionId, "v2");
assert.equal((await platic.agents.publication.get(agent.id)).publication.versionId, "v1");

// 3. Publish the draft. From the next message the agent answers with v2.
const published = await platic.agents.publish(agent.id, { note: "Go live" });
assert.equal(published.publication.versionId, "v2");

// 4. Restore v1 into the draft. It becomes v3; production still runs v2.
const restored = await platic.agents.draft.restore(agent.id, { versionId: "v1" });
assert.equal(restored.version.restoredFromVersionId, "v1");
assert.equal((await platic.agents.publication.get(agent.id)).publication.versionId, "v2");

// 5. Roll production back to v1, a version that was published before.
const rolledBack = await platic.agents.rollback(agent.id, { versionId: "v1" });
assert.equal(rolledBack.publication.versionId, "v1");

// 6. Rolling back to a version never published is a conflict you can see, and nothing changes.
try {
  await platic.agents.rollback(agent.id, { versionId: "v3" });
  assert.fail("v3 was never published");
} catch (error) {
  if (!(error instanceof ConflictError)) throw error;
  assert.equal(error.code, "NOT_PREVIOUSLY_PUBLISHED");
}

const versions: string[] = [];
for await (const version of platic.agents.versions.iterate(agent.id)) versions.push(version.versionId);
const history: string[] = [];
for await (const entry of platic.agents.publications.iterate(agent.id)) history.push(`${entry.action} ${entry.versionId}`);
assert.deepEqual(versions, ["v3", "v2", "v1"]);
assert.deepEqual(history, ["rollback v1", "publish v2", "create v1"]);

console.log(JSON.stringify({ agentId: agent.id, versions, history }));
```

## Run it

Save the code in `versions.ts` and run it with [tsx](https://tsx.is).

Set your API key and run the script:

```bash
export PLATIC_API_KEY=pl_key_...
npx tsx versions.ts
```

## What to expect

```text
{"agentId":"ag_…","versions":["v3","v2","v1"],"history":["rollback v1","publish v2","create v1"]}
```

Each step includes a check. If it fails, Node shows which one and stops the script. The agent is kept so you can inspect it in Platica. See also [Versions](/help/agents/versions) and [Publish history](/help/agents/publish-history).

## API reference used

- [Create an agent](/api-reference/agents/create)
- [Update the draft](/api-reference/agents/drafts/update)
- [Publish an agent](/api-reference/agents/publications/publish)
- [Restore a version into the draft](/api-reference/agents/drafts/restore)
- [Roll back a publication](/api-reference/agents/publications/rollback)
- [List versions](/api-reference/agents/versions/list)
- [List publications](/api-reference/agents/publications/list)

## Next steps

- [All SDK examples](/sdk/examples/quickstart): the next one is [Quickstart: create an agent and talk to it](/sdk/examples/quickstart).

- [Errors](/sdk/errors)
