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.
Building Transactions
Section titled “Building Transactions”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.
Endpoint
Section titled “Endpoint”POST /v1/tx/swapRequest Body
Section titled “Request Body”{ "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 spendingoutputMint: mint address of the token you are receivingamount: amount of the exact side — input whenswapModeisexactIn, output whenexactOutswapMode:exactIn(spend a fixed amount) orexactOut(receive a fixed amount) —exactInis the right defaultslippage: maximum allowed slippage (0.05 = 5%)priority: transaction priority (economy,fast,faster,fastest) — see Priority
What You Get Back
Section titled “What You Get Back”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.
Code Examples
Section titled “Code Examples”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);import requests
API_KEY = "api_your_key_here"API_BASE_URL = "https://api.darkfibre.dev/v1"WSOL_MINT = "So11111111111111111111111111111111111111112"
def build_buy_transaction(): response = requests.post( f"{API_BASE_URL}/tx/swap", headers={"Authorization": f"Bearer {API_KEY}"}, json={ "inputMint": WSOL_MINT, "outputMint": "TOKEN_MINT_ADDRESS", "amount": 0.002, "swapMode": "exactIn", "slippage": 0.05, "priority": "fast", }, timeout=30, ) response.raise_for_status()
data = response.json()["data"]
print(f"Submission token: {data['submissionToken']}") print(f"Expires at: {data['expiresAt']}") print(f"Priority cost: {data['priorityCost']} SOL")
return { "submissionToken": data["submissionToken"], "unsignedTransaction": data["unsignedTransaction"], }
build_buy_transaction()curl -X POST https://api.darkfibre.dev/v1/tx/swap \ -H "Authorization: Bearer api_your_key_here" \ -H "Content-Type: application/json" \ -d '{ "inputMint": "So11111111111111111111111111111111111111112", "outputMint": "TOKEN_MINT_ADDRESS", "amount": 0.002, "swapMode": "exactIn", "slippage": 0.05, "priority": "fast" }'Understanding the Response
Section titled “Understanding the Response”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.