> For the complete documentation index, see [llms.txt](https://tarmac.gitbook.io/tarmac-framework/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tarmac.gitbook.io/tarmac-framework/capabilities/key-value-store.md).

# Key:Value Datastore

The Key:Value Store capability provides WASM function developers the ability to store and retrieve data from Key:Value datastores. At the moment, Tarmac supports multiple Key:Value stores which can be enabled/disabled in the host configuration settings.

## Get

The Get function provides users with the ability to fetch data using the specified key. To avoid conflicts, the `data` key within the response, JSON, will be base64 encoded.

```golang
_, err := wapc.HostCall("tarmac", "kvstore", "get", KVStoreGetJSON)
```

### Interface Details

| Namespace | Capability | Function | Input        | Output               |
| --------- | ---------- | -------- | ------------ | -------------------- |
| `tarmac`  | `kvstore`  | `get`    | `KVStoreGet` | `KVStoreGetResponse` |

### Example JSON

This callback uses JSON messages as input and output to facilitate communications between WASM functions and the Tarmac host.

#### KVStoreGet

```json
{
	"key": "myKey"
}
```

#### KVStoreGetResponse

```json
{
	"data": "VHdlZXQgYWJvdXQgVGFybWFjIGlmIHlvdSB0aGluayBpdCdzIGF3ZXNvbWUu",
	"status": {
		"code": 200,
		"status": "OK"
	}
}
```

The Status structure within the response JSON denotes the success of the database call. The status code value follows the HTTP status code standards, with anything higher than 399 is an error.

## Set

The Set function provides users with the ability to store data within the Key:Value datastore. The `data` key within the request JSON must be base64 encoded.

```golang
_, err := wapc.HostCall("tarmac", "kvstore", "set", KVStoreSetJSON)
```

### Interface Details

| Namespace | Capability | Function | Input        | Output               |
| --------- | ---------- | -------- | ------------ | -------------------- |
| `tarmac`  | `kvstore`  | `set`    | `KVStoreSet` | `KVStoreSetResponse` |

### Example JSON

This callback uses JSON messages as input and output to facilitate communications between WASM functions and the Tarmac host.

#### KVStoreSet

```json
{
  "data": "VHdlZXQgYWJvdXQgVGFybWFjIGlmIHlvdSB0aGluayBpdCdzIGF3ZXNvbWUu",
  "key": "myKey"
}
```

#### KVStoreSetResponse

```json
{
  "status": {
    "code": 200,
    "status": "OK"
  }
}
```

The Status structure within the response JSON denotes the success of the database call. The status code value follows the HTTP status code standards, with anything higher than 399 is an error.

## Delete

The Delete function provides users with the ability to delete data stored within the Key:Value datastore.

```golang
_, err := wapc.HostCall("tarmac", "kvstore", "delete", KVStoreDeleteJSON)
```

### Interface Details

| Namespace | Capability | Function | Input           | Output                  |
| --------- | ---------- | -------- | --------------- | ----------------------- |
| `tarmac`  | `kvstore`  | `delete` | `KVStoreDelete` | `KVStoreDeleteResponse` |

### Example JSON

This callback uses JSON messages as input and output to facilitate communications between WASM functions and the Tarmac host.

#### KVStoreDelete

```json
{
  "key": "myKey"
}
```

#### KVStoreDeleteResponse

```json
{
  "status": {
    "code": 200,
    "status": "OK"
  }
}
```

The Status structure within the response JSON denotes the success of the database call. The status code value follows the HTTP status code standards, with anything higher than 399 is an error.

## Keys

The Keys function provides users with the ability to fetch a list of all keys available within the Key:Value datastore.

```golang
_, err := wapc.HostCall("tarmac", "kvstore", "keys", []byte())
```

Note: This callback requires no input JSON. However, the callback function will require users to provide a byte slice.

### Interface Details

| Namespace | Capability | Function | Input            | Output                |
| --------- | ---------- | -------- | ---------------- | --------------------- |
| `tarmac`  | `kvstore`  | `keys`   | `EmptyByteSlice` | `KVStoreKeysResponse` |

### Example JSON

This callback uses JSON messages as input and output to facilitate communications between WASM functions and the Tarmac host.

#### KVStoreKeysResponse

```json
{
	"keys": ["key1", "key2"],
	"status": {
		"code": 200,
		"status": "OK"
	}
}
```

The Status structure within the response JSON denotes the success of the database call. The status code value follows the HTTP status code standards, with anything higher than 399 is an error.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://tarmac.gitbook.io/tarmac-framework/capabilities/key-value-store.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
