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
Name | Type | Description |
---|---|---|
appId | string | The app id from your console. |
appSecret | string | The app secret, only visible once on app creation in the console. |
options | Object | Initialization options. |
options.apiURL? | string | The URL of the Privy API. Defaults to https://auth.privy.io . |
options.timeout? | number | Time 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
Name | Type | Description |
---|---|---|
userId | string | The 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
Name | Type | Description |
---|---|---|
userId | string | The 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
Name | Type | Description |
---|---|---|
importUserInput | ImportUserInput | The 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
Name | Type | Description |
---|---|---|
allowlistEntry | AllowlistEntryInput | The 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
Name | Type | Description |
---|---|---|
allowlistEntry | AllowlistEntryInput | The 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
Name | Type | Default value | Description |
---|---|---|---|
token | string | undefined | The auth token (JWT). |
verificationKeyOverride | string | '' | Overrides the default ES256 JWT verification key loaded from app settings. |
Returns
Promise
<AuthTokenClaims
>
Object containing auth token claims.