Execute A Swap With The Aggregator API
Interacting With KyberSwap Aggregator Router Contract
Last updated
Was this helpful?
Interacting With KyberSwap Aggregator Router Contract
Last updated
Was this helpful?
KyberSwap maintains a single API specification for all EVM chains:
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.
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.
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
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.
The encoded data for the selected swap route will be returned as a hexstring under the data
object.
With a configured signer, we can then execute the transaction on-chain by paying the necessary gas fees:
A tx hash will be returned once the swap tx has been successfully executed:
In such cases, integrators can still use our whose call parameters are similar except for an additional required to
field that denotes the recipient of the swapped tokens. Please see for more details.
Integrators can easily query for superior rates by passing in the following required parameters into the API:
Note that the full list of available parameters as well as their usage can be found on the .
Upon finding a favourable route, we can then go ahead and encode the selected route by including the routeSummary
in the request body of . Encoding the swap route via the API abstracts away the complexity of interacting with the contract directly.
It is highly recommended that a slippageTolerance
(in bips) is set to ensure that the swap proceeds smoothly within the boundaries set. Refer to for more info regarding it's causes and effects on swap rates.
To execute the swap, we will leverage the 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.