@reown/blockchain-api
A client library for interacting with the WalletConnect Blockchain API.
Installation
npm install @reown/blockchain-api
Usage
Basic Usage
import { BlockchainApiClient } from '@reown/blockchain-api'
const client = new BlockchainApiClient({
projectId: 'YOUR_PROJECT_ID',
sdkType: 'web-app',
sdkVersion: '1.0.0'
})
const supportedNetworks = await client.getSupportedNetworks()
const balance = await client.getBalance('0x123...', '1')
Advanced Configuration
You can configure the client with additional options:
import { BLOCKCHAIN_API_URL, BlockchainApiClient } from '@reown/blockchain-api'
const client = new BlockchainApiClient({
baseUrl: BLOCKCHAIN_API_URL,
projectId: 'YOUR_PROJECT_ID',
clientId: 'YOUR_CLIENT_ID',
sdkType: 'web-app',
sdkVersion: '1.0.0'
})
client.setProjectId('NEW_PROJECT_ID')
client.setClientId('NEW_CLIENT_ID')
client.setSdkType('mobile-app')
client.setSdkVersion('1.1.0')
Features
Account Information
const identity = await client.fetchIdentity({
address: '0x123...',
caipNetworkId: 'eip155:1',
sender: '0x456...'
})
const transactions = await client.fetchTransactions({
account: '0x123...',
chainId: '1',
cursor: 'next_page_cursor'
})
const balance = await client.getBalance('0x123...', '1')
Token Swaps
const tokens = await client.fetchSwapTokens({ chainId: '1' })
const prices = await client.fetchTokenPrice({
addresses: ['0x123...', '0x456...']
})
const quote = await client.fetchSwapQuote({
amount: '1000000000000000000',
userAddress: '0x123...',
from: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
to: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
})
const allowance = await client.fetchSwapAllowance({
tokenAddress: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
userAddress: '0x123...'
})
const approveCalldata = await client.generateApproveCalldata({
userAddress: '0x123...',
from: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
to: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
})
const swapCalldata = await client.generateSwapCalldata({
amount: '1000000000000000000',
userAddress: '0x123...',
from: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
to: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
})
ENS
const ensInfo = await client.lookupEnsName('vitalik.eth')
const ensNames = await client.reverseLookupEnsName({
address: '0x123...',
sender: '0x456...'
})
const suggestions = await client.getEnsNameSuggestions('vitalik')
const registration = await client.registerEnsName({
coinType: 60,
address: '0x123...',
message: 'Message to sign',
signature: '0x456...'
})
On-ramp
const options = await client.getOnrampOptions()
const quote = await client.getOnrampQuote({
purchaseCurrency: { id: 'ETH', symbol: 'ETH' },
paymentCurrency: { id: 'USD' },
amount: '100',
network: 'ethereum'
})
const url = await client.generateOnRampURL({
destinationWallets: [{ address: '0x123...', blockchains: ['ethereum'] }],
partnerUserId: 'user123',
defaultNetwork: 'ethereum',
purchaseAmount: '0.1',
paymentAmount: '100'
})
Smart Sessions
const sessions = await client.getSmartSessions('eip155:1:0x123...')
const revocation = await client.revokeSmartSession('0x123...', 'pci_value', 'signature_value')
Utility Functions
import {
formatAddress,
formatCaipAddress,
getChainIdFromCaipAddress,
getPlainAddress
} from '@reown/blockchain-api'
const address = getPlainAddress('eip155:1:0x123...')
const chainId = getChainIdFromCaipAddress('eip155:1:0x123...')
const caipAddress = formatCaipAddress('0x123...', 'eip155', '1')
const displayAddress = formatAddress('0x1234567890abcdef1234567890abcdef12345678')
License
MIT