Skip to main content

Initialize user overview

Before you can start sending notifications, or receiving notifications from your preferred protocol, you will need to initialize a user or re-authenticate the user. To do so you will use PushAPI.initialize call from @pushprotocol/restapi package.

Initialize user API

import { PushAPI, CONSTANTS } from "@pushprotocol/restapi";

// Initialize wallet user
// PushAPI.initialize(signer, {options?});
// 'CONSTANTS.ENV.PROD' -> mainnet apps | 'CONSTANTS.ENV.STAGING' -> testnet apps
const userAlice = await PushAPI.initialize(signer, {
env: CONSTANTS.ENV.STAGING,
})

// Check for errors in userAlice's initialization and handle them if any
if (userAlice.errors.length > 0) {
// Handle Errors Here
}

Note: If a signer is not provided, or if an invalid signer is passed, or the SDK fails to generate or decrypt keys for the signer, the PushAPI will switch to Read mode.

The function automatically initializes and returns the Push user profile object if one doesn't exist for the user or alternatively creates a new profile if the user has not been onboarded to Push network. This profile is used to interact with all function calls moving forward.


Using for Wallet Addresses

Push supports wallet addresses (EVM, with non-EVM coming soon). Simply passing the signer will ensure that the PushAPI is initialized with address of the wallet (derived by the signer).

tip

Passing null in the signer enables Read Only Mode. Read only mode allows you to use certain APIs that are not sensitive in nature like reading user profile, listing notifications that are not encrypted by the sender, listing chats, etc.


Using for Lens / Cyberconnect / Any NFTs

Push also supports Lens, Cyberconnect, NFT wallets and any EVM or non-EVM wallets ensuring cross-chain messaging or cross-nft messaging can become a reality.

You will need to pass the account parameter in options object while initializing to target them. This will ensure that the PushAPI is initialized for the right account.

Example

  • NFT profile - Pass nft:{chain_standard}:{nftChainId}:{nftContractAddress}:{nftTokenId}
  • account - nft:eip155:11155111:0x42af3147f17239341477113484752D5D3dda997B:2:1683058528

Read about supported wallet standards to understand all supported formats for account parameter.


Initialize user parameters

When initializing your user, you can customize the process using several parameters. Here's a breakdown:

ParamTypeSub-TypeDefaultRemarks
signerSignerType--Ethers or Viem Signer. Pass null and provide account to enable read only mode
optionsPushAPIInitializeProps--Optional configuration properties for initializing the PushAPI.
-options.envENVstagingAPI env - 'prod' or 'staging'.
-options.progressHook(progress: ProgressHookType) => void-A callback function to receive progress updates during initialization. Expand Progress Hook Type and Response dropdown below to learn more
-options.accountstring-The account to associate user with. If not provided, it is derived from signer.
-options.versionstringENC_TYPE_V3The encryption version to use for the PushAPI.
-options.versionMeta{ NFTPGP_V1 ?: password: string }-Metadata related to the encryption version, including a password if needed.
-options.autoUpgradebooleantrueIf true, upgrades encryption keys to latest encryption version.
-options.originstring-Specify origin or source while creating a Push Profile.

Note: Parameters in this style are mandatory.

ProgressHook Type and Response

Informs about individual progress stages during user creation / updates if progresshook as a callback function is passed during API call.

ParamTypeSubtypeDefaultRemarks
progressobject--progress object that is passed in the callback
-Progress.progressIdstring-Predefined, ID associated with the progress objects
-Progress.progressTitlestring-Predefined, title associated with the progress objects
-Progress.progressInfostring-Predefined, info associated with the progress objects
-Progress.levelstring-Predefined, Level associated with the progress objects

Progress object details

Progress.idProgress.levelProgress.titleProgress.info
PUSH-CREATE-01INFOGenerating Secure Profile SignatureThis step is only done for first time users and might take a few seconds. PGP keys are getting generated to provide you with secure yet seamless chat
PUSH-CREATE-02INFOSigning Generated ProfileThis step is only done for first time users. Please sign the message to continue.
PUSH-CREATE-03INFOEncrypting Generated ProfileEncrypting your keys. Please sign the message to continue.
PUSH-CREATE-04INFOSyncing Generated ProfilePlease sign the message to continue. Steady lads, chat is almost ready!
PUSH-CREATE-05SUCCESSSetup Complete-
PUSH-UPGRADE-01INFODeprecated Generating New Profile SignatureTrying to Upgrade Push Chat Keys to latest version. Please sign the message to continue.
PUSH-UPGRADE-02INFODecrypting Old ProfileTrying to Upgrade Push Chat Keys to latest version. Please sign the message to continue.
PUSH-UPGRADE-03INFODeprecated Syncing New ProfilePlease sign the message to continue. Steady lads, chat is almost ready!
PUSH-UPGRADE-04INFODeprecated Decrypting Old ProfileTrying to Upgrade Push Chat Keys to latest version. Please sign the message to continue.
PUSH-UPGRADE-05SUCCESSUpgrade Completed, Welcome to Push Chat-
PUSH-DECRYPT-01INFODecrypting ProfilePlease sign the transaction to decrypt profile
PUSH-DECRYPT-02SUCCESSPush Profile UnlockedUnlocking push profile
PUSH-AUTH-UPDATE-01INFOGenerating New Profile SignatureTrying to Update Push Chat Keys to VERSION_NUMBER version. Please sign the message to continue.
PUSH-AUTH-UPDATE-02INFOGenerating New Encrypted ProfileEncrypting Push Chat Keys with VERSION_NUMBER version. Please sign the message to continue.
PUSH-AUTH-UPDATE-03INFOSyncing Updated ProfilePlease sign the message to continue. Steady lads, chat is almost ready!
PUSH-AUTH-UPDATE-04SUCCESSUpdate Completed, Welcome to Push Chat-
PUSH-AUTH-UPDATE-05INFOGenerating New Profile SignatureTrying to Update Push Profile creds. Please sign the message to continue.
PUSH-AUTH-UPDATE-06INFOGenerating New Profile SignatureEncrypting Push Chat Keys with new creds. Please sign the message to continue.
PUSH-DECRYPT-AUTH-01INFODecrypting Profile CredsPlease sign the transaction to decrypt profile creds.
PUSH-DECRYPT-AUTH-02INFOPush Profile Creds UnlockedUnlocking push profile creds
PUSH-PROFILE-UPDATE-01ERRORSyncing Updated ProfileSteady lads, your profile is getting a new look!
PUSH-PROFILE-UPDATE-02SUCCESSProfile Update Completed, Welcome to Push Chat-
PUSH-ERROR-00ERRORNon Specific ErrorDynamic error in the following format: [Push SDK] - API - Error - API [functionName()] -: [Dynamic Error Info]
PUSH-ERROR-01WARNUpgrade Profile FailedDynamic warning in the following format: [Push SDK] - API - Error - API decryptPgpKey() -: [Dynamic Error Info]
PUSH-ERROR-02ERRORTransaction failedDynamic warning in the following format: [Push SDK] - Contract - Error - [Name] -: [Dynamic Error Info]
Expected response
{
{
account: "0xC785989454C958eF0fD343e3E6DcCDf9893a5b02",
channel: Channel {
signer: JsonRpcSigner,
env: 'prod',
guestMode: true,
account: '0xC785989454C958eF0fD343e3E6DcCDf9893a5b02',
info: ƒ,

},
chat: Chat {
account: '0xC785989454C958eF0fD343e3E6DcCDf9893a5b02',
decryptedPgpPvtKey: '-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxcLYBGQRtHI…nYedYb\n=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D5wiajPEcMZlZlqHZuN12cnxriN51JrqhJgF4E/m+e/DtKZ/n4K/A\nKshB3MGUzVXJFZF2U/17dCLFMKoOB/JRUpFq9hiAfyX0wUDDnsGrxj6xySAO\nRjsDThxi933dIJyXAYLhf0ayGcFiBuM9sCo5I8GGrWL6LVH7NgarL3Y8bFPz\neuCMmJciOGJXFkLNlXL5fwpTnydAshtaPbNHTGA7ugydpkU4xi0e2h6YIirn\nz4hC3sRMQ2ypgpHm75/p6nn6kXS+29kEOnYNVUy+hptRbkA/fU2wV0bBQlod\nJPYaDBOrwODHzX8INjnYedYb\n=o4+2\n-----END PGP PRIVATE KEY BLOCK-----\n",
encryption: Encryption {
account: '0xC785989454C958eF0fD343e3E6DcCDf9893a5b02',
decryptedPgpPvtKey: '-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxcLYBGQRtHI…nYedYb\n=o4+2\n-----END PGP PRIVATE KEY BLOCK-----\n',
pgpPublicKey: '-----BEGIN PGP PUBLIC KEY BLOCK-----\n\nxsBNBGQRtHIB…HnWGw==\n=h2rm\n-----END PGP PUBLIC KEY BLOCK-----\n',
env: 'prod',
signer: JsonRpcSigner,

},
env: "prod",
notification: Notification {
signer: JsonRpcSigner,
env: 'prod',
guestMode: true,
account: '0xC785989454C958eF0fD343e3E6DcCDf9893a5b02',
list: ƒ,

},
pgpPublicKey: "-----BEGIN PGP PUBLIC KEY BLOCK-----\n\nxsBNBGQRtHIBCADWGXf/Ir844r6xG4FIoprXLv8LaekzuEDT1pYckE4TQSub\n6y1pCCF/eMWvUePvWBAgX8cNdBQ96gxnOB0Kxu2wQTWqLQ0PrL9ggF0dNQcQ\nPbPghOOTWwO2h/TKNa9KrhkCh9m9zjQU9o8w4HxOOJ74t8T9OXqYMOXBMSOr\nVw2bnGQfSodZV7PTJUqmA7ap5JwbGcNP9t1/XH87f6HFSgPwgN5u9rgKAwhJ\nFwujZxSJv2IPRNzpDAq/r/C+fgofkV9/kwU3odsKNMJ+REfNOhDVo+nBL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DgfyUVKRavYYgH8l9MFAw57Bq8Y+\nsckgDkY7A04cYvd93SCclwGC4X9GshnBYgbjPbAqOSPBhq1i+i1R+zYGqy92\nPGxT83rgjJiXIjhiVxZCzZVy+X8KU58nQLIbWj2zR0xgO7oMnaZFOMYtHtoe\nmCIq58+IQt7ETENsqYKR5u+f6ep5+pF0vtvZBDp2DVVMvoabUW5AP31NsFdG\nwUJaHST2GgwTq8Dgx81/CDY52HnWGw==\n=h2rm\n-----END PGP PUBLIC KEY BLOCK-----\n",
profile: Profile {
account: '0xC785989454C958eF0fD343e3E6DcCDf9893a5b02',
decryptedPgpPvtKey: '-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxcLYBGQRtHI…nYedYb\n=o4+2\n-----END PGP PRIVATE KEY BLOCK-----\n',
env: 'prod',
progressHook: undefined
},
progressHook: undefined,
signer: JsonRpcSigner {
_isSigner: true,
provider: Web3Provider,
_index: 0,
_address: null
},
user: User {
account: '0xC785989454C958eF0fD343e3E6DcCDf9893a5b02',
env: 'prod'
},
}
}

Initialize user interface

/**
* Initialization Parameters for PushAPI
*/

/**
* Specifies the type of signer.
* Choose between Ethers or Viem Signer.
*/
type Signer = SignerType;

interface PushAPIInitializeProps {
/**
* Sets the API environment.
* Options: 'prod', 'staging', 'dev'
* Default: 'staging'
*/
env?: ENV;

/**
* A callback function to receive updates during the initialization process.
*/
progressHook?: (progress: ProgressHookType) => void;

/**
* The account you want to link with the PushAPI.
* If left empty, it'll use the account associated with your signer.
*/
account?: string;

/**
* Specifies the encryption version for the PushAPI.
* Default: 'ENC_TYPE_V3'
*/
version?: string;

/**
* Provides additional data related to your chosen encryption version,
* like a necessary password.
*/
versionMeta?: {
NFTPGP_V1?: {
password: string;
};
};

/**
* When set to true, the system will automatically upgrade encryption keys
* to the latest available encryption version.
* Default: true
*/
autoUpgrade?: boolean;

/**
* Defines the origin or source when setting up a Push Profile.
*/
origin?: string;
}

/**
* Initializes the PushAPI with given parameters.
*
* @param signer The type of signer (Ethers or Viem Signer).
* @param options Optional configurations for initializing the PushAPI.
*/
function initializePushAPI(signer: Signer, options?: PushAPIInitializeProps) {
// Initialization logic here
}

Reinitialize user API

// Reinitialize PushAPI for fresh start of NFT Account
// Reinitialize only succeeds if the signer account is the owner of the NFT
// PushAPI.reinitialize{options?});
await userAlice.reinitialize({ versionMeta: { NFTPGP_V1: { password: 'NewPassword' } } });

The function resets the Push NFT user profile including all connections and communication. It's important to note that this method is not applicable to standard wallet addresses.

This is particularly useful during instances of NFT transfers where the NFT user password is not shared as it completely resets all communications, acting as a fresh start.

Important - If the NFT user password is shared, then do not to use this method, instead you should look at User encryption update API for managing user migration.

Reinitialize user parameters

ParamTypeSub-TypeDefaultRemarks
optionsPushAPIReinitializeProps--Optional configuration properties for reinitializing the PushAPI NFT account.
-options.versionMeta{ NFTPGP_V1 : password: string }-Metadata related to the nft encryption version, including a password need for encrypting NFT account

Note: Parameters in this style are mandatory.