To sponsor gas fees for transactions on Solana, see our guide here.

To send a transaction from a wallet using the React SDK, use the sendTransaction method from the useSendTransaction hook:

sendTransaction: (input: {
  transaction: SupportedSolanaTransaction;
  connection: Connection;
  uiOptions?: SendTransactionModalUIOptions;
  transactionOptions?: SendTransactionOptions;
  fundWalletConfig?: SolanaFundingConfig;
  address?: string;
}) => Promise<SolanaTransactionReceipt>

Usage

import {useSendTransaction, useSolanaWallets} from '@privy-io/react-auth/solana';
import {Connection, Transaction, VersionedTransaction, PublicKey, SystemProgram} from '@solana/web3.js';

// Inside your component
const { sendTransaction } = useSendTransaction();
const { wallets } = useSolanaWallets();

// Configure your connection to point to the correct Solana network
const connection = new Connection('https://api.mainnet-beta.solana.com');

// Create your transaction (either legacy Transaction or VersionedTransaction)
const transaction = new Transaction(); // or new VersionedTransaction()

// Build and add your instructions to the transaction...
const transferInstruction = SystemProgram.transfer({
  fromPubkey: new PublicKey(wallets[0].address), // Replace with the sender's address
  toPubkey: new PublicKey('RecipientPublicKeyHere'), // Replace with the recipient's address
  lamports: 1000000 // Amount in lamports (1 SOL = 1,000,000,000 lamports)
});
transaction.add(transferInstruction);

// Fetch and set the latest blockhash
const connection = new Connection('https://api.mainnet-beta.solana.com'); // Replace with your Solana RPC endpoint
const latestBlockhash = await connection.getLatestBlockhash();
transaction.recentBlockhash = latestBlockhash.blockhash; // Replace with the latest blockhash

// Set your address as the fee payer
transaction.feePayer = new PublicKey(wallets[0].address); // Set fee payer

// Send the transaction
const receipt = await sendTransaction({
    transaction: transaction,
    connection: connection
});

console.log("Transaction sent with signature:", receipt.signature);

Parameters

transaction
SupportedSolanaTransaction
required

The transaction to be sent. Can be either a legacy Transaction or VersionedTransaction from @solana/web3.js.

connection
Connection
required

Connection to an SVM (Solana) network.

uiOptions
SendTransactionModalUIOptions

UI options to customize the transaction request modal.

To hide confirmation modals, set options.uiOptions.showWalletUIs to false. Learn more about configuring modal prompts here.

transactionOptions
SendTransactionOptions

Transaction options to customize the transaction request.

fundWalletConfig
SolanaFundingConfig

Funding configuration to specify cluster and funding amount (if enabled), in the case of insufficient funds.

address
string

Address for the embedded wallet sending the transaction. Defaults to the user’s embedded wallet at HD index 0.

Returns

signature
string

The signature of the transaction.

signedTransaction
SupportedSolanaTransaction

The signed transaction.

parsedTransaction
ParsedTransactionWithMeta | null

The parsed transaction result.

fees
bigint

The fees paid for the transaction in lamports.