To perform a swap, you must follow a two-step process: first, compute the swap details (price impact, route, etc.) using the /compute/swap-base-in endpoint, and then request the actual transaction data using the /transaction/swap-base-in endpoint.
Step 1: Compute Swap
Call the compute endpoint with the following parameters:
inputMint: The mint address of the token you are providing.outputMint: The mint address of the token you want to receive.amount: The amount of input token.slippageBps: Slippage expressed in basis points (e.g., 0.5% = 50).txVersion: Either 'V0' or 'LEGACY'.
Step 2: Request Transaction
Post to the transaction endpoint with a body containing:
computeUnitPriceMicroLamports: The priority fee (can be fetched from the API).swapResponse: The full response object received from the compute step.txVersion: The version used in the compute step.wallet: The public key of the owner.wrapSol: true if the input is SOL (to wrap it).unwrapSol: true if the output is SOL (to unwrap it).inputAccount: The token account address for the input mint (if not SOL).outputAccount: The token account address for the output mint (if not SOL).
Step 3: Sign and Send
Transactions are returned as base64 encoded strings. Deserialize them based on the txVersion (VersionedTransaction for V0, Transaction for LEGACY), sign with the owner keypair, and send via the Solana connection.
// 1. Compute the swap
const { data: swapResponse } = await axios.get<SwapCompute>(`${API_URLS.SWAP_HOST}/compute/swap-base-in?inputMint=${inputMint}&outputMint=${outputMint}&amount=${amount}&slippageBps=${slippage * 100}&txVersion=${txVersion}`);
// 2. Get the transaction
const { data: swapTransactions } = await axios.post<{ id: string; version: string; success: boolean; data: { transaction: string }[] }>(`${API_URLS.SWAP_HOST}/transaction/swap-base-in`, {
computeUnitPriceMicroLamports: String(priorityFee),
swapResponse,
txVersion,
wallet: owner.publicKey.toBase58(),
wrapSol: isInputSol,
unwrapSol: isOutputSol,
inputAccount: isInputSol ? undefined : inputTokenAcc?.toBase58(),
outputAccount: isOutputSol ? undefined : outputTokenAcc?.toBase58(),
});
// 3. Deserialize and Sign
const txBuf = Buffer.from(swapTransactions.data[0].transaction, 'base64');
const transaction = txVersion === 'V0' ? VersionedTransaction.deserialize(txBuf) : Transaction.from(txBuf);
transaction.sign(isV0Tx ? [owner] : owner);