API
Error Handling
Handle API errors gracefully in your Buff integration.
API Error Format
All Buff API endpoints return a consistent JSON format. On error:
error-response.json
typescript
1{2 "ok": false,3 "error": "Auth required. Use x-api-key OR x-wallet + x-signature headers."4}Common Errors
| Status | Error | Cause |
|---|---|---|
| 401 | Auth required | Missing API key or wallet signature |
| 403 | Invalid API key / Invalid signature | Wrong credentials |
| 400 | txValueUsd is required | Missing required field |
| 400 | Invalid public key format | Malformed Solana address |
| 400 | Simulation failed | Transaction would fail on-chain |
| 400 | Insufficient balance after rent exemption | Not enough SOL for swap |
| 429 | Rate limited | Too many requests (60/min per IP) |
| 500 | Transaction failed | On-chain execution error |
| 502 | Jupiter quote failed | Jupiter API unavailable |
Catching Errors
errors.ts
typescript
1import { Buff } from "buff-protocol-sdk"23const buff = new Buff({ apiKey: "YOUR_KEY" })45try {6 const result = await buff.buildSwap(buffWalletPubkey)7 if (result.ready) {8 for (const tx of result.transactions) {9 await buff.executeSwap(signedTx)10 }11 }12} catch (err) {13 if (err instanceof Error) {14 // All SDK errors are plain Error instances15 // The message contains the API error string16 console.error("Buff error:", err.message)1718 // Common patterns:19 if (err.message.includes("Auth required")) {20 // Re-authenticate21 }22 if (err.message.includes("Rate limited")) {23 // Back off and retry24 }25 if (err.message.includes("Jupiter")) {26 // Jupiter API issue — retry later27 }28 }29}Retry Strategy
Transient failures (network, Jupiter, rate limits) can be retried:
retry.ts
typescript
1async function withRetry(fn, maxRetries = 3) {2 for (let i = 0; i < maxRetries; i++) {3 try {4 return await fn()5 } catch (err) {6 if (i === maxRetries - 1) throw err7 await new Promise(r => setTimeout(r, 1000 * (i + 1)))8 }9 }10}1112// Usage13const result = await withRetry(() => buff.buildSwap(wallet))Note
The Buff API handles swap routing retries internally. Client-side retries are only needed for network-level failures (timeouts, connection drops, rate limits).