https://betastation.jup.ag/docs/apis/swap-api
- User
- Assistant
The Jupiter Swap API provides developers with a streamlined method to access liquidity on the Solana blockchain. By specifying the desired token pairs, amount, and slippage, the API returns serialized transactions ready for execution on the Solana network[1].
Key Features:
-
Versioned Transactions and Address Lookup Tables: The API utilizes versioned transactions and address lookup tables to optimize swap routes into a single transaction. However, since not all wallets support versioned transactions, it's essential to check wallet compatibility and use the
asLegacyTransactionparameter when necessary[1]. -
Self-Hosted API Option: For mission-critical applications, such as liquidations and oracles, developers can deploy their own Jupiter Swap API servers. This setup allows for full control over infrastructure and eliminates reliance on public API rate limits. A dedicated Solana RPC node with access to the Yellowstone gRPC plugin is recommended for this configuration[2].
Integration Guide:
-
Install Required Libraries: Ensure you have Node.js (version 16 or higher) installed. Then, install the necessary libraries:
npm install @solana/web3.js cross-fetch @project-serum/anchor bs58 -
Set Up Connection and Wallet: Import the libraries and establish a connection to the Solana network. It's recommended to use your own RPC endpoint:
import { Connection, Keypair, VersionedTransaction } from '@solana/web3.js'; import fetch from 'cross-fetch'; import { Wallet } from '@project-serum/anchor'; import bs58 from 'bs58'; const connection = new Connection('https://your-rpc-endpoint/'); const wallet = new Wallet(Keypair.fromSecretKey(bs58.decode(process.env.PRIVATE_KEY || ''))); -
Obtain a Swap Quote: Request a quote for swapping SOL to USDC with a specified amount and slippage:
const quoteResponse = await ( await fetch('https://quote-api.jup.ag/v6/quote?inputMint=So11111111111111111111111111111111111111112&outputMint=EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v&amount=100000000&slippageBps=50') ).json(); -
Generate Swap Transaction: Convert the quote into a serialized transaction:
const { swapTransaction } = await ( await fetch('https://quote-api.jup.ag/v6/swap', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ quoteResponse, userPublicKey: wallet.publicKey.toString(), wrapAndUnwrapSol: true, }) }) ).json(); -
Sign and Execute the Transaction: Deserialize, sign, and send the transaction to the Solana network:
const swapTransactionBuf = Buffer.from(swapTransaction, 'base64'); const transaction = VersionedTransaction.deserialize(swapTransactionBuf); transaction.sign([wallet.payer]); const latestBlockHash = await connection.getLatestBlockhash(); const rawTransaction = transaction.serialize(); const txid = await connection.sendRawTransaction(rawTransaction, { skipPreflight: true, maxRetries: 2 }); await connection.confirmTransaction({ blockhash: latestBlockHash.blockhash, lastValidBlockHeight: latestBlockHash.lastValidBlockHeight, signature: txid }); console.log(`Transaction successful: https://solscan.io/tx/${txid}`);
Priority Fees:
To enhance transaction confirmation during network congestion, you can set a priority fee by including the
computeUnitPriceMicroLamportsparameter:const transaction = await ( await fetch('https://quote-api.jup.ag/v6/swap', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ quoteResponse, userPublicKey: wallet.publicKey.toString(), dynamicComputeUnitLimit: true, prioritizationFeeLamports: 'auto' // Automatically sets a priority fee, capped at 0.005 SOL }) }) ).json();For comprehensive details and additional examples, refer to the official Jupiter Swap API documentation[1].
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.