Skip to main content

sdk.instructions()

Get raw swap instructions for composing custom transactions. Use this when you need to add custom instructions (tips, memos, etc.) or batch multiple swaps.
const ixs = await sdk.instructions(params: InstructionsRequest): Promise<InstructionsResponse>

Parameters (InstructionsRequest)

FieldTypeRequiredDefaultDescription
userWalletstringyesUser’s wallet address (base58)
inputMintstringyesInput token mint address
outputMintstringyesOutput token mint address
amountstringyesAmount in smallest token units
swapMode"ExactIn" | "ExactOut"yesSwap direction
slippageBpsnumberno50Slippage tolerance in basis points

Response (InstructionsResponse)

{
  "instructions": [
    {
      "programId": "ComputeBudget111111111111111111111111111111",
      "accounts": [],
      "data": "AgAAANCWAwA="
    },
    {
      "programId": "ArgyFuvk4S2vjC5XWuCWbcozjKwNrRzopLBJjCcTTWTH",
      "accounts": [
        { "publicKey": "...", "isSigner": true, "isWritable": true },
        { "publicKey": "...", "isSigner": false, "isWritable": false }
      ],
      "data": "base64-encoded-instruction-data"
    }
  ],
  "addressLookupTableAddresses": ["ALTaddress1...", "ALTaddress2..."],
  "amountIn": "1000000000",
  "amountOut": "145320000",
  "otherAmountThreshold": "144593400",
  "feeAmount": "100005",
  "hopCount": 1,
  "route": [
    "So11111111111111111111111111111111111111112",
    "uSd2czE61Evaf76RNbq4KPpXnkiL3irdzgLFUMe3NoG"
  ],
  "pools": ["HJPjoWUrhoZzkNfRpHuieeFk9WcZWjwy6PBjZ81ngndJ"]
}

Response Fields

FieldTypeDescription
instructionsRawInstruction[]Ordered list of Solana instructions
addressLookupTableAddressesstring[]ALT addresses to include in the v0 transaction
amountInstringInput amount
amountOutstringEstimated output amount
otherAmountThresholdstringSlippage-adjusted threshold
feeAmountstringAggregator fee in output token units
hopCountnumberNumber of swap hops
routestring[]Token mint path
poolsstring[]Pool addresses used

RawInstruction

FieldTypeDescription
programIdstringProgram ID (base58)
accountsRawAccountMeta[]Account metas
datastringBase64-encoded instruction data

RawAccountMeta

FieldTypeDescription
publicKeystringAccount public key (base58)
isSignerbooleanWhether the account must sign
isWritablebooleanWhether the account is writable

Building a Transaction

After receiving the instructions, you build the transaction yourself:
import {
  Connection,
  PublicKey,
  TransactionMessage,
  VersionedTransaction,
} from "@solana/web3.js";

const connection = new Connection("https://mainnet.fogo.io");

const ixs = await sdk.instructions({
  userWallet: wallet.publicKey.toBase58(),
  inputMint: "So11111111111111111111111111111111111111112",
  outputMint: "uSd2czE61Evaf76RNbq4KPpXnkiL3irdzgLFUMe3NoG",
  amount: "1000000000",
  swapMode: "ExactIn",
});

// Fetch blockhash
const { blockhash, lastValidBlockHeight } =
  await connection.getLatestBlockhash();

// Resolve Address Lookup Tables
const altAccounts = await Promise.all(
  ixs.addressLookupTableAddresses.map((addr) =>
    connection.getAddressLookupTable(new PublicKey(addr))
  )
);
const lookupTables = altAccounts
  .map((a) => a.value)
  .filter((v) => v !== null);

// Convert to web3.js instruction format
const instructions = ixs.instructions.map((ix) => ({
  programId: new PublicKey(ix.programId),
  keys: ix.accounts.map((acc) => ({
    pubkey: new PublicKey(acc.publicKey),
    isSigner: acc.isSigner,
    isWritable: acc.isWritable,
  })),
  data: Buffer.from(ix.data, "base64"),
}));

// Build v0 transaction
const messageV0 = new TransactionMessage({
  payerKey: wallet.publicKey,
  recentBlockhash: blockhash,
  instructions,
}).compileToV0Message(lookupTables);

const tx = new VersionedTransaction(messageV0);
tx.sign([wallet]);

const sig = await connection.sendTransaction(tx);

When to Use instructions() vs swap()

Use caseEndpoint
Simple swap, let the API handle everythingsdk.swap()
Add custom instructions (tip, memo, etc.)sdk.instructions()
Batch multiple swaps in one transactionsdk.instructions()
Inspect accounts and data before signingsdk.instructions()
Need lower latency (no blockhash fetch server-side)sdk.instructions()