Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

How a swap works: quote, swap, track

Implement a correct quoting flow

A key part of our service is the ability to include transaction data in a /swap response. After already implementing our /providers and their /tokens, you will use the /quote and /swap endpoints to process transactions for your users.

1. Initial Quote Request

First, fetch quotes with the /v3/quote endpoint. For this step, "sourceAddress" and "destinationAddress" are not needed and instead you are just quoting the price of a swap:

curl -X POST "https://api.swapkit.dev/v3/quote" \
  -H "Content-Type: application/json" \
  -H "x-api-key: YOUR_API_KEY" \
  -d '{
    "sellAsset": "BTC.BTC",
    "buyAsset": "ETH.ETH",
    "sellAmount": "0.1",
    "slippage": 3
  }'

You can filter for the providers you have integrated using the "providers" argument if you want to limit the options shown.

This returns a regular quote response including pricing information, estimated timing and fees, and route details with the available providers, but without transaction data. The "nextActions" object will inform you of the following steps to take. Generally, you will provide the routeId into the /swap endpoint.

2. Identify the Provider route you want to use

Once you have presented a price to the user, they would then accept it. This determines which provider offers the best match for the route you quoted. The available providers are labeled with the "RECOMMENDED", "FASTEST" and "CHEAPEST" tags. This is also where you drop routes your wallet can't sign — filter on each route's txHint before moving to step 3.

Then, before presenting the user with a transaction to sign, you will call the /swap endpoint, referencing the chosen quote via its routeId.

3. Request a /swap - Transaction Building

When the user is ready to execute the swap, request the /v3/swap endpoint with the selected routeId along with the user's sourceAddress and destinationAddress

This endpoint can take a bit longer to reply. SwapKit builds the transaction payload, including fetching UTXOs and building PSBT when required, does a balance check and also performs address screening on every /swap call automatically.

The routeId is valid for 5 minutes. After it expires, a new quote is fetched using the original parameters before the swap request is processed.

The swap will include transaction-related fields under tx, which may be either an object or a string. It also includes the price, so you can display it to the user again before signing or compare it internally against the previous value before presenting it:

If SLIP-0024 signing is enabled for your API key, the response also includes a signed payload ready for verification.


Transaction Building Process

When calling the /swap endpoint, the system performs the following validations:

1. Balance Verification

SwapKit first checks if the source address has sufficient balance to cover the sellAmount. If insufficient funds are detected, an insufficientBalance error is thrown for the entire request.

2. Transaction Building

If the balance check passes, the system attempts to build the transaction. In certain edge cases, the wallet may have enough balance to match the sellAmount, but insufficient funds to cover network fees. Since different providers have varying transaction sizes, some may return valid transaction data while others might fail with an unableToBuildTransaction error.

3. Successful Response

If all validations pass, you will receive a complete route with transaction data that can be directly signed and broadcasted.

Key Fields

  • targetAddress The deposit address where funds should be sent, or the address of the contract that should be called.

  • inboundAddress The address monitoring for incoming transactions.

  • tx The transaction data ready for signing.

  • txType The format of the transaction data (e.g., "PSBT" for Bitcoin or "EVM" for EVM chains).

Error Handling

Be prepared to handle the different errors that can be returned. You can find more details on the swap endpoint page.

  • insufficientBalance The source address doesn't have enough tokens for the swap.

  • insufficientAllowance: The source address doesn't have enough tokens approved for the contract interaction. Relevant for tokens in EVM networks.

  • unableToBuildTransaction The wallet has sufficient tokens but insufficient funds for network fees.

This is an example error for a wallet without enough balance:

Rate limits and trade size

Your API key carries two ceilings: requests per minute, and the USD value of a single trade. Both are set per key, and both are raised on request — in the partner dashboard under App > Settings, or by messaging us in your shared group with the SwapKit team.

Request limits are per minute, per key, per service and per region — each counts separately, so spending your quote budget leaves /tokens untouched, and a key spread across regions gets more total throughput than its number suggests. A new or unverified key starts at 10 requests per minute on each service.

Every response carries your standing, not only the ones that fail:

Header
Meaning

x-ratelimit-limit

Your limit on this service, per minute.

x-ratelimit-remaining

Requests left in the current window.

x-ratelimit-reset

Seconds until the window resets.

retry-after

Seconds to wait. Present on a 429.

Over the limit is 429 rateLimitExceeded. Wait retry-after — retrying sooner spends the budget you're waiting for — and watch x-ratelimit-remaining to slow down before you get there. If the limiter itself can't be reached it lets requests through, so a request that succeeded is not proof you were under the limit.

A trade over your key's USD ceiling, $1,000 by default, is rejected with 403 swapSizeExceeded. That is not a route's own size ceiling, which belongs to the route and drops it silently: if a large trade errors it's your key's limit, if it quietly returns fewer routes it's the route's.

Last updated