> ## Documentation Index
> Fetch the complete documentation index at: https://s2.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Protocol

> S2 v1 HTTP API for clients, including authentication, compression, and data plane protocol details.

Clients interact with S2 at its public [`https` endpoints](https://s2.dev/docs/api/endpoints), primarily with JSON bodies conforming to its [OpenAPI spec](https://github.com/s2-streamstore/s2-specs/blob/main/s2/v1/openapi.json).

> [!TIP]
>
> Prefer to use an available [SDK](https://s2.dev/docs/sdk).

## Authentication

The API requires a `Bearer` [access token](https://s2.dev/docs/concepts/access-tokens.md) to authenticate requests.

S2 SDKs take care of supplying it automatically. If you are using `curl`, you can provide it with `-H "Authorization: Bearer ${S2_ACCESS_TOKEN}"`.

> [!NOTE]
>
> The first usage of an access token on a connection may experience a latency overhead for verification, typically tens of milliseconds. Subsequent requests will not incur such an overhead.

## Compression

To optimize network usage, S2 supports compressed request and response bodies with the following algorithms:

* `zstd` *preferred*
* `gzip`

Standard HTTP semantics around `Content-Encoding` and `Accept-Encoding` apply.

## Data Plane

There are 3 core data operations on a [stream](https://s2.dev/docs/concepts/streams.md):

> [card] [Append records](https://s2.dev/docs/api/records/append.md)
>
> `POST /streams/{stream}/records`

> [card] [Read records](https://s2.dev/docs/api/records/read.md)
>
> `GET /streams/{stream}/records?seq_num=42&count=100`

> [card] [Check the tail](https://s2.dev/docs/api/records/check-tail.md)
>
> `GET /streams/{stream}/records/tail`

### Data in JSON

The following pieces of record data are stored as bytes:

* Header name
* Header value
* Body

The header `s2-format` is used to indicate the desired encoding of these bytes when records are represented in JSON.

> [card] raw
>
> `s2-format: raw` or omit header.
>
> *Use when your record data is valid Unicode.*
>
> &#x20;Zero overhead, human-readable.
>
> &#x20;Cannot handle binary data safely.

> [card] base64
>
> `s2-format: base64`
>
> *Use when you are working with arbitrary bytes.*
>
> &#x20;Always safe.
>
> &#x20;33% overhead over the wire.

> [!WARNING]
>
> You can write using one format and read with another. When reading `raw`, S2 is interpreting the stored bytes as UTF-8. This will be a potentially-lossy conversion if it was not also written as `raw`, or as `base64`-encoded valid UTF-8.

### Protobuf messages

Data plane endpoints to [`append`](https://s2.dev/docs/api/records/append) and [`read`](https://s2.dev/docs/api/records/read) records also support [protobuf](https://protobuf.dev) bodies. This helps avoid the base64 encoding tax compared to binary data in JSON messages.

To send and receive protobuf:

* Set the `Content-Type` header to `application/protobuf` and send a protobuf-encoded payload.
* Set the `Accept` header to `application/protobuf` to receive a protobuf-encoded response. The response will include the `Content-Type: application/protobuf` header if the server returns a protobuf.

Type definitions are available in [ git](https://github.com/s2-streamstore/s2-specs/blob/main/s2/v1/s2.proto) and [Buf](https://buf.build/streamstore/s2/docs/main:s2.v1).

> [card] Append
>
> &#x20;[`POST ../{stream}/records`](https://s2.dev/docs/api/records/append)
>
> &#x20;[`AppendInput`](https://buf.build/streamstore/s2/docs/main:s2.v1#s2.v1.AppendInput)
>
> &#x20;[`AppendAck`](https://buf.build/streamstore/s2/docs/main:s2.v1#s2.v1.AppendAck)

> [card] Read
>
> &#x20;[`GET ../{stream}/records?..`](https://s2.dev/docs/api/records/read)
>
> &#x20;[`ReadBatch`](https://buf.build/streamstore/s2/docs/main:s2.v1#s2.v1.ReadBatch)

> [!NOTE]
>
> Sending the `Accept: application/protobuf` request header only guarantees a protobuf response in case of a success (HTTP 200). Other status codes are always accompanied by JSON bodies.

### Sessions

S2S (`S2-Session`) is a minimal binary protocol used by S2 SDKs to encapsulate streaming `append` and `read` session semantics over HTTP/2 (HTTP/3 in future, too!), at the same endpoints as "unary" calls.

It is meant for S2 SDKs, but documented for the adventurous.

> [accordion] S2S spec
>
> #### Setup
>
> * `Content-Type: s2s/proto` signals that a session is being requested.
> * `Accept-Encoding` signals which compression algorithms are supported (service supports `zstd` and `gzip`). `Content-Encoding` is not sent as message-level compression is used.
> * `200 OK` response establishes a session.
>
> #### Message framing
>
> *All integers use big-endian byte order. Messages smaller than 1KiB should not be compressed.*
>
> **Length prefix** (3 bytes): Total message length (flag + body), at least 1 and at most 2.5 MiB
>
> **Flag byte** (1 byte): `[T][CC][A][RRRR]`
>
> * `T` (bit 7): Terminal flag (`1` = stream ends after this message)
> * `CC` (bits 6-5): Compression (`00`=`none`, `01`=`zstd`, `10`=`gzip`)
> * `A` (bit 4): Reconnect advised (`1` = server suggests reconnecting, only on messages from *Server → Client*)
> * `RRRR` (bits 3-0): Reserved (ignored on receive)
>
> **Body** (variable):
>
> * Regular message is a Protobuf
> * Terminal message contains a 2-byte non-2xx HTTP status code (`300`–`599`), followed by JSON error information (corresponding to unary response behavior); any other status code is a protocol error
>
> A message's body decompresses to at most 2.5 MiB − 1 bytes, enough for any valid batch. Either side treats a message past these limits as a protocol error. A terminal message is never compressed and never carries reconnect advice (its non-terminal flag bits are ignored on receive).
>
> #### Data flow
>
> **Append** sessions are a bi-directional stream of [`AppendInput`](https://buf.build/streamstore/s2/docs/main:s2.v1#s2.v1.AppendInput) messages from *Client → Server*, and [`AppendAck`](https://buf.build/streamstore/s2/docs/main:s2.v1#s2.v1.AppendAck) messages from *Server → Client*. The response ends cleanly only once every input the server received is acknowledged; otherwise it ends with a terminal message. Before emitting any terminal error classified with `side_effects: "none"` in [`error-codes.json`](error-codes.json) (including `server_draining`, `hot_server`, `412`, and mid-session input rejections), the server emits acknowledgements for every earlier accepted input that became durable and guarantees that no remaining unacknowledged input can become durable.
>
> **Read** sessions are a uni-directional stream of [`ReadBatch`](https://buf.build/streamstore/s2/docs/main:s2.v1#s2.v1.ReadBatch) messages from *Server → Client*. A heartbeat is an empty batch carrying the tail, sent only when the server has nothing earlier to send; while waiting for new records, one is sent at least every 15 seconds. The response ends cleanly only when the read's limits, `until`, or `wait` end it.
>
> #### Reconnect advice
>
> A server that is about to terminate sets the reconnect-advised flag on regular messages.
>
> * **Read** sessions should be re-established with a fresh request. Read sessions still attached when the server drains are ended by the server with a terminal `503` with error code `server_draining`.
> * **Append** sessions should stop sending inputs and half-close the request stream. The server acknowledges all accepted inputs and then ends the session cleanly. A new session can be established concurrently, and should start being used once all acknowledgements for in-flight appends have been received.
> * Append sessions still attached when the server drains are ended by the server: it stops reading inputs, acknowledges accepted inputs, and sends a terminal `503` with error code `server_draining`. Acknowledgements always precede the terminal message, so an input is either acknowledged or was never processed, and unresolved inputs can be safely resubmitted on a new session.
> * Under severe load shedding (such as when the frontend cannot reserve initial session memory), the server may also end a session with a terminal `502` and error code `hot_server` and close the connection; as with other `side_effects: "none"` terminal errors, no unacknowledged input became durable, and clients should retire the connection and reconnect on a new connection with backoff.

> [!TIP]
>
> Read sessions are also supported over [SSE](https://s2.dev/docs/api/records/read.md#sse) which has the benefit of being usable in the browser context.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.