Write Data
Overview
At the core, prefsdb.com can write and store anything from simple key/value pairs, to large data blobs, or serialize and deserialize entire objects.
All functions take the data and an optional options object.
Syntax:
.write( data, [options])
.setItem( key, value, [options])
.putObject( file, [options])
Use .write() (or .setItem()) to store any value: text, a number, or an object. A single value can be up to 1 MB; for anything bigger, upload a file with .putObject(). A larger value gets an error (VALUE_TOO_LARGE) instead of being stored. Key names can be up to 240 characters, and can contain any characters, emoji included. They match regardless of upper or lower case: Theme and theme are the same key.
.putObject() uploads a file, and .getObject() downloads it again (see below).
Parameters
| parameter | type | description |
|---|---|---|
| data | text | number | object | Data to be stored, up to 1 MB (4 MB on Plus, 8 MB on Pro). It's stored exactly as given, including characters like +, % and &, any language, and emoji. Objects are stored as JSON and come back as objects when read. |
| options | { } | Optional object containing overrides, such as set your own request timeout, onsuccess and onerror handlers. Every function returns a Promise; the handlers are an alternative to awaiting it. See Options. |
.write()
Stores a value under the current key, replacing what was there, or adds it to the current list. The value is sent in the request body, so it doesn't show up in web addresses or server logs.
.setItem()
Convenience alias method for familiarity with local Storage in a browser.
Usage
It is equivalent to
.putObject()
Uploads a file (a File from an <input type="file">, or any Blob). The file is encrypted at rest; the response's file field is the id you pass to .getObject() or .deleteObject() later.
Usage:
To store an object as data rather than as a file, use .write(object) or .setItem("myObj", object).
Examples
Key/value Example
First, include the PrefsDB JavaScript library in your project:
Then get your API key (see Getting started).
IMPORTANT!
Make sure to use the same secrets in the seed array. If you change seed parameters or even reorder them within an array - it will result in a new key being generated and existing data under the old key may be lost.
You are now ready to store and retrieve data.
For instance, to store a value identified by a key named foobar
or, one key per value:
Over plain HTTP
Short values can go in the address as key=value, as above, which also lets you write several keys at once. Send anything longer, or anything you'd rather keep out of server logs, as the request body (key= in the address, the value as the body), as the JavaScript client does.
Object serialization
The sample code below serializes (saves) an object instance as "myObj".
Lists
Lists are un-ordered collections of data. Prefsdb.com makes it easy and effortless to collect data in a list.
Files
Storage space
Each key can store files up to a limit set by its plan. getkey(), every upload and every file delete report how much of it is used, so your app can warn its users before uploads start failing. You decide when to warn and what to say.
Users can free up space by deleting files, or upgrade from the page their review link opens (db.reviewUrl()).
Response
Generally a response will contain a an indicator of how many fields were changed. Field updated show how many values were updated - should be 1 or 2. 2 is not an error, it is a correct number indicating internal update path (has to do with internal key constraints being triggered).
version (when one key or map entry was written) is the value's new version. Pass it back as { ifVersion } to change the value again only if nobody else has in the meantime: see Several Devices.
When no data was written, either the data has not changed or attempt was made to write an empty value.
{
"success": true,
"status": "success",
"updated": 0,
"message": "No data written or value not changed.",
"ts": "2026-03-26 21:14:07"
}
Organizing stored data.
Any piece of data can be additionally categorized and stored under a named project, domain, or subdomain.
Coming soon
A Java client is planned. Until it's available, call the HTTP API directly from any HTTP client — see the cURL tab.
Coming soon
A Python client is planned. Until it's available, call the HTTP API directly (e.g. with requests) — see the cURL tab.
The piece of data saved above can be retrieved using the sample code below.
Handling Errors
If the server refuses the write, the Promise resolves with success: false, an error code and a message; it rejects only when the request got no answer at all, for example a timeout. With the throwOnError: true option, a refusal rejects too. See How the JavaScript client reports errors.
A write made while offline resolves with queued: true: it's saved in the browser and sent later, but isn't on the server yet. See Offline and Optimistic Writes.
Write success
A successful write operation will return the following response:
Write failure
A failed operation will have its sucess status set to false and offer more details about the error with error code and an error message.