Swap API
Generates ready-to-use transaction calldata for executing a swap via the Universal Router. The response can be sent directly as a transaction.POST /api/v1/swap
Request Body
{
"tokenIn": "0xB190a6A7fC2873f1Abf145279eD664348d5Ef630",
"tokenOut": "0x3Ac3B30b7f18AEFD4590D7FE4d9C5944aaeB7220",
"amountIn": "1000000000000000000",
"tradeType": 0,
"slippage": 0.5,
"recipient": "0xYourAddress",
"deadline": 1200
}
| Field | Type | Required | Description |
|---|---|---|---|
tokenIn | string | Yes | Input token address |
tokenOut | string | Yes | Output token address |
amountIn | string | Conditional | Input amount (required when tradeType=0) |
amountOut | string | Conditional | Output amount (required when tradeType=1) |
tradeType | number | No | 0 = Exact Input (default), 1 = Exact Output |
slippage | number | No | Slippage tolerance as percentage (default: 0.5) |
recipient | string | Yes | Address to receive output tokens |
deadline | number | No | Seconds until expiration (default: 1200 = 20 min) |
Response
{
"to": "0x440d7f5FE865eFCcfdCB1ee9a000C114163689ba",
"data": "0x3593564c...",
"value": "0",
"gasEstimate": "100000",
"tradeType": 0,
"quote": {
"tokenIn": "0xb190...",
"tokenOut": "0x3ac3...",
"amountIn": "1000000000000000000",
"amountOut": "1007249042881810956",
"minAmountOut": "1002212797667401901",
"priceImpact": 0.3,
"protocol": "v2",
"path": ["0xb190...", "0x3ac3..."],
"fees": [0],
"hops": 1
},
"deadline": "1764573986",
"slippage": 0.5,
"permit2": "0xc320bc492Bb56169aBE18D3C0a2048c45febC897"
}
| Field | Description |
|---|---|
to | Universal Router address — use as transaction to |
data | Encoded calldata — use as transaction data |
value | Native KAS amount — use as transaction value (non-zero when input is KAS) |
gasEstimate | Estimated gas units |
quote.minAmountOut | Minimum output (Exact Input) — on-chain slippage protection |
quote.maxAmountIn | Maximum input (Exact Output) — on-chain slippage protection |
permit2 | Permit2 contract address |
Slippage Protection
| Trade Type | Field | Formula |
|---|---|---|
| Exact Input (0) | minAmountOut | amountOut × (1 - slippage / 100) |
| Exact Output (1) | maxAmountIn | amountIn × (1 + slippage / 100) |
Usage
// 1. Get calldata
const res = await fetch('/swap-api/api/v1/swap', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
tokenIn, tokenOut, amountIn,
tradeType: 0,
slippage: 0.5,
recipient: userAddress,
deadline: 1200
})
});
const swapData = await res.json();
// 2. Send transaction
const tx = await signer.sendTransaction({
to: swapData.to,
data: swapData.data,
value: swapData.value
});
Examples
{
"tokenIn": "0xB190a6A7fC2873f1Abf145279eD664348d5Ef630",
"tokenOut": "0x3Ac3B30b7f18AEFD4590D7FE4d9C5944aaeB7220",
"amountIn": "1000000000000000000",
"tradeType": 0,
"slippage": 0.5,
"recipient": "0xUserAddress",
"deadline": 1200
}
{
"tokenIn": "0xB190a6A7fC2873f1Abf145279eD664348d5Ef630",
"tokenOut": "0x3Ac3B30b7f18AEFD4590D7FE4d9C5944aaeB7220",
"amountOut": "1000000000000000000",
"tradeType": 1,
"slippage": 0.5,
"recipient": "0xUserAddress",
"deadline": 1200
}