Connect
The recommended and easiest way to connect to Phantom is by callingwindow.phantom.solana.connect()
. However, the provider also exposes a request
JSON RPC interface.
connect()
request()
connect()
call will return a Promise that resolves when the user accepts the connection request, and reject (throw when awaited) when the user declines the request or closes the pop-up. See Errors for a breakdown of error messages Phantom may emit.
When the user accepts the request to connect, the provider will also emit a connect
event.
isConnected
boolean.
Eagerly connecting
After a web application connects to Phantom for the first time, it becomes trusted. Once trusted, it’s possible for the application to automatically connect to Phantom on subsequent visits or page refreshes, without prompting the user for permission. This is referred to as “eagerly connecting”. To implement this, applications should pass anonlyIfTrusted
option into the connect()
call.
connect()
request()
connect
event if the application is trusted. If the application is not trusted, Phantom will throw a 4001 error and remain disconnected until the user is prompted to connect without an onlyIfTrusted
flag. In either case, Phantom will not open a pop-up window, making this convenient to use on all page loads.
The following is an example of how a React application can eagerly connect to Phantom.
Disconnect
Disconnecting mirrors the same process as connecting. However, it is also possible for the wallet to initiate the disconnection, rather than the application itself.disconnect()
request()
disconnect
event.
Change accounts
Phantom allows users to seamlessly manage multiple accounts (such as keypairs) from within a single extension or mobile app. Whenever a user switches accounts, Phantom will emit anaccountChanged
event.
If a user changes accounts while already connected to an application, and the new account had already whitelisted that application, then the user will stay connected and Phantom will pass the PublicKey of the new account: