GET /swap

Update JULY 9

New markets added, Moonshot, Orca and Raydium CPMM, new Swap API URL, speed improvements and more.

API now supports: Moonshot (opens in a new tab), Pump.fun (opens in a new tab) tokens, Raydium, Raydium CPMM, Orca and any token supported by Jupiter.

curl --location 'https://swap-v2.solanatracker.io/swap?from=So11111111111111111111111111111111111111112&to=4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R&fromAmount=1&slippage=10&payer=PAYER_ADDRESS'

Request Parameters

The cURL request requires the following parameters in the URL:

  • from: The base token address. In this case, it's the Solana token address (So11111111111111111111111111111111111111112).
  • to: The quote token address. In this case, it's the RAY token address (4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R).
  • fromAmount: The amount of the base token to convert. In this case, it's 1 which means 1 SOL (or "auto" which will use the full amount of a token that's in the wallet.)
  • slippage: The maximum acceptable slippage percentage. In this case, it's 10.
  • payer: The public key of the wallet that will send this transaction

Optional

  • priorityFee: Increase your chances of faster transaction processing by setting a higher priority fee. Value is amount in SOL.
  • txVersion: "v0" or "legacy" By default it returns a legacy transction. Setting it to v0 will return a versioned transaction.
  • fee: Charge a fee for each transaction by adding the fee parameter. ?fee=WALLET_ADDRESS_HERE:PERCENTAGE, example: fee=CN8w8cuh87gZYjhpaPxbD78GTrBmUKmVysqgsC9uYu9i:0.5

Example Response

{
"txn": "BASE64_TX",
"rate": {
"amountIn": 1,
"amountOut": 81.631985,
"minAmountOut": 73.4687865,
"currentPrice": 0.012219167460548153,
"executionPrice": 0.010997250714493338,
"priceImpact": 0.002517,
"fee": 0.000005,
"baseCurrency": {
"mint": "So11111111111111111111111111111111111111112",
"decimals": 9
},
"quoteCurrency": {
"mint": "4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R",
"decimals": 6
},
"platformFee": 9000000,
"platformFeeUI": 0.009
},
"timeTaken": 0.016,
"type": "legacy"
}

The response includes the following information

Transaction Details

  • txn: The serialized transaction , encoded in a base64 string.
  • type: What the transaction version is.

How to load the transaction

// res is the response from the api
 
const serializedTransactionBuffer = Buffer.from(res.txn, "base64");
let txn = false;
if (res.type === 'v0') {
  // Deserialize the versioned transaction
  txn = VersionedTransaction.deserialize(serializedTransactionBuffer);
} else {
  // Deserialize the transaction
  txn = Transaction.from(serializedTransactionBuffer);
}
 
if (!txn) return false;

How to send the transaction using React Wallet Adapter?

if (res.type === 'v0' && txn) {
  await wallet.signTransaction(txn);
}
const txid = await wallet.sendTransaction(txn, connection, {
  skipPreflight: true,
  maxRetries: 4,
});

How to send the transaction in NodeJS?

 
// keypair is your private key which you can load like this
const keypair = Keypair.fromSecretKey(bs58.decode('YOUR SECRET KEY HERE'));
 
if (res.isJupiter) {
  const txn = VersionedTransaction.deserialize(
    serializedTransactionBuffer
  );
  txn.sign([keypair]);
 
  txid = await connection.sendRawTransaction(txn.serialize(), {
    skipPreflight: false,
    preflightCommitment: "singleGossip",
  });
} else {
  const txn = Transaction.from(serializedTransactionBuffer);
  txn.sign(keypair);
  const rawTransaction = txn.serialize();
  txid = await connection.sendRawTransaction(rawTransaction, {
    skipPreflight: false,
    preflightCommitment: "singleGossip",
  });
}

We charge a 0.9% fee on each successful transaction. Using this for a public bot or site with a high processing volume? Contact us via Discord or email ([email protected]) and get the fee reduced (down to 0.1%, depending on volume, only if accepted.)