🔧 Configuration Setup
Configure API keys, RPC URLs, and production settings for optimal performance. Configure SwapKit →
Before diving into SwapKit’s features, it’s essential to understand its core concepts. These building blocks are used throughout the SDK and understanding them will make your development experience much smoother.
AssetValue
is SwapKit’s primary way of representing amounts of any asset across all supported chains. It handles decimal precision, formatting, and arithmetic operations automatically.
// @noErrorValidationimport { AssetValue } from "@swapkit/sdk";
const oneEth = AssetValue.from({ asset: "ETH.ETH", value: 1,});
const oneEthInWei = AssetValue.from({ asset: "ETH.ETH", value: "1000000000000000000", fromBaseDecimal: true,});
SwapKit uses a consistent format for identifying assets:
// @noErrorValidation
"ETH.ETH";"BTC.BTC";"AVAX.AVAX";
"ETH.USDC-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";"AVAX.USDC-0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E";
"ETH/ETH";"BTC/BTC";
AssetValue provides safe arithmetic operations:
// @noErrorValidationconst amount1 = AssetValue.from({ asset: "ETH.ETH", value: 1.5 });const amount2 = AssetValue.from({ asset: "ETH.ETH", value: 0.5 });
const sum = amount1.add(amount2);
const difference = amount1.sub(amount2);
const doubled = amount1.mul(2);
const halved = amount1.div(2);
Compare AssetValue instances safely:
// @noErrorValidationconst amount1 = AssetValue.from({ asset: "BTC.BTC", value: 1 });const amount2 = AssetValue.from({ asset: "BTC.BTC", value: 0.5 });
amount1.gt(amount2);amount1.gte(amount2);amount1.lt(amount2);amount1.lte(amount2);amount1.eq(amount2);
Format values for display:
// @noErrorValidationconst btcAmount = AssetValue.from({ asset: "BTC.BTC", value: 0.00123456 });
btcAmount.toSignificant(4);
btcAmount.toCurrency("USD", 45000);
const largeAmount = AssetValue.from({ asset: "ETH.ETH", value: 1234567 });largeAmount.toAbbreviation();
btcAmount.getValue("string");btcAmount.getBaseValue("string");
Check asset properties:
// @noErrorValidationconst ethAmount = AssetValue.from({ asset: "ETH.ETH", value: 1 });const usdcAmount = AssetValue.from({ asset: "ETH.USDC-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", value: 100,});const synthEth = AssetValue.from({ asset: "ETH/ETH", value: 1 });
ethAmount.isGasAsset();usdcAmount.isGasAsset();synthEth.isSynthetic();
SwapKitNumber
provides utilities for safe numeric operations with BigInt support:
// @noErrorValidationimport { SwapKitNumber } from "@swapkit/sdk";
const num1 = new SwapKitNumber(123.456);const num2 = new SwapKitNumber("789.012");const num3 = new SwapKitNumber({ value: 100, decimal: 18 });
const sum = num1.add(num2);const product = num1.mul(num2);
num1.toSignificant(4);num1.toFixed(2);
SwapKit uses consistent chain identifiers across the entire SDK:
// @noErrorValidationimport { Chain } from "@swapkit/sdk";
Chain.Ethereum;Chain.Avalanche;Chain.BinanceSmartChain;Chain.Polygon;Chain.Arbitrum;Chain.Optimism;Chain.Base;
Chain.Bitcoin;Chain.BitcoinCash;Chain.Litecoin;Chain.Dogecoin;Chain.Dash;Chain.Zcash;
Chain.Cosmos;Chain.THORChain;Chain.Maya;Chain.Kujira;
Chain.Solana;Chain.Polkadot;Chain.Chainflip;Chain.Radix;Chain.Ripple;
SwapKit uses a typed error system for better debugging:
The base error class with additional context:
// @noErrorValidationimport { SwapKitError } from "@swapkit/sdk";
try { await swapKit.swap({ route });} catch (error) { if (error instanceof SwapKitError) { console.error("Error:", error.message); console.error("Code:", error.code); console.error("Details:", error.details); }}
SwapKit uses standardized error codes:
// @noErrorValidation
"wallet_not_found";"wallet_connection_failed";"wallet_insufficient_funds";
"transaction_failed";"transaction_rejected";"transaction_timeout";
"api_request_failed";"api_rate_limit";"api_invalid_response";
"invalid_address";"invalid_amount";"invalid_chain";
Errors include helpful context:
// @noErrorValidationcatch (error) { if (error instanceof SwapKitError) {
console.log('Context:', error.info); }}
SwapKit supports various wallet types through the WalletOption
enum:
// @noErrorValidationimport { WalletOption } from "@swapkit/sdk";
WalletOption.METAMASK;WalletOption.COINBASE;WalletOption.OKX;WalletOption.BITGET;WalletOption.EXODUS;WalletOption.KEPLR;WalletOption.LEAP;WalletOption.COSMOSTATION;WalletOption.LEDGER;WalletOption.TREZOR;WalletOption.KEEPKEY;WalletOption.WALLETCONNECT;WalletOption.KEYSTORE;WalletOption.POLKADOTJS;WalletOption.TALISMAN;WalletOption.PHANTOM;WalletOption.ONEKEY;WalletOption.RADIX_MOBILE;WalletOption.BRAVE;WalletOption.TRUSTWALLET;WalletOption.XDEFI;
Control transaction fees with the FeeOption
enum:
// @noErrorValidationimport { FeeOption } from "@swapkit/sdk";
FeeOption.Average;FeeOption.Fast;FeeOption.Fastest;
await swapKit.transfer({ assetValue, recipient, feeOptionKey: FeeOption.Fast,});
SwapKit integrates with multiple swap providers:
// @noErrorValidationimport { ProviderName } from "@swapkit/sdk";
ProviderName.THORCHAIN;ProviderName.CHAINFLIP;ProviderName.ONEINCH;ProviderName.UNISWAP;ProviderName.SUSHISWAP;ProviderName.PANCAKESWAP;
// @noErrorValidation
const amount = AssetValue.from({ asset: "ETH.ETH", value: 1 });await swapKit.transfer({ assetValue: amount, recipient });
await swapKit.transfer({ value: 1, recipient });
// @noErrorValidation
const usdcAmount = AssetValue.from({ asset: "ETH.USDC-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", value: 100,});
const weiAmount = AssetValue.from({ asset: "ETH.ETH", value: "1000000000000000000", fromBaseDecimal: true,});
// @noErrorValidation
if (balance.gte(requiredAmount)) { await performTransaction();}
if (Number(balance.getValue()) >= Number(requiredAmount.getValue())) { await performTransaction();}
// @noErrorValidation
const btcBalance = AssetValue.from({ asset: "BTC.BTC", value: 0.00123456 });
const displayValue = btcBalance.toSignificant(6);
const usdValue = btcBalance.toCurrency("USD", btcPrice);
Now that you understand SwapKit’s core concepts, you’re ready to build your first application:
🔧 Configuration Setup
Configure API keys, RPC URLs, and production settings for optimal performance. Configure SwapKit →
⚡ Essential Actions
Learn the core functionality every SwapKit application needs to implement. Connect Wallets →
🛠️ Direct Chain Operations
Use toolboxes for low-level blockchain operations and custom integrations. Toolbox Usage →
📚 API Reference
Explore the complete SwapKit API with detailed method documentation. API Reference →
For beginners: Start with Essential Actions to learn wallet connections and basic transactions.
For experienced developers: Jump to Advanced Features or explore specific chain integrations.
For production deployments: Review Best Practices and Security Guidelines.