Integration
Query Prices
How to get token prices and swap quotes
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
How to get token prices and swap quotes
const params = new URLSearchParams({
tokenIn: '0xTokenA',
tokenOut: '0xTokenB',
amountIn: '1000000000000000000', // 1 token
tradeType: '0'
});
const res = await fetch(`https://dex.kasplex.org/swap-api/api/v1/quote?${params}`);
const quote = await res.json();
console.log(`Price: 1 TokenA = ${quote.executionPrice} TokenB`);
console.log(`Price impact: ${quote.priceImpact}%`);
// Single token price
const res = await fetch('https://dex.kasplex.org/swap-api/api/v1/price/0xTokenAddress');
// Pair price
const res = await fetch('https://dex.kasplex.org/swap-api/api/v1/price/pair/0xToken0/0xToken1');
const PAIR_ABI = [
'function getReserves() view returns (uint112, uint112, uint32)',
'function token0() view returns (address)'
];
const pair = new ethers.Contract(pairAddress, PAIR_ABI, provider);
const [reserve0, reserve1] = await pair.getReserves();
const token0 = await pair.token0();
// Price of token0 in terms of token1
const price = Number(reserve1) / Number(reserve0);
const POOL_ABI = [
'function slot0() view returns (uint160, int24, uint16, uint16, uint16, uint8, bool)'
];
const pool = new ethers.Contract(poolAddress, POOL_ABI, provider);
const [sqrtPriceX96, tick] = await pool.slot0();
// Convert sqrtPriceX96 to price
const price = (Number(sqrtPriceX96) / 2 ** 96) ** 2;
// Adjust for decimals if tokens have different decimal places
const adjustedPrice = price * (10 ** (decimals0 - decimals1));
const res = await fetch(
'https://dex.kasplex.org/swap-api/api/v1/price/history/0xPoolAddress?limit=100'
);
| Method | Accuracy | Latency | Trust Model | Best For |
|---|---|---|---|---|
| Quote API | Highest (considers routing) | ~100ms | Centralized API | Swap UIs |
| Price API | High | ~50ms | Centralized API | Dashboards |
| On-chain V2 | Pool-level | ~2s (RPC) | Trustless | Oracles |
| On-chain V3 | Pool-level | ~2s (RPC) | Trustless | Advanced analytics |