# Sequence versions and publishing (https://docs.opensequence.dev/docs/concepts/sequence/versions)

How drafts, the optimistic lock, publishing, new versions and archiving work, and what enrolled contacts do when a new version ships.

<CodeGroup>

```bash title="curl"
curl -X POST $SEQ/v1/sequences -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" -d '{"name":"UK intro","description":"First touch for UK facilities managers"}'
# {"sequence":{"id":"seq_…","status":"active","defaultVersionId":null},"version":{"id":"ver_…","tag":"v1","status":"draft","lockVersion":0}}
curl -X PUT  $SEQ/v1/sequences/seq_…/versions/ver_…/draft   -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" -d '{"definition":{…},"lock_version":0}'
curl -X POST $SEQ/v1/sequences/seq_…/versions/ver_…/publish -H "Authorization: Bearer $KEY" -H "Content-Type: application/json"
curl -X POST $SEQ/v1/sequences/seq_…/versions -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" -d '{"from_version":"ver_…","tag":"v2"}'
curl -X POST $SEQ/v1/sequences/seq_…/versions/ver_…/archive -H "Authorization: Bearer $KEY"
```

```ts title="TypeScript"
const { sequence, version } = await oseq.sequences.create({ name: 'UK intro' });
const saved = await oseq.sequences.versions.saveDraft(sequence.id, version.id, { definition, lock_version: version.lockVersion });
await oseq.sequences.versions.publish(sequence.id, version.id);
const v2 = await oseq.sequences.versions.create(sequence.id, { from_version: version.id, tag: 'v2' });
await oseq.sequences.versions.archive(sequence.id, version.id);
```

</CodeGroup>

## Lifecycle [#lifecycle]

```text
POST /v1/sequences ──► v1 draft ──PUT …/draft (edit)──► ──POST …/publish──► published (immutable, the default) ──POST …/archive──► archived
                                                                                   │
                                                              POST /v1/sequences/{id}/versions {from_version, tag} ──► v2 draft …
```

| Status | Meaning |
|---|---|
| `draft` | editable with `PUT .../draft`; deletable with `DELETE .../versions/{vid}`; cannot enroll |
| `published` | frozen; the sequence's default version for new enrollments; cannot be edited (`409 VERSION_NOT_EDITABLE`) |
| `archived` | no new enrollments into it; people already in it finish on it |

Creating a sequence makes `v1` as a draft; pass `definition` in the create body to start from your own document instead of the empty `start → end`. `external_id` lets you address the sequence by your own id, and `labels` is free JSON for your UI.

## Saving a draft [#saving-a-draft]

`PUT /v1/sequences/{id}/versions/{vid}/draft` takes `{ definition, lock_version }`. The version's `lockVersion` increments on every save; sending a stale one is `409 STALE_LOCK` ("someone else saved this draft after you loaded it"), so two editors cannot overwrite each other. Saving accepts a definition with **errors**: they come back as `problems` so a canvas can show them while the user works. Only publishing refuses them.

## Publishing [#publishing]

`POST .../publish` validates (errors block with `422 INVALID_DEFINITION` and `details.problems`; warnings pass and are returned), freezes the definition, records `published_by` (the body's `published_by`, else the key id) and makes the version the sequence's default. Publishing a published version is a no-op that returns it.

## A new version [#a-new-version]

`POST /v1/sequences/{id}/versions` with `{ from_version?, tag }` copies any version (default: the sequence's default, else the latest) into a new draft. Tags are unique per sequence (`409 TAG_TAKEN`), up to 24 letters, digits, dots and dashes (`BAD_TAG`).

## What enrolled contacts do when a new version ships [#what-enrolled-contacts-do-when-a-new-version-ships]

Nothing changes for them: an enrollment is pinned to its version id, the definition is immutable, and the journey shows the definition the contact actually ran. New enrollments (and `POST .../enrollments` without an explicit `version`) use the new default. To move people, exit them (`POST /v1/enrollments:exit { sequence, status: "active" }`) and enroll them again; they start the new version from its entry node, and the re-enrollment rule (`settings.reenroll`) applies, so `never` will skip them with `already_enrolled_before`.

## Pausing and archiving a sequence [#pausing-and-archiving-a-sequence]

`PATCH /v1/sequences/{id}` with `status: "paused"` stops planning for every version (enrollments keep their state and resume when the sequence is active again); `status: "archived"` is refused with `409 HAS_ACTIVE_ENROLLMENTS` while contacts are in it, unless `exit_active: true`, which exits them with reason `manual` and the label `sequence archived`. The same `PATCH` renames (`name`), describes (`description`) and relabels (`labels`).

## Reading [#reading]

`GET /v1/sequences` lists sequences, each with its version summaries and its enrollment counts by status. The sequence itself is a stored record (camelCase); `versions` and `counts` are built for the list (snake_case). One row:

```json
{
  "id": "seq_01J9…", "projectId": "prj_01J9…", "externalId": null, "name": "UK intro", "description": null, "createdBy": null, "labels": {},
  "status": "active", "defaultVersionId": "ver_01J9…", "createdAt": "…", "updatedAt": "…",
  "versions": [
    { "id": "ver_01J9…", "tag": "v1", "status": "published", "published_at": "2026-10-13T09:00:00.000Z", "created_at": "…" },
    { "id": "ver_01JA…", "tag": "v2", "status": "draft", "published_at": null, "created_at": "…" }
  ],
  "counts": { "active": 142, "paused": 3, "waiting_limit": 0, "completed": 57, "exited": 88, "failed": 0 }
}
```

`counts` is every enrollment of the sequence, all versions together, by its current status; every key is present, `0` when empty. For a list screen: running is `active + paused + waiting_limit`, finished is `completed + exited`. Replies and meetings are not in this list: read them from [`GET /v1/analytics/sequences`](/docs/analytics/endpoints#cross-sequence) (one call for every sequence) or from a sequence's `analytics/summary`.

`GET /v1/sequences/{id}` returns the same record with `versions` and without `counts`. `GET /v1/sequences/{id}/versions` returns the full version records. `GET /v1/sequences/{id}/versions/{vid}` returns the version with its current validation problems; the definition is inside `version`:

```json
{
  "version": { "id": "ver_01J9…", "sequenceId": "seq_01J9…", "tag": "v1", "status": "draft", "lockVersion": 4, "schemaVersion": 1,
               "definition": { "schema_version": 1, "entry": "start", "nodes": { … }, "...": "..." },
               "basedOnVersionId": null, "createdBy": null, "publishedBy": null, "publishedAt": null, "archivedAt": null, "createdAt": "…", "updatedAt": "…" },
  "problems": [ { "path": "nodes.email_intro.params.subject", "code": "TEMPLATE_UNKNOWN_FIELD", "message": "…", "severity": "warning" } ]
}
```

`PUT .../draft`, `POST .../publish` and `POST /v1/sequences` return this same `{ version, problems }` pair (the create adds `sequence`).
