Skip to main content

Class: PrivyClient

The Privy client performs operations against the Privy API.

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

Constructors

constructor

new PrivyClient(appId, appSecret, options?)

Creates a new Privy client.

Parameters

NameTypeDescription
appIdstringThe app id from your console.
appSecretstringThe app secret, only visible once on app creation in the console.
optionsObjectInitialization options.
options.apiURL?stringThe URL of the Privy API. Defaults to https://auth.privy.io.
options.timeout?numberTime in milliseconds after which to timeout requests to the API. Defaults to 10000 (10 seconds).

Methods

deleteUser

deleteUser(userId): Promise<void>

Delete the user object associated with the given user DID (decentralized ID).

const user = await client.deleteUser("did:privy:XXXXX");

Parameters

NameTypeDescription
userIdstringThe Privy DID of the user.

Returns

Promise<void>


getAllowlist

getAllowlist(): Promise<AllowlistEntry[]>

Get the allowlist entries for a the given app. This is a list of users that are allowed to access the app, if the allowlist is enabled.

const allowlist = await client.getAllowlist();

Returns

Promise<AllowlistEntry[]>

a list of AllowlistEntry objects.


getAppSettings

getAppSettings(): Promise<AppSettings>

Get the app settings associated with the given app.

Returns

Promise<AppSettings>

the AppSettings


getUser

getUser(userId): Promise<User>

Get the user object associated with the given user DID (decentralized ID).

const user = await client.getUser("did:privy:XXXXX");

Parameters

NameTypeDescription
userIdstringThe Privy DID of the user.

Returns

Promise<User>

A User if the user exists.


getUsers

getUsers(): Promise<User[]>

Get the list of users associated with this app.

const users = await client.getUsers();

Returns

Promise<User[]>

A list of users.


getVerificationKey

getVerificationKey(): Promise<string>

Get the auth token verification key.

Note that the verification key is pulled from app settings for the first time only and then cached. On subsequent calls the cached verification key is returned.

Returns

Promise<string>

String holding the verification key.


importUser

importUser(importUserInput): Promise<null | User>

Import existing user data into Privy, including their linked account data.

This allows your users to interact with all their linked accounts normally after your migration to Privy.

const user = await client.importUser({
linkedAccounts: [
{
"subject": "123456789123456789",
"username": "batman#1234",
"email": "[email protected]",
"type": "discord_oauth"
},
{
"number": "+1 123 456 7890",
"type": "phone"
},
{
"address": "0x3DAF84b3f09A0E2092302F7560888dBc0952b7B7",
"type": "wallet",
"chainType": "ethereum"
}
]);

Parameters

NameTypeDescription
importUserInputImportUserInputThe user object ImportUserInput, containing linked accounts to import.

Returns

Promise<null | User>

The resultant user User object.


inviteToAllowlist

inviteToAllowlist(allowlistEntry): Promise<AllowlistEntry>

Add an allowlist entry for the given app, to give that user access.

The allowlist must enabled for the app, otherwise this will fail.

const entry = await client.inviteToAllowlist({ type: 'email', value: '[email protected]'});

Parameters

NameTypeDescription
allowlistEntryAllowlistEntryInputThe AllowlistEntryInput to add to the allowlist.

Returns

Promise<AllowlistEntry>

the created AllowlistEntry object.


removeFromAllowlist

removeFromAllowlist(allowlistEntry): Promise<AllowlistEntry>

Remove an entry from the allowlist for a Privy app.

The allowlist must enabled for the app, otherwise this will fail.

await client.removeFromAllowlist();

Parameters

NameTypeDescription
allowlistEntryAllowlistEntryInputThe AllowlistEntryInput to remove from the allowlist.

Returns

Promise<AllowlistEntry>

the deleted AllowlistEntry object.


verifyAuthToken

verifyAuthToken(token, verificationKeyOverride?): Promise<AuthTokenClaims>

Verify the auth token format, signature, and claims.

Example usage given e.g. a NextApiRequest request:

const header = request.headers.authorization;
const token = header.replace(/^Bearer /, '');
const verifiedClaims = await verifyAuthToken(token);

Parameters

NameTypeDefault valueDescription
tokenstringundefinedThe auth token (JWT).
verificationKeyOverridestring''Overrides the default ES256 JWT verification key loaded from app settings.

Returns

Promise<AuthTokenClaims>

Object containing auth token claims.