Skip to main content

Class: PrivyClient

The Privy client performs operations against the Privy API.

import {PrivyClient} from '@privy-io/privy-browser';

Constructors​

constructor​

β€’ new PrivyClient(options)

Creates a new Privy client.

Parameters​

NameTypeDescription
optionsObjectInitialization options.
options.apiURL?stringThe URL of the Privy API. Defaults to https://api.privy.io/v0.
options.kmsURL?stringThe URL of the Privy KMS. Defaults to https://kms.privy.io/v0.
options.sessionSessionAn object that implements the Session interface.
options.timeout?numberTime in milliseconds after which to timeout requests to the API and KMS. Defaults to 10000 (10 seconds).

Defined in​

privy-js/packages/privy-browser/src/client.ts:54

Methods​

decryptAndVerify​

β–Έ decryptAndVerify(field, ciphertext, integrityHash): Promise<Uint8Array>

Parameters​

NameType
fieldEncryptedUserDataResponseValue
ciphertextUint8Array
integrityHashstring

Returns​

Promise<Uint8Array>

Defined in​

privy-js/packages/privy-browser/src/client.ts:520


decryptKeys​

β–Έ decryptKeys(userId, keys): Promise<Uint8Array[]>

Parameters​

NameType
userIdstring
keys{ encrypted_key: string ; field_id: string ; wrapper_key_id: string }[]

Returns​

Promise<Uint8Array[]>

Defined in​

privy-js/packages/privy-browser/src/client.ts:566


del​

β–Έ del(userId, fields): Promise<void>

Delete a field of data for a given user.

await client.del("0x123", "email");

Parameters​

NameTypeDescription
userIdstringThe id of the user.
fieldsstringThe field to delete.

Returns​

Promise<void>

Defined in​

privy-js/packages/privy-browser/src/client.ts:187

β–Έ del(userId, fields): Promise<void>

Delete multiple fields of data for a given user.

await client.del("0x123", ["email", "name"]);

Parameters​

NameTypeDescription
userIdstringThe id of the user.
fieldsstring[]The list of fields to delete.

Returns​

Promise<void>

Defined in​

privy-js/packages/privy-browser/src/client.ts:198


get​

β–Έ get(userId, fields): Promise<null | FieldInstance>

Get a single field of user data from the Privy API.

const email = await client.get("0x123", "email");

Parameters​

NameTypeDescription
userIdstringThe id of the user this data belongs to.
fieldsstringString field name.

Returns​

Promise<null | FieldInstance>

A FieldInstance if the field exists, or null otherwise.

Defined in​

privy-js/packages/privy-browser/src/client.ts:100

β–Έ get(userId, fields): Promise<(null | FieldInstance)[]>

Get multiple fields of user data from the Privy API.

const [firstName, lastName] = await client.get("0x123", ["first-name", "last-name"]);

Parameters​

NameTypeDescription
userIdstringThe id of the user this data belongs to.
fieldsstring[]Array of string field names.

Returns​

Promise<(null | FieldInstance)[]>

Array of results in the same order as the input. Each result is a FieldInstance if the field exists or null otherwise.

Defined in​

privy-js/packages/privy-browser/src/client.ts:112


getByIntegrityHash​

β–Έ getByIntegrityHash(integrityHash): Promise<null | FieldInstance>

Lookup a field instance by its integrity hash. This method can be used to verify data in addition to fetching it from Privy. For example, this method will:

  1. Lookup data by integrity hash
  2. Return the field instance if it exists
  3. Re-compute the integrity hash client side. If it is NOT the same as the integrityHash argument, this method will throw an error.
const ssn = await client.put("0x123", "ssn", "123-45-6789");
const ssnIntegrityHash = ssn.integrity_hash;

// later on...
const ssn = await client.getByIntegrityHash(ssnIntegrityHash);

Parameters​

NameTypeDescription
integrityHashstringHash used for content addressing.

Returns​

Promise<null | FieldInstance>

The corresponding FieldInstance if it exists, or null otherwise.

Defined in​

privy-js/packages/privy-browser/src/client.ts:350


getFile​

β–Έ getFile(userId, field): Promise<null | FieldInstance>

Download a file stored under a field.

const avatar = await client.getFile("0x123", "avatar");
download(avatar);

function download(field: FieldInstance) {
const data = window.URL.createObjectURL(field.blob());

// Lookup extension by mime type (included on blob)
const ext = getExtensionFromMIMEType(blob.type);
const filename = `${field.integrity_hash}.${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();
}

Parameters​

NameTypeDescription
userIdstringThe id of the user this file belongs to.
fieldstringThe field the file is stored under.

Returns​

Promise<null | FieldInstance>

A FieldInstance if the file exists, or null otherwise.

Defined in​

privy-js/packages/privy-browser/src/client.ts:240


getWrapperKeys​

β–Έ getWrapperKeys(userId, fields, algorithm): Promise<{ algorithm: string ; id: Uint8Array ; publicKey: Uint8Array }[]>

Parameters​

NameType
userIdstring
fieldsstring[]
algorithmstring

Returns​

Promise<{ algorithm: string ; id: Uint8Array ; publicKey: Uint8Array }[]>

Defined in​

privy-js/packages/privy-browser/src/client.ts:546


put​

β–Έ put(userId, field, value): Promise<FieldInstance>

Updates data for a single field for a given user.

const email = await client.put("0x123", "email", "foo@example.com");

Parameters​

NameTypeDescription
userIdstringThe id of the user this data belongs to.
fieldstringString field name.
valuestringValue to save.

Returns​

Promise<FieldInstance>

FieldInstance of the updated field.

Defined in​

privy-js/packages/privy-browser/src/client.ts:140

β–Έ put(userId, fields): Promise<FieldInstance[]>

Updates data for multiple fields for a given user.

const [firstName, lastName] = await client.put("0x123", [
{field: "first-name", value: "Jane"},
{field: "last-name", value: "Doe"},
]);

Parameters​

NameTypeDescription
userIdstringThe id of the user this data belongs to.
fieldsUpdateFieldInstanceRequest[]Array of objects with field and value keys.

Returns​

Promise<FieldInstance[]>

Array of FieldInstances of the updated fields, in the same order as the input.

Defined in​

privy-js/packages/privy-browser/src/client.ts:155


putFile​

β–Έ putFile(userId, field, blob): Promise<FieldInstance>

Upload a file for a given field.

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

Parameters​

NameTypeDescription
userIdstringThe id of the user this file belongs to.
fieldstringThe field to store the file in.
blobBlobThe plaintext contents of the file in a Blob.

Returns​

Promise<FieldInstance>

FieldInstance for the uploaded file.

Defined in​

privy-js/packages/privy-browser/src/client.ts:290