Skip to content
✈️ TokenFlight SDK

Swap Widget

The Swap widget provides a cross-chain token swap interface. Users select source and destination tokens, enter an amount, and execute the swap.

Swap Widget
import { TokenFlightSwap } from '@tokenflight/swap';
const swap = new TokenFlightSwap({
container: '#swap-widget',
config: {
theme: 'dark',
locale: 'en-US',
slippage: 50,
},
callbacks: {
onSwapSuccess: (data) => console.log('Success:', data),
onSwapError: (data) => console.log('Error:', data),
onQuoteReceived: (data) => console.log('Quote:', data),
onAmountChanged: (data) => console.log('Amount:', data),
},
});
swap.initialize();
<tokenflight-swap
theme="dark"
locale="en-US"
default-slippage="50"
from-token="eip155:1:0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
to-token="eip155:8453:0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"
></tokenflight-swap>
AttributeDefaultDescription
theme'light''light', 'dark', or 'auto'
locale'en-US'Display locale
default-slippage'50'Slippage tolerance in basis points (50 = 0.5%)
from-tokenSource token in CAIP-10 format (e.g. eip155:1:0x...)
to-tokenDestination token in CAIP-10 format
api-endpointCustom API endpoint URL
csp-nonceNonce for CSP-restricted environments
PropertyTypeDefaultDescription
theme'dark' | 'light' | 'auto''light'Widget color theme
localestring'en-US'Display locale
slippagenumber50Slippage tolerance in basis points (50 = 0.5%)
fromTokenTokenIdentifierSource token ({ chainId, address } or CAIP-10 string)
toTokenTokenIdentifierDestination token
apiEndpointstringCustom API endpoint URL
customColorsRecord<string, string>CSS color variable overrides
CallbackPayloadDescription
onSwapSuccessSwapSuccessDataFired after a successful swap transaction
onSwapErrorSwapErrorDataFired when a swap fails
onQuoteReceivedQuoteResponseFired when a new price quote is fetched
onAmountChangedAmountChangedDataFired when the user changes the input amount
onWalletConnectedWalletConnectedDataFired when a wallet connects
// Update theme without re-initialization
swap.setTheme('light'); // 'light' | 'dark' | 'auto'
// Apply custom color overrides
swap.setCustomColors({ primary: '#ff6600', background: '#1a1a2e' });
// Clean up the widget
swap.destroy();