Skip to navigation

Get Started

View as Markdown

The example code does everything on this page with Solana Kit: src/swap.ts has one function per step. With only an API key it quotes and simulates a swap; add a private key to send one.

1. Request swap instructions

Send a POST request to /solana/swap-instructions.

const response = await fetch("https://api.0x.org/solana/swap-instructions", {
method: "POST",
headers: { "0x-api-key": API_KEY, "Content-Type": "application/json" },
body: JSON.stringify({
token_in: "So11111111111111111111111111111111111111111", // native SOL
token_out: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", // USDC
amount_in: 1_000_000, // 0.001 SOL, in lamports
taker: takerAddress, // the wallet that signs the swap
slippage_bps: 50,
transaction_version: "v1",
}),
});
  • Tokens are mint addresses and amounts are in base units. So11111111111111111111111111111111111111111 is native SOL; So11111111111111111111111111111111111111112 is the wSOL mint.
  • The response contains the instructions to execute, in order, the expected amount_out, and min_amount_out, the least the taker receives after slippage.

See the API Reference for every field.

2. Build, sign, and send the transaction

  1. Convert the instructions to your library’s format, keeping their order. Addresses and data are byte arrays.
  2. Create a version 1 transaction message with the taker as fee payer.
  3. Set the compute unit limit and the loaded accounts data size limit. Simulate the transaction to measure both, and add a margin of about 10%. A v1 transaction that does not set these limits has limits of zero and fails.
  4. Optionally set a priority fee. In v1 it is the total fee in lamports, not a price per compute unit.
  5. Have the taker sign, then send.

These steps build a version 1 transaction, which the user’s wallet and your Solana library must support. Solana Kit 8.5 or later does; @solana/web3.js 1.x does not.

Tips for landing swaps

  • Request instructions just before the user signs. Pool prices move every slot. A quote that is several seconds old is more likely to fail on min_amount_out.
  • Set a priority fee when the network is busy.
  • Do not resend an expired transaction. If its blockhash expires before it lands, request new instructions and build a new transaction.

Adding your own instructions

If you add instructions to the transaction (a transfer, a tip, a memo), set reserve_transaction_accounts to the number of accounts they add that the swap does not already use. Keep it as small as you can: every reserved account is one the route cannot use. Check the size of the final transaction before you send it.

v0 transactions

Set transaction_version: "v0" only for a wallet that cannot sign v1 transactions yet. v0 transactions are smaller, so large trades get worse prices. Load the returned address_lookup_tables when you compile the transaction, and add ComputeBudget instructions yourself. Reserve room for your own instructions with reserve_transaction_bytes.