Skip to main content

Connecting to Phantom Wallets

After instantiating the SDK, use sdk.connect() to establish a connection to the wallet and access chain-specific operations.

Basic Connection

User Wallet Connection

import { BrowserSDK, AddressType } from "@phantom/browser-sdk";

// 1. Create SDK instance
const sdk = new BrowserSDK({
  providerType: "embedded", // or "injected" for browser extension
  addressTypes: [AddressType.solana, AddressType.ethereum],
  appId: "your-app-id",
});

// 2. Connect to wallet
const { addresses } = await sdk.connect();
console.log("Connected addresses:", addresses);

// 3. Use chain-specific methods
const signature = await sdk.solana.signMessage("Hello!");
const ethResult = await sdk.ethereum.sendTransaction({
  to: "0x...",
  value: "1000000000000000000",
  gas: "21000",
});

Switching Between Provider Types

Even if you start the SDK with the embedded provider, you can switch to the injected provider (browser extension) at runtime:
import { BrowserSDK, AddressType, waitForPhantomExtension } from "@phantom/browser-sdk";

// Start with embedded provider
const sdk = new BrowserSDK({
  providerType: "embedded",
  addressTypes: [AddressType.solana, AddressType.ethereum],
  appId: "your-app-id",
  authOptions: {
    redirectUrl: "https://yourapp.com/auth/callback", // Must be an existing path in your app and whitelisted in Portal
  },
});

// Check if Phantom extension is available before switching
const isExtensionAvailable = await waitForPhantomExtension(5000); // Wait up to 5 seconds

if (!isExtensionAvailable) {
  console.log("Phantom extension is not installed");
  // Handle extension not available (show install prompt, etc.)
  return;
}

// Switch to injected provider and connect
await sdk.switchProvider("injected");
const { addresses } = await sdk.connect();
console.log("Connected to extension:", addresses);
Future API Changes: This interface will change in future versions. The connect() method will receive a parameter with the provider type, eliminating the need for switchProvider().
Important notes about redirectUrl (for embedded provider):
  • Must be an existing page/route in your application
  • Must be whitelisted in your Phantom Portal app configuration
  • This is where users will be redirected after completing OAuth with Google

Chain-Specific Operations

After connection, use dedicated chain interfaces:

Solana Operations

// Message signing
const signature = await sdk.solana.signMessage("Hello Solana!");

// Transaction signing (without sending)
const signedTx = await sdk.solana.signTransaction(transaction);

// Sign and send transaction
const result = await sdk.solana.signAndSendTransaction(transaction);

// Network switching (works on embedded for solana)
await sdk.solana.switchNetwork('devnet');

// Utilities
const publicKey = await sdk.solana.getPublicKey();
const isConnected = sdk.solana.isConnected();

Ethereum Operations

// EIP-1193 requests
const accounts = await sdk.ethereum.request({ method: 'eth_accounts' });
const chainId = await sdk.ethereum.request({ method: 'eth_chainId' });

// Message signing
const signature = await sdk.ethereum.signPersonalMessage(message, address);

// EIP-712 typed data signing
const typedDataSignature = await sdk.ethereum.signTypedData(typedData, address);

// Transaction sending
const result = await sdk.ethereum.sendTransaction({
  to: "0x...",
  value: "1000000000000000000", // 1 ETH in wei
  gas: "21000",
});

// Network switching
await sdk.ethereum.switchChain(1); // Ethereum mainnet
await sdk.ethereum.switchChain(137); // Polygon

// Utilities
const chainId = await sdk.ethereum.getChainId();
const accounts = await sdk.ethereum.getAccounts();
const isConnected = sdk.ethereum.isConnected();

Auto-Connect Feature

The SDK can automatically reconnect to existing sessions when instantiated, providing a seamless user experience.
const sdk = new BrowserSDK({
  providerType: "injected", // or "embedded" for user wallets
  addressTypes: [AddressType.solana],
  appId: "your-app-id",
  autoConnect: true, // Default: true
});

// SDK will automatically check for existing valid session and connect in background
// No need to call connect() if user already has a session

// Check if already connected
if (sdk.isConnected()) {
  console.log("Already connected!");
  const addresses = await sdk.getAddresses();
} else {
  // First time or session expired, need to connect manually
  await sdk.connect();
}
I