# Deployments

> Register your service's URL, choose the transport, and ship new versions without breaking runs in flight.

Source: https://cairnflow.dev/docs/cloud/deployments

:::caution[Early-access waitlist]
Cairnflow Cloud is not open yet — [join the waitlist](/#pricing) to hear when early access opens.
This page describes the hosted service as it is built for its Beta, and details may change before
it opens. To run cairnflow today, [host it yourself](/docs/self-hosting).
:::

A **deployment** is one registered endpoint of your service: a URL where cairnflow can reach the
handlers of one version of your code. Registering reads the endpoint's list of services and
handlers and makes it the current version of each service it contains.

The same API applies to a self-hosted cluster; the rules about `https://`, identity keys and the
default transport are those of Cairnflow Cloud.

## Register

```sh
curl -X POST "$CAIRN_URL/deployments" \
  -H "authorization: Bearer $CAIRN_KEY" \
  -H "content-type: application/json" \
  -d '{"uri":"https://your-service.example.com"}'
```

You need an **Admin** key. The answer describes the new deployment: its ID (`dp_…`) and
`protocolMode`, which says how the environment will call your service.

Each registration creates a new, immutable deployment, even for the same URL. Handler options
such as the retry policy and timeouts are read at registration, so changing them in code means
registering again.

### What Cloud checks

| Requirement | If not met |
|---|---|
| The URI is `https://` on the public internet | Refused: private and internal addresses are not allowed |
| The service checks identity keys (`identityKeys` in `serve()` or `fetchHandler()`) | `400` with code `identity_required` |
| The SDK compresses request-response bodies (the TypeScript SDK does by default; do not set `compression: false`) | `400` with code `compression_required` |
| The SDK and the runtime share a protocol version | Refused at registration |
| At most 10 registrations per minute | `429` with code `deploy_rate_limited` and a `Retry-After` header |

## Transport

Cairnflow calls your service in one of two modes:

- **`REQUEST_RESPONSE`.** Each turn of a handler is one HTTP request. When the handler waits (a
  sleep, a call, a promise) the request ends, and the next turn sends the journal recorded so far.
  A turn must finish within the [turn time limit](/docs/cloud/limits#turn-time-limit), and tokens
  and stream records of a step reach readers when the step finishes. Works behind every host and
  on serverless functions.
- **`BIDI_STREAM`.** One HTTP/2 stream stays open for the whole invocation, and the SDK keeps it
  alive while a step runs. Needed for live token streaming and for cancelling a running step
  through its `AbortSignal`. `serve()` declares this mode.

On Cloud, an `https://` endpoint is registered as **`REQUEST_RESPONSE`** by default, even if your
service declares `BIDI_STREAM`. Most hosts end TLS at their edge and talk HTTP/1.1 to your app, or
buffer the request body, and a streaming invocation would hang there.

If your host forwards HTTP/2 all the way to your app (Fly.io with `h2_backend` and Cloud Run with
`--use-http2` are meant to; streaming has not been verified on Fly.io yet), keep the mode your
service declares:

```sh
curl -X POST "$CAIRN_URL/deployments" \
  -H "authorization: Bearer $CAIRN_KEY" \
  -H "content-type: application/json" \
  -d '{"uri":"https://your-service.example.com","transport":"declared"}'
```

Check `protocolMode` in the answer. If streaming does not reach your app, invocations fail with a
message starting with `bidi_no_response_headers` (in the invocation's last failure) and are
retried. Register the endpoint again without `transport`.

`fetchHandler` always declares `REQUEST_RESPONSE`: a Fetch API handler cannot hold a
bidirectional stream.

### Keeping request-response cheap

In `REQUEST_RESPONSE` the journal travels to your service on every turn, so bytes grow with the
number of steps. Two SDK features reduce that:

- **Compression.** Request and response bodies are gzipped (on by default). Each journal entry is
  compressed on its own, so bytes of one entry are never compressed together with another.
- **Replay cache.** `serve()` keeps the journal it has already received for each running
  invocation in memory (64 MiB, 15 minutes by default), and the runtime then sends only the new
  entries plus a digest of the rest. If the cache misses, your service answers `409` and the turn
  is sent again in full at once, so it is always safe. `serve({ replayCache: false })` turns it
  off; `fetchHandler` has it off by default, because Lambda, Workers and edge functions do not keep
  a process between turns.

With several replicas behind a load balancer, the cache only hits when turns of one invocation
reach the same replica. Cairnflow sends back cookies that your previous response set for the same
invocation, so sticky-session cookies work; on Fly.io the SDK asks for the same machine by itself.

Keep step results small either way: large values belong in [artifacts](/docs/develop/artifacts),
not in the journal, and the [payload limit](/docs/cloud/limits) applies.

## Platforms

| Platform | Serve with | Notes |
|---|---|---|
| Container, VM (Node.js 22.6 or newer) | `serve()` | Long-running process; replay cache on. Listen on `hostname: "0.0.0.0"` and the port your host gives you |
| Vercel (Next.js App Router) | `fetchHandler` in `app/api/cairn/[...path]/route.ts`, exported as `GET` and `POST` | Node.js runtime. Register a production or named domain: preview Deployment Protection answers with a login page |
| Cloudflare Workers | `export default { fetch: handler }` | Set `compatibility_flags = ["nodejs_compat"]` |
| AWS Lambda (Function URL or API Gateway HTTP API, payload v2) | `export const handler = lambdaHandler(fetchHandler({ … }))` | `identityKeys` is what keeps strangers out, so a Function URL with auth type `NONE` is fine |
| Deno, Bun | `Deno.serve(handler)`, `Bun.serve({ fetch: handler })` | |

The Fetch adapter is tested on Node.js, not yet on these platforms themselves.

Register the **base URL** where the handler is mounted, for example
`https://your-app.vercel.app/api/cairn`. With a function, one turn must finish within the
platform's time limit: split long work into several short `ctx.run` steps. See
[Serverless](/docs/workflows/serverless).

## Ship a new version

Every invocation stays on the deployment it started on, for its whole life, even after you
register a new version. New invocations go to the current version. So:

1. **Keep the old endpoint running** until nothing is left on it.
2. Register the new version. It becomes current for the services it contains.
3. Watch the old deployment drain: `GET /deployments/{id}` reports `pinnedInvocations`, the number
   of unfinished invocations still on it.
4. Remove it when it is empty: `DELETE /deployments/{id}`. It answers `409` while the deployment
   is still a current version or still has invocations on it. While it is being removed, moving a
   service to it, migrating to it or restarting on it answers `409` too.

For a careful rollout, register without moving anything with `POST /deployments?activate=false`,
then move each service yourself:

```sh
curl -X PUT "$CAIRN_URL/services/Chat/revision" \
  -H "authorization: Bearer $CAIRN_KEY" \
  -H "content-type: application/json" \
  -d '{"deployment":"dp_new…","expect":"dp_old…"}'
```

`expect` makes the move happen only if the service still points at that deployment: when two
people roll back at once, exactly one wins. The same call with an older deployment is a rollback.

The console's **Deployments → Deploy safely** page walks through these steps: register without
moving, compare handlers, replay-check up to 20 unfinished invocations per service on the new
version (read-only, nothing is committed), move, drain, migrate the invocations that replay
cleanly, roll back, and remove the old version. See [Versioning](/docs/develop/versioning) for
what makes a code change safe for invocations in flight.

## List and inspect

| Request | Returns |
|---|---|
| `GET /deployments` | Every deployment: `id`, `endpoint`, `createdAtMs`, `protocolMode`, `currentFor` (services it is the current version of) |
| `GET /deployments/{id}` | The same, plus `pinnedInvocations` and the manifest: services, handlers and their options as registered |
| `GET /services` | Services and handlers you can call, with the deployment that serves each |

`protocolMode` is the first thing to check when every invocation of a deployment fails in
transport.

## Next

- [Limits](/docs/cloud/limits)
- [The console](/docs/cloud/console)
- [HTTP API reference](/docs/reference/http-api)
