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

# setState()

> `protected` **setState**(`patch`): `Promise`\<`State`>

Merge `patch` into the persistent state and return the merged result.

The merge is a recursive JSON merge patch (RFC 7396): top-level keys in
`patch` are merged in, nested objects are deep-merged, and a `null` value
deletes that key. The merge runs in a storage transaction, and
[`onStateChanged`](/docs/agent-sdk/interface/onStateChanged) fires after it commits.
The committed patch is also pushed to connected clients subscribed to the
`state` stream — no broadcast call needed.

Values must be storage-codec-safe (JSON-native values plus `Date`, `Map`,
`Set`, `ArrayBuffer`/TypedArray, `BigInt`, `RegExp`); functions, class
instances, or circular references throw a `CodecError`.

**Parameters**

| Parameter | Type | Description |
| - | - | - |
| `patch` | `MergePatch`\<`State`> | The fields to merge; `null` deletes a key (RFC 7396). |

**Returns**

`Promise`\<`State`>

The full state after the merge.
