Skip to content

Build a transaction

When you call sdk.buy() or sdk.sell(), the SDK calls this endpoint (POST /v1/tx/swap) to build an unsigned transaction. This page explains what happens under the hood.

You build an unsigned transaction via REST, then sign it locally. The API constructs an optimized transaction with the correct accounts, instructions and a fresh blockhash.

POST /v1/tx/swap
{
"inputMint": "So11111111111111111111111111111111111111112",
"outputMint": "TOKEN_MINT_ADDRESS",
"amount": 0.002,
"swapMode": "exactIn",
"slippage": 0.05,
"priority": "fast"
}

Fields:

  • inputMint: mint address of the token you are spending
  • outputMint: mint address of the token you are receiving
  • amount: amount of the exact side — input when swapMode is exactIn, output when exactOut
  • swapMode: exactIn (spend a fixed amount) or exactOut (receive a fixed amount) — exactIn is the right default
  • slippage: maximum allowed slippage (0.05 = 5%)
  • priority: transaction priority (economy, fast, faster, fastest) — see Priority

The API returns several critical pieces:

  • submissionToken: Links this transaction server-side. You’ll need this when submitting.
  • unsignedTransaction: Base64-encoded wire transaction ready for signing. Don’t modify this.
  • expiresAt: ~30 second validity window due to Solana blockhash expiry. Sign and submit quickly.
  • estimates: Pre-execution estimates for input/output amounts and price impact.
  • priorityCost: The actual priority fee for this transaction. Check this before signing if you have cost limits.

For complete API reference, see: POST /v1/tx/swap.

import axios from 'axios';
const API_KEY = 'api_your_key_here';
const API_BASE_URL = 'https://api.darkfibre.dev/v1';
const WSOL_MINT = 'So11111111111111111111111111111111111111112';
const api = axios.create({
baseURL: API_BASE_URL,
headers: { 'Authorization': `Bearer ${API_KEY}` },
timeout: 30000,
});
async function buildBuyTransaction() {
const response = await api.post('/tx/swap', {
inputMint: WSOL_MINT,
outputMint: 'TOKEN_MINT_ADDRESS',
amount: 0.002,
swapMode: 'exactIn',
slippage: 0.05,
priority: 'fast',
});
const { submissionToken, unsignedTransaction, expiresAt, priorityCost } = response.data.data;
console.log('Submission token:', submissionToken);
console.log('Expires at:', expiresAt);
console.log('Priority cost:', priorityCost, 'SOL');
return { submissionToken, unsignedTransaction };
}
buildBuyTransaction().catch(console.error);

The response contains several important fields:

  • submissionToken: Links this transaction server-side. You’ll need this when submitting.
  • unsignedTransaction: Base64-encoded wire transaction ready for signing.
  • expiresAt: ~30s validity window due to blockhash expiry.
  • estimates: Pre-execution estimates for input/output amounts and price impact.
  • priorityCost: The actual priority fee for this transaction. Check this before signing if you have cost limits.