Get Started
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.
- Tokens are mint addresses and amounts are in base units.
So11111111111111111111111111111111111111111is native SOL;So11111111111111111111111111111111111111112is the wSOL mint. - The response contains the
instructionsto execute, in order, the expectedamount_out, andmin_amount_out, the least the taker receives after slippage.
See the API Reference for every field.
2. Build, sign, and send the transaction
- Convert the instructions to your library’s format, keeping their order. Addresses and data are byte arrays.
- Create a version 1 transaction message with the taker as fee payer.
- 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.
- Optionally set a priority fee. In v1 it is the total fee in lamports, not a price per compute unit.
- 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.