Build a transaction
When you call sdk.buy(), the SDK first calls this endpoint (POST /v1/tx/buy) 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/buyRequest Body
Section titled “Request Body”{ "mint": "TOKEN_MINT_ADDRESS", "solAmount": 0.002, "slippage": 0.05, "priority": "fast"}Fields:
mint: Token address you want to buysolAmount: Amount of SOL to spendslippage: 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/buy.
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 api = axios.create({ baseURL: API_BASE_URL, headers: { 'Authorization': `Bearer ${API_KEY}` }, timeout: 30000,});
async function buildBuyTransaction() { const response = await api.post('/tx/buy', { mint: 'TOKEN_MINT_ADDRESS', solAmount: 0.002, 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"
def build_buy_transaction(): response = requests.post( f"{API_BASE_URL}/tx/buy", headers={"Authorization": f"Bearer {API_KEY}"}, json={ "mint": "TOKEN_MINT_ADDRESS", "solAmount": 0.002, "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/buy \ -H "Authorization: Bearer api_your_key_here" \ -H "Content-Type: application/json" \ -d '{ "mint": "TOKEN_MINT_ADDRESS", "solAmount": 0.002, "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.