Skip to content

Pregenerating an embedded wallet

With Privy, you can pregenerate self-custodial embedded wallets associated with a given account, like an email address or phone number, without requiring the user to login. You can even send assets to the wallet before the user logs in to your app for the first time.

Once the user associated with the account logs in, they will be able to access the pregenerated wallet and any assets sent to them.

Using @privy-io/server-auth

To pregenerate embedded wallets for a given user, use the PrivyClient's importUser method.

tsx
const user = await privy.importUser({
  linkedAccounts: [
    {
      type: 'email',
      address: '[email protected]',
    },
  ],
  createEmbeddedWallet: true,
});

As a parameter to the method, pass an ImportUserInput object with the following fields:

FieldTypeDescription
linkedAccountsLinkedAccount[]An array including all of the user's linked accounts.

These objects are in the same shape as the linked accounts returned by getUser. For each linked account, you must specify the type and must not include a verifiedAt timestamp.
createEmbeddedWallettrueWhether to pregenerate an embedded wallet for the imported user. You must set this to true.

This will pregenerate an embedded wallet that can be accessed by logging into the imported account (e.g. [email protected] in the example above).

Using the REST API

To pregenerate embedded wallets, simply make a POST request to Privy's import user API:

sh
https://auth.privy.io/api/v1/users

In the body of the request:

  • Include a linked_accounts array that contains the user's account to claim the wallet.
  • Include a create_embedded_wallet boolean with the value true.

Below is a sample cURL command for generating a new wallet for a user with Privy:

sh
$ curl --request POST https://auth.privy.io/api/v1/users \
-u "<your-privy-app-id>:<your-privy-app-secret>" \
-H "privy-app-id: <your-privy-app-id>" \
-H "Content-Type: application/json" \
-d '{
  "create_embedded_wallet": true,
  "linked_accounts": [
    {
      "address": "[email protected]",
      "type": "email"
    }
  ]
}'

A successful response will include the new user object along with their DID and embedded wallet address, like below:

json
{
  "id": "did:privy:clddy332f002tyqpq3b3lv327",
  "created_at": 1674788927,
  "linked_accounts": [
    {
      "address": "[email protected]",
      "type": "email"
    },
    {
      "address": "0x3DAF84b3f09A0E2092302F7560888dBc0952b7B7",
      "type": "wallet",
      "walletClient": "privy",
      "chain_type": "ethereum"
    }
  ]
}

Accessing a pregenerated wallet

A user can easily claim their pregenerated wallet simply by logging into your app with one of its linked accounts.