Write
Persist versioned key/value data with full history.
Writing data in Litebase means storing a value under a key — durably, immutably, and with version control. Every write creates a new version, tied to a unique transaction and timestamp, so you can always trace changes over time.
This guide walks through how to structure keys, store values, and leverage versioned writes as part of your application logic.
1. Choose a key
Keys are strings used to organize and retrieve data. They follow a flat but hierarchical structure — typically dot-separated to group related records:
user.abc123.profileproject.42.statusorder.20250405.receipt
Keys must be unique. Writing to the same key creates a new version — previous values are preserved.
2. Prepare your value
Values can be any valid JSON-serializable object:
- Flat key-value maps
- Nested objects
- Arrays
- Numbers, strings, or booleans
Avoid excessively large values (e.g. >1MB) unless necessary — all writes are versioned and retained.
Example:
3. Write to the key
Send an HTTP PUT request to the key’s endpoint:
The response will include:
key: the key nametime: the commit timestamptx: the transaction IDversion: the version number of this write
Example:
4. Write multiple keys (batch)
You can write multiple keys in a single transaction to ensure consistency:
All writes in a batch are applied under the same transaction.
5. (Optional) Publish events
You can optionally emit an event alongside your write to keep state and stream timelines in sync:
This ensures strong consistency between your stored data and your event log — perfect for reactive systems.