> For the complete documentation index, see [llms.txt](https://developer.opzeknd.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.opzeknd.xyz/go-contract-sdk/saving-and-reading-state.md). Agents can also search and read this documentation through its [MCP server](https://developer.opzeknd.xyz/~gitbook/mcp).

# Saving and reading state

### The contract state <a href="#the-contract-state" id="the-contract-state"></a>

Each contract has access to a sandboxed state for storage of data. The write actions on the state are rolled back in case the contract operation returns an error. If a transaction is successfully committed, it contains the hash of the state root so that any given state is committed to the blockchain.

### #Writing to the state <a href="#writing-to-the-state" id="writing-to-the-state"></a>

Use the `Set` operation on the contract context to save to the state.

```
Set(key []byte, pb proto.Message) error
```

`go-zeknd` mandates that the data being saved is a protobuf object.

### #Reading from the state <a href="#reading-from-the-state" id="reading-from-the-state"></a>

Use the `Has` operation to check whether a particular key exists in the state

```
Has(key []byte) bool
```

To read the value saved at a key, use the `Get` operation

```
Get(key []byte, pb proto.Message) error
```

Get will unmarshal the saved data into a protobuf struct.

### #Deleting a key <a href="#deleting-a-key" id="deleting-a-key"></a>

Data saved at a key can be deleted with

```
Delete(key []byte)
```
