Skip to main content

useConnection()

Hook to access the Solana RPC connection. Supports both legacy @solana/web3.js Connection and modern @solana/kit Rpc.

Return Values

DualConnection
Solana connection instance for RPC calls. Can be either:
  • Legacy: Connection from @solana/web3.js
  • Kit: Rpc from @solana/kit
The connection type depends on what you passed to ConnectionProvider or HermisProvider. All dual architecture transaction methods work seamlessly with either type.
WalletAdapterNetwork
Current network (mainnet-beta, devnet, testnet). Optional field.

Usage with web3.js Connection

Usage with Kit Rpc

Provider Setup

With Legacy Connection

With Kit Rpc (Custom Provider)

Note: ConnectionProvider creates a legacy Connection internally. To use Kit Rpc, create a custom provider:

With HermisProvider (Simplified)

Note: HermisProvider uses ConnectionProvider internally, which creates a legacy Connection.
The HermisProvider is the recommended approach for most applications. It combines wallet and connection setup with sensible defaults.

DualConnection Type

The DualConnection type is a union type that accepts both architectures:
This means you can pass either:
  • Connection from @solana/web3.js (legacy)
  • Rpc from @solana/kit (modern)
All transaction methods in the SDK (like sendTransaction, signAndSendTransaction) automatically handle both connection types.

Network Detection

When using auto-detected networks, the connection analyzes the RPC endpoint URL:
Supported network detection patterns:
  • URLs containing “devnet” → WalletAdapterNetwork.Devnet
  • URLs containing “testnet” → WalletAdapterNetwork.Testnet
  • URLs containing “mainnet” → WalletAdapterNetwork.Mainnet

Common RPC Methods

Both Connection and Kit Rpc support these common methods (with slight API differences):

getBalance

getAccountInfo

getLatestBlockhash

Type Safety

TypeScript will correctly infer the connection type based on your provider setup:
However, if you need to check at runtime which type you have:

Migration Notes

If you’re upgrading from v1 and want to use Kit:
Both approaches work - choose based on your needs:
  • Stick with Connection if you have existing web3.js code
  • Upgrade to Kit Rpc for modern features and better tree-shaking

Using Kit Rpc Without Providers

If you want to use Kit Rpc but don’t want to create a custom provider, you can pass the connection directly to transaction methods:
This approach works because all transaction methods (sendTransaction, signAndSendTransaction, etc.) accept DualConnection and handle both connection types automatically.