🧬
Launch App
English
English
  • Say Hello to Raydium
  • Getting Started
    • How-to guides
    • SPL Wallet
      • Buying and selling digital assets
      • Bridging assets
    • Best practices
    • Welcome to UI V3
    • FAQ
  • Traders
    • Swapping
      • Swap FAQ
    • Blinks and Referrals
    • Trade API
    • Raydium Perps
      • Trading basics
      • Order types
      • Trading fees
  • Liquidity providers
    • Providing Concentrated Liquidity (CLMM)
      • Intro on Concentrated Liquidity
      • Estimated APR Calculations
      • Position creation cost (CLMM)
    • Providing Constant Product liquidity
      • Farms
    • Liquidity providing FAQ
  • POOL CREATION
    • LaunchLab
      • Create a Token
      • Buying a LaunchLab token
      • Earn referral fees!
      • Creator fee share
      • Platforms
      • LaunchLab TypeScript SDK
    • Pool types overview
    • Creating a CLMM Pool and Farm
    • Creating a Constant Product Pool
      • Creating an Ecosystem Farm
    • Burn & Earn
    • Pool creation fees
    • Pool creation FAQ
  • Protocol
    • RAY TOKEN
      • RAY Staking
      • Protocol Fees
      • RAY Buybacks
    • Developers
      • Addresses
      • APIs
    • Security
      • Access Controls
    • Bug Bounty Program
      • CLMM Bug Bounty Details
      • Hybrid AMM Bug Bounty Details
      • CPMM (CP-Swap) Bug Bounty Details
    • Protocol Metrics & Analytics
  • Media assets
  • updates
    • Token-2022 Support
    • Archive
      • Staking History Tool
      • V3 LPs migrated to V4
      • Serum DEX3 upgrade
      • Associated Token Account Migration
      • Serum DEX Deprecation
      • Transparency on Exploit Compensation Funds
      • Claim Portal
      • Creating an OpenBook AMM Pool
        • Creating an Ecosystem Farm
      • Integration with OpenBook
      • Acceleraytor
  • Learn more
    • Discord
    • Telegram
Powered by GitBook
On this page
  • Get quote parameters
  • Post parameters
  • Guide for Raydium Trade API
  • Setting priority fee

Was this helpful?

  1. Traders

Trade API

Raydium trade API is the fastest and easiest way to interact with Raydium liquidity. It allows you to swap for any asset with 2 requests and a signature.

PreviousBlinks and ReferralsNextRaydium Perps

Last updated 2 months ago

Was this helpful?

Get quote parameters

Parameter
Type
Required
Description

inputMint

string

yes

Input token mint address

outputMint

string

yes

Output token mint address

amount

number

yes

Either inputAmount or outpoutAmount depending on the swap mode. You need to account for decimals (x10^n) eg. To swap 1 tokenA, where tokenA has 6 decimals, you need to input 1 * 10^6 or 1,000,000.

slippageBps

number

yes

Slippage tolerance in base points (0.01%).

Post parameters

Parameter
Type
Required
Description

txversion

string

yes

Use 'V0' for versioned transaction, and 'LEGACY' for legacy transaction.

wrapSol

boolean

no

Need to be true to accept SOL as inputToken.

unwrapSol

boolean

no

Need to set to true to unwrap wSol received as outputToken.

computeUnitPriceMicroLamports

string

yes

You can type it in manually or use Raydium priority fee API to set an automatic amount with 'String(data.data.default.h)'. The 'h' here stands for high priority. 'vh' for very high and 'm' for medium are also accepted value.

wallet

string

yes

pubkey

inputAccount

string

yes

account always needs to be passed if inputToken ≠ SOL

outputAccount

string

no

default to ATA

swapResponse

string

yes

computed by the API, no modification needed.

Guide for Raydium Trade API

You can follow the in sdk v2 demo.

  1. Installation

$ yarn add @raydium-io/raydium-sdk-v2
  1. Import libraries

import { Transaction, VersionedTransaction, sendAndConfirmTransaction } from '@solana/web3.js'
import { NATIVE_MINT } from '@solana/spl-token'
import axios from 'axios'
import { connection, owner, fetchTokenAccountData } from '../config'
import { API_URLS } from '@raydium-io/raydium-sdk-v2'

You can paste in a burner private key for testing purposes but it's not recommended for production.

export const owner: Keypair = Keypair.fromSecretKey(bs58.decode('<YOUR_WALLET_SECRET_KEY>'))
export const connection = new Connection('<YOUR_RPC_URL>') //<YOUR_RPC_URL>
  1. Get quote (https://transaction-v1.raydium.io/$) & and define the swap type.

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}`
  ) // Use the URL xxx/swap-base-in or xxx/swap-base-out to define the swap type. 
 

'BaseOut' swaps will get you a quote for the ExactOut amount of token received. In this mode, slippage is inputted to the base token.

  1. Serialize (https://transaction-v1.raydium.io/$)

 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(data.data.default.h),
    swapResponse,
    txVersion,
    wallet: owner.publicKey.toBase58(),
    wrapSol: isInputSol,
    unwrapSol: isOutputSol, // true means output mint receive sol, false means output mint received wsol
    inputAccount: isInputSol ? undefined : inputTokenAcc?.toBase58(),
    outputAccount: isOutputSol ? undefined : outputTokenAcc?.toBase58(),
  })
  1. Deserialize

  const allTxBuf = swapTransactions.data.map((tx) => Buffer.from(tx.transaction, 'base64'))
  const allTransactions = allTxBuf.map((txBuf) =>
    isV0Tx ? VersionedTransaction.deserialize(txBuf) : Transaction.from(txBuf)
  )

  console.log(`total ${allTransactions.length} transactions`, swapTransactions)
  1. Sign and execute

  let idx = 0
  if (!isV0Tx) {
    for (const tx of allTransactions) {
      console.log(`${++idx} transaction sending...`)
      const transaction = tx as Transaction
      transaction.sign(owner)
      const txId = await sendAndConfirmTransaction(connection, transaction, [owner], { skipPreflight: true })
      console.log(`${++idx} transaction confirmed, txId: ${txId}`)
    }
  } else {
    for (const tx of allTransactions) {
      idx++
      const transaction = tx as VersionedTransaction
      transaction.sign([owner])
      const txId = await connection.sendTransaction(tx as VersionedTransaction, { skipPreflight: true })
      const { lastValidBlockHeight, blockhash } = await connection.getLatestBlockhash({
        commitment: 'finalized',
      })
      console.log(`${idx} transaction sending..., txId: ${txId}`)
      await connection.confirmTransaction(
        {
          blockhash,
          lastValidBlockHeight,
          signature: txId,
        },
        'confirmed'
      )
      console.log(`${idx} transaction confirmed`)

Setting priority fee

You'll need to get historical data if you'd like to optimize your priority using Raydium API.

  // get statistical transaction fee from API
  /**
   * vh: very high
   * h: high
   * m: medium
   */
  const { data } = await axios.get<{
    id: string
    success: boolean
    data: { default: { vh: number; h: number; m: number } }
  }>(`${API_URLS.BASE_HOST}${API_URLS.PRIORITY_FEE}`)

Then, set computeUnitPriceMicroLamports to one of the default tier.

 }>(`${API_URLS.SWAP_HOST}/transaction/swap-base-in`, {
    computeUnitPriceMicroLamports: String(data.data.default.h) // or custom lamport number.

Set up your RPC connection and wallet in a config.ts file following .

If you are looking for a more integrated solution check Raydium SDK here:

Need help troubleshooting for errors? feel free to ask questions to our dev community on .

demo implementation
this template
https://github.com/raydium-io/raydium-sdk-V2/tree/master
Discord