Raydium trade API is the fastest and easiest way to interact with Raydium liquidity. It allows you to swap for any asset with 2 requests and a signature.
Get quote parameters
Parameter
Type
Required
Description
inputMint
string
yes
Input token mint address
outputMint
string
yes
Output token mint address
amount
number
yes
Either inputAmount or outpoutAmount depending on the swap mode.
slippageBps
number
yes
Slippage tolerance in base points (0.01%).
Post parameters
Parameter
Type
Required
Description
version
string
yes
Use 'V0' for versioned transaction, and 'LEGACY' for legacy transaction.
wrapSol
boolean
no
Need to be true to accept SOL as inputToken.
unwrapSol
boolean
no
Need to set to true to unwrap wSol received as outputToken.
computeUnitPriceMicroLamports
string
yes
You can type it in manually or use Raydium priority fee API to set an automatic amount with 'String(data.data.default.h)'. The 'h' here stands for high priority.
'vh' for very high and 'm' for medium are also accepted value.
Get quote (https://transaction-v1.raydium.io/$) & and define the swap type.
const { data: swapResponse } =awaitaxios.get<SwapCompute>(`${API_URLS.SWAP_HOST}/compute/swap-base-in?inputMint=${inputMint}&outputMint=${outputMint}&amount=${amount}&slippageBps=${ slippage *100}&txVersion=${txVersion}` ) // Use the URL xxx/swap-base-in or xxx/swap-base-out to define the swap type.
'BaseOut' swaps will get you a quote for the ExactOut amount of token received.
In this mode, slippage is inputted to the base token.
Serialize (https://transaction-v1.raydium.io/$)
const { data: swapTransactions } =awaitaxios.post<{ id:string version:string success:boolean data: { transaction:string }[] }>(`${API_URLS.SWAP_HOST}/transaction/swap-base-in`, { computeUnitPriceMicroLamports:String(data.data.default.h), swapResponse, txVersion, wallet:owner.publicKey.toBase58(), wrapSol: isInputSol, unwrapSol: isOutputSol,// true means output mint receive sol, false means output mint received wsol inputAccount: isInputSol ?undefined:inputTokenAcc?.toBase58(), outputAccount: isOutputSol ?undefined:outputTokenAcc?.toBase58(), })
You'll need to get historical data if you'd like to optimize your priority using Raydium API.
// get statistical transaction fee from API/** * vh: very high * h: high * m: medium */const { data } =awaitaxios.get<{ id:string success:boolean data: { default: { vh:number; h:number; m:number } } }>(`${API_URLS.BASE_HOST}${API_URLS.PRIORITY_FEE}`)
Then, set computeUnitPriceMicroLamports to one of the default tier.
}>(`${API_URLS.SWAP_HOST}/transaction/swap-base-in`, { computeUnitPriceMicroLamports:String(data.data.default.h) // or custom lamport number.