A resilient helper for building Jupiter swap transactions that automatically probes Ultra → Quote v6 → Lite endpoints, cools down rate-limited URLs, and can build both buy and sell legs with a single call.
- Endpoint selection: probes Ultra first, then Quote v6, then Lite, with cooldown handling.
- Rate-limit awareness: cools endpoints on 429s and retries with backoff.
- Optional Nozomi/Temporal API key injection (if configured via env).
- Buy & sell builders: one call can return both directions.
- Legacy or v0 transaction support (auto-deserializes base64).
npm install @solana/web3.js node-fetchimport { Connection, Keypair } from "@solana/web3.js";
import { buildBuySellTransactions } from "./jupiter.js"; // adjust path as needed
const connection = new Connection("https://api.mainnet-beta.solana.com", "confirmed");
const signer = Keypair.fromSecretKey(/* your secret key Uint8Array */);
const inputMint = "So11111111111111111111111111111111111111112"; // SOL (wrapped)
const outputMint = "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"; // USDT
const { buyTx, sellTx, buyOrder, sellOrder } = await buildBuySellTransactions({
inputMint,
outputMint,
buyAmountNative: 1_000_000, // lamports or token native units
sellAmountToken: 0, // set if you want a sell leg too
slippage: 1, // percent → converted to bps internally
signerKeypair: signer,
connection,
forceLegacy: false,
});
// Sign/serialize/send buyTx or sellTx as needed:
// const sig = await connection.sendTransaction(buyTx, [signer], { skipPreflight: false });ULTRA_API_KEY: Jupiter Ultra API key (addsJUP-API-KEY/x-api-keyheaders).JUP_ENDPOINT_COOLDOWN_MS: override per-endpoint cooldown after 429 (default 6000).- Optional Nozomi/Temporal passthrough:
USE_NOZOMI(true/false),NOZOMI_API_KEY/TEMPORAL_API_KEYNOZOMI_ORDER_URL/NOZOMI_ENDPOINT/TEMPORAL_ORDER_URL/TEMPORAL_ENDPOINT
- Endpoint choice is cached unless cooled down; logs will indicate Ultra/QuoteV6/Lite.
- Amounts are expected in native units (e.g., lamports for SOL, token base units).
slippageis provided as percent; code converts to basis points.- For signing, recent blockhash and fee payer are applied automatically before partial sign.
MIT (see root LICENSE).