Skip to content

Same-chain token swap

Swap one token for another on the same chain with a single API call. GET /build-best-path quotes the pair, ranks the candidates, and returns executable calldata for the top route in one round trip — no separate quote and build steps.

Use this when your integration executes immediately. If the user needs to review a route (output, fees, providers) before committing, use the two-step flow in End-to-end swap instead.

Prerequisites

  • An Edith API key, sent as an api-key header — Get API access.
  • A funded wallet on the target chain that can sign and broadcast transactions.
  • The input and output token addresses (find them via Search tokens).

1. Request the best path

Pass the same query parameters as GET /quote, with fromChain equal to toChain:

curl "https://vector.example.com/build-best-path?fromChain=84532&toChain=84532\
&fromToken=0x4200000000000000000000000000000000000006\
&toToken=0x036CbD53842c5426634e7929541eC2318f3dCF7e\
&amount=1000000000000000000&sender=0xYourWalletAddress&slippage=0.5" \
  -H "api-key: $EDITH_API_KEY"
const params = new URLSearchParams({
  fromChain: "84532",
  toChain: "84532",
  fromToken: "0x4200000000000000000000000000000000000006",
  toToken: "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
  amount: "1000000000000000000",
  sender: "0xYourWalletAddress",
  slippage: "0.5",
});
const res = await fetch(
  `https://vector.example.com/build-best-path?${params}`,
  { headers: { "api-key": process.env.EDITH_API_KEY! } },
);
const { data } = await res.json();
const tx = data.Tx; // note the capital T

The response bundles the transaction and the route it was built from:

{
  "code": 0,
  "data": {
    "Tx": {
      "allowanceTarget": "0x5b73C5498c1E3b4dbA84de0F1833c4a029d90519",
      "chain": 84532,
      "data": "0x8119c065…",
      "expiry": 1753791245,
      "gasLimit": "250000",
      "simulation": false,
      "to": "0x5b73C5498c1E3b4dbA84de0F1833c4a029d90519",
      "value": "0"
    },
    "route": {
      "quoteId": "9f8c1d2e…",
      "amountOut": "3421000000",
      "minAmountOut": "3403895000",
      "expiry": 1753791245,
      "routeKind": "sameChainSwap"
    }
  },
  "message": "success"
}

Warning

The transaction key is a capital Tx, not tx. The route object next to it is the same shape as a GET /quote result — useful for logging what was executed.

2. Approve and submit

  1. If the input token is an ERC-20, approve Tx.allowanceTarget to spend at least the input amount of fromToken.
  2. From the sender address, sign and broadcast the transaction using Tx.to, Tx.data, Tx.value, and Tx.gasLimit — before Tx.expiry (Unix seconds).

3. Track the result

curl "https://vector.example.com/status?txHash=0xYourTxHash&fromChain=84532" \
  -H "api-key: $EDITH_API_KEY"

data.status moves through pending and src_confirmed to completed or failed. See Get transaction status.

If no route exists

An unsupported pair — including any request where fromChain differs from toChain, since cross-chain planning currently returns no routes — fails with:

{ "code": 400, "data": null, "message": "no route for pair" }

Check both tokens exist on the chain via List tokens, and see Cross-chain swap for the state of cross-chain routing.

Next