API Docs

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";

const USDB_ADDRESS = "0x4200000000000000000000000000000000000022";

// Enable BigInt serialization to JSON format
BigInt.prototype.toJSON = function () {
  return this.toString();
};

// Initialize wallet client with specified chain and transport
const walletClient = createWalletClient({
  chain: blastSepolia,
  transport: http(),
});

const showCaseRocketSwap = async () => {
  // Convert private key to account format
  const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`);
  // Define swap amount
  const swapAmount = parseEther("0.01");

  // Request swap route from RocketSwap API
  const response = await axios.post("https://swap.rocketbot.xyz/api/getSwapRoute", {
    amountIn: swapAmount.toString(),
    outputToken: USDB_ADDRESS,
  });

  const { data: route } = response;

  // Request swap transaction data from RocketSwap API
  const response2 = await axios.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 transaction
  const request = await walletClient.prepareTransactionRequest({
    account,
    to: routerAddress as `0x${string}`,
    value: swapAmount,
    data: swapTxData as `0x${string}`,
  });

  const signature = await walletClient.signTransaction(request);

  // Send the transaction and log the transaction hash
  const hash = await walletClient.sendRawTransaction({ serializedTransaction: signature });
  console.log(`https://testnet.blastscan.io/tx/${hash}`);
};

showCaseRocketSwap();

Last updated