Persistent State
Key-value storage is optional and lives in your server code, not in
@farcaster/snap itself.
For coding agents: use @farcaster/snap-turso. It exports a DataStore with:
await store.get(key)— returns a JSON-serializable value ornullif the key is missing.await store.set(key, value)— writes the value (overwrites an existing key).
Create a single store when the process starts (module scope), then close over it inside
your SnapFunction.
import type { SnapFunction } from "@farcaster/snap";
import { createTursoDataStore } from "@farcaster/snap-turso";
const store = createTursoDataStore();
const snap: SnapFunction = async (ctx) => {
const visits = ((await store.get("visits")) as number) ?? 0;
await store.set("visits", visits + 1);
return {
version: "1.0",
ui: {
root: "page",
elements: {
page: { type: "stack", props: {}, children: ["count"] },
count: {
type: "text",
props: { content: `Visits: ${visits + 1}` },
},
},
},
};
};
createTursoDataStore() uses an in-memory map for local development and testing.
Full wiring example lives in the repo template — start from
template/src/index.ts.