Skip to main content

Accessing data

Initializing your PrivyClient instance with either privy-browser or privy-nodeโ€‹

In order to interact with your data within Privy, you must create an instance of PrivyClient and integrate your auth system with Privy. Follow these steps to do so if you haven't already!

Writing dataโ€‹

While fields have to be explicitly set up with appropriate access control, there is no need to explicitly create new users. This happens automatically when you put() data for a new user id.

//client is an instance of PrivyClient

// Single field
// this can be the first mention of user id 0x123
const email = await client.put(userId, 'email', 'foo@example.com');

// Multiple fields. Resulting list is ordered according to input list.
const [firstName, lastName] = await client.put(userId, [
{field: 'first-name', value: 'Jane'},
{field: 'last-name', value: 'Doe'},
]);

Reading dataโ€‹

// NOTE: When reading field values, resulting objects may
// be null if no value exists for that user and field yet.

// Single field
const email = await client.get(userId, 'email');

// Multiple fields. Resulting list is ordered according to input list.
const [firstName, lastName, avatarUrl] = await client.get(userId, [
'first-name',
'last-name',
'avatar-url',
]);

console.log(email !== null ? email.text() : 'N/A'); // foo@example.com
console.log(firstName !== null ? firstName.text() : 'N/A'); // Jane
console.log(lastName !== null ? lastName.text() : 'N/A'); // Doe
console.log(avatarUrl !== null ? avatarUrl.text() : 'N/A'); // N/A

Deleting dataโ€‹

const [email, firstName, lastName] = await client.put(userId, [
{field: 'email', value: 'foo@example.com'},
{field: 'first-name', value: 'Jane'},
{field: 'last-name', value: 'Doe'},
]);

// Delete single field
await client.del(userId, 'email');
const email = await client.get(userId, 'email');
console.log(email); // null

// NOTE: Deleting a field that has never been set or is already deleted will
// not throw an error and will return nothing as with a successful deletion.
const res = await client.del(userId, 'email');
console.log(res); // undefined

// Delete multiple fields
await client.del(userId, ['first-name', 'last-name']);
const [firstName, lastName] = await client.get(userId, ['first-name', 'last-name']);
console.log(firstName, lastName); // null, null

Writing filesโ€‹

Privy supports unstructured data handling (useful for e.g. videos, images, etc). You can access them as follows.

const onUpdateAvatar = async (avatar: File) => {
try {
await client.putFile(userId, 'avatar', avatar);
} catch (error) {
console.log(error);
}
};

Reading filesโ€‹

const avatar = await client.getFile(userId, 'avatar');

if (avatar !== null) {
download(avatar.integrity_hash, avatar.blob());
}

function download(uniqueId: string, blob: Blob) {
const data = window.URL.createObjectURL(blob);

// Lookup extension by mime type (included on blob)
const ext = getExtensionFromMIMEType(blob.type);
const filename = `${uniqueId}.${ext}`;

// Create a link pointing to the ObjectURL containing the blob.
const link = document.createElement('a');
link.style = 'display: none;';
link.href = data;
link.download = filename;
link.click();

// Cleanup
window.URL.revokeObjectURL(data);
link.remove();
}
info

If you want to learn about retrieving a single data cell by a unique identifier, check out integrity hashes.

For full documentation, please see the Privy API references.