The JupiterSwapApiClient provides high-level methods to interact with the Jupiter Swap API. You initialize it with a base URL (e.g., https://quote-api.jup.ag/v6).
Key methods:
quote(&QuoteRequest): Fetches a quote for a potential swap. Requires an amount, input_mint, output_mint, and slippage_bps.swap(&SwapRequest): Executes a swap request, returning a transaction. Requires user_public_key, the quote_response, and a TransactionConfig.swap_instructions(&SwapRequest): Retrieves swap instructions instead of a full transaction.
use jupiter_swap_api_client::{
quote::QuoteRequest, swap::SwapRequest, transaction_config::TransactionConfig,
JupiterSwapApiClient,
};
use solana_sdk::pubkey::Pubkey;
const USDC_MINT: Pubkey = pubkey!("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
const NATIVE_MINT: Pubkey = pubkey!("So11111111111111111111111111111111111111112");
const TEST_WALLET: Pubkey = pubkey!("2AQdpHJ2JpcEgPiATUXjQxA8QmafFegfQwSLWSprPicm");
#[tokio::main]
async fn main() {
let jupiter_swap_api_client = JupiterSwapApiClient::new("https://quote-api.jup.ag/v6");
let quote_request = QuoteRequest {
amount: 1_000_000,
input_mint: USDC_MINT,
output_mint: NATIVE_MINT,
slippage_bps: 50,
..QuoteRequest::default()
};
// GET /quote
let quote_response = jupiter_swap_api_client.quote("e_request).await.unwrap();
println!("{quote_response:#?}");
// POST /swap
let swap_response = jupiter_swap_api_client
.swap(&SwapRequest {
user_public_key: TEST_WALLET,
quote_response: quote_response.clone(),
config: TransactionConfig::default(),
})
.await
.unwrap();
println!("Raw tx len: {}", swap_response.swap_transaction.len());
// POST /swap-instructions
let swap_instructions = jupiter_swap_api_client
.swap_instructions(&SwapRequest {
user_public_key: TEST_WALLET,
quote_response,
config: TransactionConfig::default(),
})
.await
.unwrap();
println!("{swap_instructions:#?}");
}