Solana Swap Instruction API Guide – Retrieve Transaction Instructions for DEX Trading

·

Integrating decentralized exchange (DEX) trading into your application or wallet? The Solana Swap Instruction API enables developers to programmatically retrieve fully assembled transaction instructions for token swaps across multiple liquidity sources. Whether you're building a custom trading interface, integrating DEX aggregation, or enabling cross-protocol routing, this API delivers secure, efficient, and flexible swap data tailored for the Solana blockchain.

This comprehensive guide walks through the endpoint, request parameters, response structure, and practical use cases—optimized for developers seeking reliable DEX API, swap API, and Solana transaction instruction integration.


How to Fetch Solana Swap Instructions

To retrieve transaction instructions for a token swap on Solana, make a GET request to the following endpoint:

GET https://web3.okx.com/api/v5/dex/aggregator/swap-instruction

This endpoint returns all necessary data to construct and sign a valid Solana transaction, including program IDs, account lists, instruction data, and routing details from integrated decentralized exchanges.


Required & Optional Request Parameters

Below is a breakdown of all available parameters you can pass when querying the API.

🔹 Mandatory Parameters

🔹 Optional Parameters

👉 Generate optimized swap instructions with real-time routing and slippage control


Understanding the Response Structure

The API returns a rich JSON object containing everything needed to build and execute a Solana transaction.

🔹 Core Response Fields

addressLookupTableAccount

Array of lookup tables used in modern Solana transactions to compress account lists and reduce transaction size—critical for high-performance dApps.

instructionLists

List of serialized instructions, each containing:

_routerResult – Swap Route Details

Contains full routing metadata:

Token Information Objects

Both _fromToken and _toToken include:

_quoteCompareList

Comparison of alternative routes by:

_tx

Transaction metadata:


Practical Use Case: Building a Custom Solana Swap UI

Imagine you're developing a non-custodial wallet that supports token swaps. By calling this API:

  1. User selects input/output tokens and enters amount.
  2. Your app calls the /swap-instruction endpoint with user wallet, slippage settings, and optional referral info.
  3. The API returns optimized route(s), fees, and complete instruction set.
  4. You display estimated output, price impact, and network cost.
  5. After confirmation, your app constructs the transaction using the returned instructions and signs it locally.

This ensures full decentralization while leveraging intelligent routing across top Solana DEXes.

👉 Access real-time Solana swap instructions with dynamic route optimization


FAQ: Common Developer Questions

Q: What is the difference between chainIndex and chainId?
A: Use chainIndex (e.g., 501 for Solana). The chainId parameter is deprecated and should not be used.

Q: How do I handle token decimals correctly?
A: Always multiply the human-readable amount by 10^decimals. For example, 1 USDC (6 decimals) = "1000000". You can retrieve decimal values via the token list API.

Q: Can I earn referral fees on Solana swaps?
A: Yes. Set feePercent and either fromTokenReferrerWalletAddress or toTokenReferrerWalletAddress. Note: The referrer address must hold SOL to be active on-chain.

Q: Why is my transaction failing even with correct instructions?
A: Common causes include insufficient SOL for fees, incorrect compute budget (computeUnitLimit), or expired lookup tables. Always validate account states before signing.

Q: Is positive slippage supported on all chains?
A: No. Only Solana currently supports capturing positive slippage via positiveSlippagePercent. On other chains, excess tokens go directly to the user.

Q: How does price impact protection work?
A: If the estimated price impact exceeds your specified percentage (e.g., 25%), the API returns an error. This helps avoid bad trades during high volatility.


Best Practices for Integration

Developers are encouraged to test in devnet environments before going live.

👉 Start integrating powerful DEX swap capabilities today


By leveraging the Solana Swap Instruction API, developers gain access to smart routing, secure transaction assembly, and monetization features—all essential for modern Web3 applications. With clean responses, robust safety checks, and support for affiliate models, this API streamlines DEX integration without compromising control or transparency.