Skip to main content

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
}
FieldTypeRequiredDescription
tokenInstringYesInput token address
tokenOutstringYesOutput token address
amountInstringConditionalInput amount (required when tradeType=0)
amountOutstringConditionalOutput amount (required when tradeType=1)
tradeTypenumberNo0 = Exact Input (default), 1 = Exact Output
slippagenumberNoSlippage tolerance as percentage (default: 0.5)
recipientstringYesAddress to receive output tokens
deadlinenumberNoSeconds 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"
}
FieldDescription
toUniversal Router address — use as transaction to
dataEncoded calldata — use as transaction data
valueNative KAS amount — use as transaction value (non-zero when input is KAS)
gasEstimateEstimated gas units
quote.minAmountOutMinimum output (Exact Input) — on-chain slippage protection
quote.maxAmountInMaximum input (Exact Output) — on-chain slippage protection
permit2Permit2 contract address

Slippage Protection

Trade TypeFieldFormula
Exact Input (0)minAmountOutamountOut × (1 - slippage / 100)
Exact Output (1)maxAmountInamountIn × (1 + slippage / 100)
These limits are enforced on-chain by the Universal Router. The transaction reverts if the actual execution exceeds the slippage bounds.

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
}