How to execute a swap
Last updated
Last updated
In this section, you'll learn how to request ready-to-use transaction data for executing a token swap on Solana.
As in the section, you can choose either the explicit path or the protocol-agnostic (less explicit) path to get the transaction data.
Use this when you want to work with a specific Solana AMM protocol (e.g., Raydium, Orca).
Request Transaction Data Fetch specific protocol information using:
Required Parameters:
action
Type of operation
swap
protocol
Protocol name
raydium
fromToken
Token mint to swap from
So11111111111111111111111111111111111111112
(Wrapped SOL)
toToken
Token mint to swap to
EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
(USDC)
amountIn
Input amount in lamports
1000000000
(1 SOL)
minAmountOut
Minimum output with slippage
25000000
(25 USDC)
slippage
Maximum price slippage
0.5
(0.5% slippage tolerance)
userPubkey
User's wallet public key
7xKXtg2CW87d97TXJSDpbD5jBkheTqA83TZRuJosgAsU
Example Request:
Response:
Let our system automatically select the optimal AMM protocol for your swap.
Use the Generic Endpoint
Use the same parameters as above, excluding protocol
.
Example Request:
Execute the Transaction
The transaction needs to be signed and submitted to the Solana network. This happens on your end - we don't touch your funds, custody remains yours.
Here's a basic example using @solana/web3.js:
After executing the swap, you can:
Always ensure you have enough SOL for transaction fees
Check that you have Associated Token Accounts (ATAs) created for the tokens
The API will automatically handle ATA creation if needed
Transaction data includes all necessary setup instructions (ATA creation, token approvals)
Slippage protection is crucial due to Solana's fast block times