RocketSwap API is free to use and currently does not have any limits.
/getSwapRoute
Used to get a quota for a swap. It's a post request that takes two or three variables. amountIn which is required. And then inputToken, outputToken, or both.
It then returns a route if found.
/getSwapTx
After retrieving the route, the transactions needed to execute on chain can be fetched using this function. It will return various useful data. Most notable swapTxData which can be signed and sent directly to the chain.
Check the API Documentation below to see a list of all the things returned.
Sample Code
In the code snippet below, it can be seen how the Swap API can be used in TypeScript using Axios and Viem.
To run the test code, you need a small amount of test ETH. You can get that from one of the many faucets available. https://docs.blast.io/tools/faucets
import { privateKeyToAccount } from"viem/accounts";import axios from"axios";import { createWalletClient, http, parseEther } from"viem";import { blastSepolia } from"viem/chains";constUSDB_ADDRESS="0x4200000000000000000000000000000000000022";// Enable BigInt serialization to JSON formatBigInt.prototype.toJSON=function () {returnthis.toString();};// Initialize wallet client with specified chain and transportconstwalletClient=createWalletClient({ chain: blastSepolia, transport:http(),});constshowCaseRocketSwap=async () => {// Convert private key to account formatconstaccount=privateKeyToAccount(process.env.PRIVATE_KEYas`0x${string}`);// Define swap amountconstswapAmount=parseEther("0.01");// Request swap route from RocketSwap APIconstresponse=awaitaxios.post("https://swap.rocketbot.xyz/api/getSwapRoute", { amountIn:swapAmount.toString(), outputToken:USDB_ADDRESS, });const { data: route } = response;// Request swap transaction data from RocketSwap APIconstresponse2=awaitaxios.post("https://swap.rocketbot.xyz/api/getSwapTx", { route, slippage:5, recipient:account.address, }, { headers: {"Content-Type":"application/json","Accept-Encoding":"gzip, deflate", }, });const { swapTxData,routerAddress } =response2.data;// Prepare and sign the transactionconstrequest=awaitwalletClient.prepareTransactionRequest({ account, to: routerAddress as`0x${string}`, value: swapAmount, data: swapTxData as`0x${string}`, });constsignature=awaitwalletClient.signTransaction(request);// Send the transaction and log the transaction hashconsthash=awaitwalletClient.sendRawTransaction({ serializedTransaction: signature });console.log(`https://testnet.blastscan.io/tx/${hash}`);};showCaseRocketSwap();