> For the complete documentation index, see [llms.txt](https://docs.kyberswap.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.kyberswap.com/~/changes/1kQEhDNzHMDf7SwOE6Yu/kyberswap-solutions/kyberswap-aggregator/developer-guides/upgrading-from-apiv1-to-apiv2.md).

# Upgrading From APIv1 To APIv2

More Performant Routing And Encoding

## Overview

Following feedback on the initial APIv1, KyberSwap has implemented a more performant APIv2 which improves the response time for getting a route via offloading encoding requirements to a separate post method. With this upgrade, APIv2 brings the following improvements:

* Reduced latency which enables gas estimation calculations to be more accurate
* Reduces the risk of stale encoded data which could potentially lead to failed transactions on the UI
* Functional separation which enables more precise data handling and compute

## Swap flow: V1 vs V2

As part of the APIv2 upgrade, querying of the route data and the encoded data has been separated into their own respective calls. This stands in contrast to APIv1 whereby both the route and encoded data are returned in a single [`GET`](/~/changes/1kQEhDNzHMDf7SwOE6Yu/kyberswap-solutions/kyberswap-aggregator/aggregator-api-specification/evm-swaps.md#chain-route-encode) query.

Due to this functional separation, applications have more flexibility when handling route refreshes prior to user confirming the swap route. For example, if rates need to be refreshed, the application will just have to query the APIv2 [`GET`](/~/changes/1kQEhDNzHMDf7SwOE6Yu/kyberswap-solutions/kyberswap-aggregator/aggregator-api-specification/evm-swaps.md#chain-api-v1-routes) endpoint which will return just the route summary. As the encoded data does not have to be processed at this stage, responses are much faster and redundant encodings are avoided.

Moreover, given the reduced latency for each call in APIv2, this reduces the likelihood that the returned data will be stale hence mitigating transaction failure risks. Routes are secured (i.e. "locked-in") via the APIv2 [`POST`](/~/changes/1kQEhDNzHMDf7SwOE6Yu/kyberswap-solutions/kyberswap-aggregator/aggregator-api-specification/evm-swaps.md#chain-api-v1-route-build) which returns the encoded data to be sent to the [`MetaAggregationRouterV2`](/~/changes/1kQEhDNzHMDf7SwOE6Yu/kyberswap-solutions/kyberswap-aggregator/contracts/aggregator-contract-addresses.md) contract.

### APIv1 swap flow

Route and encoded data returned in single `GET` call.

<figure><img src="https://1368568567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fw1XgQJc40kVeGUIxgI7c%2Fuploads%2FuoxkgZtgEcvFABYbNQsL%2FAggregator%20APIv2-APIv1.drawio%20(3).png?alt=media&amp;token=38c91ca4-d126-4491-8ba4-715be0379cb4" alt=""><figcaption><p>APIv1 sequence diagram</p></figcaption></figure>

### APIv2 swap flow&#x20;

Route summary returned in `GET` response and encoded data returned in `POST` response.

<figure><img src="https://1368568567-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fw1XgQJc40kVeGUIxgI7c%2Fuploads%2Ft6sWhxvueI5W7Ji1t9xD%2FAggregator%20APIv2-APIv2.drawio.png?alt=media&amp;token=61aba1dd-2bff-4ee0-84da-5484ad51ad27" alt=""><figcaption><p>APIv2 sequence diagram</p></figcaption></figure>

## Field mappings

As part of this upgrade, some of the parameters have also been modified to make it more intuitive. The table below maps the relevant changes for avoidance of doubt.

* APIv1 `GET`: [`/{chain}/route/encode`](/~/changes/1kQEhDNzHMDf7SwOE6Yu/kyberswap-solutions/kyberswap-aggregator/aggregator-api-specification/evm-swaps.md#chain-route-encode)
* APIv2 `GET`: [`/{chain}/api/v1/routes`](/~/changes/1kQEhDNzHMDf7SwOE6Yu/kyberswap-solutions/kyberswap-aggregator/aggregator-api-specification/evm-swaps.md#chain-api-v1-routes)
* APIv2 `POST`: [`/{chain}/api/v1/route/build`](/~/changes/1kQEhDNzHMDf7SwOE6Yu/kyberswap-solutions/kyberswap-aggregator/aggregator-api-specification/evm-swaps.md#chain-api-v1-route-build)

### Request

| APIv1 GET         | APIv2 GET       | APIv2 POST              |
| ----------------- | --------------- | ----------------------- |
| tokenIn\*         | tokenIn\*       | -                       |
| tokenOut\*        | tokenOut\*      | -                       |
| amountIn\*        | amountIn\*      | -                       |
| saveGas           | saveGas         | -                       |
| slippageTolerance | -               | slippageTolerance       |
| isInBps           | isInBps         | -                       |
| chargeFeeBy       | chargeFeeBy     | -                       |
| feeReceiver       | feeReceiver     | -                       |
| feeAmount         | feeAmount       | -                       |
| deadline          | -               | deadline                |
| to\*              | -               | recipient\*             |
| clientData        | -               | source                  |
| -                 | includedSources | -                       |
| -                 | excludedSources | -                       |
| -                 | gasInclude      | -                       |
| -                 | gasPrice        | -                       |
| -                 | -               | routeSummary\*          |
| -                 | -               | sender\*                |
| -                 | -               | skipSimulateTransaction |

### Response

| APIv1 GET         | APIv2 GET         | APIv2 POST    |
| ----------------- | ----------------- | ------------- |
| inputAmount       | amountIn          | amountIn      |
| outputAmount      | amountOut         | amountOut     |
| totalGas          | gas               | gas           |
| gasPriceGwei      | gasPrice          |               |
| gasUsd            | gasUsd            | gasUsd        |
| amountInUsd       | amountInUsd       | amountInUsd   |
| amountOutUsd      | amountOutUsd      | amountOutUsd  |
| receiveUsd        | -                 | -             |
| swaps             | route             | -             |
| pool              | pool              | -             |
| tokenIn           | tokenIn           | -             |
| tokenOut          | tokenOut          | -             |
| swapAmount        | swapAmount        | -             |
| amountOut         | amountOut         | -             |
| limitReturnAmount | limitReturnAmount | -             |
| maxPrice          | -                 | -             |
| exchange          | exchange          | -             |
| poolLength        | poolLength        | -             |
| poolType          | poolType          | -             |
| tokens            | -                 | -             |
| address           | -                 | -             |
| symbol            | -                 | -             |
| name              | -                 | -             |
| price             | -                 | -             |
| decimals          | -                 | -             |
| encodedSwapData   | -                 | data          |
| routerAddress     | routerAddress     | routerAddress |
| -                 | code              | code          |
| -                 | message           | message       |
| -                 | data              | data          |
| -                 | routeSummary      | -             |
| -                 | extraFee          | -             |
| -                 | feeAmount         | -             |
| -                 | chargeFeeBy       | -             |
| -                 | isInBps           | -             |
| -                 | feeReceiver       | -             |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.kyberswap.com/~/changes/1kQEhDNzHMDf7SwOE6Yu/kyberswap-solutions/kyberswap-aggregator/developer-guides/upgrading-from-apiv1-to-apiv2.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
