Skip to main content
GET
/
kswap
/
swap
cURL
curl --request GET \
  --url https://api.kamino.finance/kswap/swap/
{
  "data": {
    "transaction": "<string>",
    "expectedAmountOut": "<string>",
    "minAmountOut": "<string>"
  },
  "traceId": "<string>"
}

Documentation Index

Fetch the complete documentation index at: https://kamino.com/docs/llms.txt

Use this file to discover all available pages before exploring further.

Get the best route and transaction for swapping between two tokens on Solana via Kamino’s swap aggregator.

Example

1

Import Dependencies

import {
  createSolanaRpc,
  createSolanaRpcSubscriptions,
  pipe,
  getTransactionDecoder,
  getCompiledTransactionMessageDecoder,
  decompileTransactionMessageFetchingLookupTables,
  setTransactionMessageFeePayerSigner,
  setTransactionMessageLifetimeUsingBlockhash,
  addSignersToTransactionMessage,
  signTransactionMessageWithSigners,
  sendAndConfirmTransactionFactory,
  getSignatureFromTransaction,
  isTransactionWithinSizeLimit,
} from '@solana/kit';
import { parseKeypairFile } from '@kamino-finance/klend-sdk/dist/utils/signer';
2

Configure and Initialize

const KEYPAIR_FILE = '/path/to/your/keypair.json';
const API_BASE_URL = 'https://api.kamino.finance';
const RPC_ENDPOINT = 'https://api.mainnet-beta.solana.com';
const WS_ENDPOINT = 'wss://api.mainnet-beta.solana.com';

const signer = await parseKeypairFile(KEYPAIR_FILE);
const rpc = createSolanaRpc(RPC_ENDPOINT);
const rpcSubscriptions = createSolanaRpcSubscriptions(WS_ENDPOINT);
3

Fetch Swap Transaction

Call the Kswap API to get the best route and a pre-built transaction. This example swaps 1 SOL to USDC with 50bps max slippage.
const SOL = 'So11111111111111111111111111111111111111112';
const USDC = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v';

const params = new URLSearchParams({
  tokenIn: SOL,
  tokenOut: USDC,
  amountIn: '1000000000', // 1 SOL in lamports
  maxSlippageBps: '50',
  wallet: signer.address,
});

const res = await fetch(`${API_BASE_URL}/kswap/swap/?${params}`);
const { data } = await res.json();

console.log(`Router: ${data.routerType}, Expected out: ${data.expectedAmountOut}`);

const encodedTransaction = data.transaction;
4

Decode, Sign, and Send

Decode the base64 transaction, sign it locally, and submit to the network.
const txBuffer = Buffer.from(encodedTransaction, 'base64');
const txMessageBytes = getTransactionDecoder().decode(txBuffer).messageBytes;
const compiledMessage = getCompiledTransactionMessageDecoder().decode(txMessageBytes);

const { value: latestBlockhash } = await rpc.getLatestBlockhash({
  commitment: 'finalized'
}).send();

const signedTransaction = await pipe(
  await decompileTransactionMessageFetchingLookupTables(compiledMessage, rpc),
  tx => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
  tx => setTransactionMessageFeePayerSigner(signer, tx),
  tx => addSignersToTransactionMessage([signer], tx),
  tx => signTransactionMessageWithSigners(tx)
);

if (!isTransactionWithinSizeLimit(signedTransaction)) {
  throw new Error('Transaction exceeds size limit');
}

const signature = getSignatureFromTransaction(signedTransaction);

await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(signedTransaction, {
  commitment: 'confirmed',
  skipPreflight: true,
});

console.log('Swap successful! Signature:', signature);

Query Parameters

tokenIn
string
required
tokenOut
string
required
amountIn
string
required
maxSlippageBps
number
required
wallet
string
required
includeSetupIxs
boolean
default:true
wrapAndUnwrapSol
boolean
default:true

Response

200 - application/json

Default Response

data
object
required
traceId
string