Execute A Swap With The Aggregator API
Interacting With KyberSwap Aggregator Router Contract
Overview
KyberSwap maintains a single API specification for all EVM chains:
Sequence diagram

To execute a swap, the router (MetaAggregationRouterV2
) contract requires the encoded swap data to be included as part of the transaction. This encoded swap data as well as other swap metadata are returned as part of the API response. As such, developers are expected to call the swap API prior to sending a transaction to the router contract.
TypeScript Example
Aggregator API Demo
The code snippets in the guide below have been extracted from our demo GitHub repo which showcases the full end-to-end Aggregator operations in a TypeScript environment.
Step 1: Query Swap Route
Integrators can easily query for superior rates by passing in the following required parameters into the [V1] Get Swap Route
API:
const targetPathConfig = {
params: {
tokenIn: tokenIn.address,
tokenOut: tokenOut.address,
amountIn: Number(1*10**tokenIn.decimals).toString()
}
};
Note that the full list of available parameters as well as their usage can be found on the specification page.
For each of the token swaps queried, the [V1] GET
API will return a data object consisting of:
routeSummary
-> An object containing the routing data in human readable format. The API will only return the route with the best rate as determined by the KyberSwap Aggregator algorithm.routerAddress
-> The address of the router contract which facilitates the swap
Step 2: Encode Preferred Swap Route
Upon finding a favourable route, we can then go ahead and encode the selected route by including the routeSummary
in the request body of [V1] Post Swap Route For Encoded Data
. Encoding the swap route via the API abstracts away the complexity of interacting with the contract directly.
const requestBody = {
routeSummary: routeSummary,
sender: signerAddress,
recipient: signerAddress,
slippageTolerance: 10 //0.1%
}
Note that the [V1] POST
API requires the sender
and recipient
to be appended to the routeSummary
in order to configure the parties to the swap. In most cases, these would usually be the address that is executing the swap.
It is highly recommended that a slippageTolerance
(in bips) is set to ensure that the swap proceeds smoothly within the boundaries set. Refer to slippage for more info regarding it's causes and effects on swap rates.
The encoded data for the selected swap route will be returned as a hexstring under the data
object.
Step 3: Execute Swap Transaction On-Chain
To execute the swap, we will leverage the ethers.js library. For simplicity, we will hard code a private key under /src/libs/signer.ts
that will allow us to programatically interact with the EVM from the backend.
With a configured signer, we can then execute the transaction on-chain by paying the necessary gas fees:
const executeSwapTx = await signer.sendTransaction({
data: encodedSwapData,
from: signerAddress,
to: routerContract,
maxFeePerGas: 1000000000000,
maxPriorityFeePerGas: 1000000000000
});
A tx hash will be returned once the swap tx has been successfully executed:
Last updated
Was this helpful?