What We’re Building
A server-side flow that swaps tokens using Dynamic’s Swap API directly. By the end of this guide you will be able to:- Get a swap quote with route, fees, and a ready-to-sign payload
- Handle ERC-20 token approvals when required
- Sign and broadcast the swap transaction on-chain
- Poll for cross-chain completion
Prerequisites
- A Dynamic environment
- A wallet with private key access (for signing)
- Node.js 18+ (or any runtime with
fetch)
Base URL
All swap endpoints live under:Overview
The swap flow is two API calls plus one on-chain transaction:The Swap API is stateless — there is no session token or transaction state to manage. Each call is independent.
Step 1: Get a Swap Quote
Request a quote by specifying the source and destination tokens. The API finds the best route and returns asigningPayload you can send directly on-chain.
Request Fields
Response
signingPayload contains the to, data, and value fields needed to submit the transaction on-chain.
For cross-chain swaps, the steps array shows each hop (bridge + swap), so you can display the full route to users.
Step 2: Sign and Broadcast
Use thesigningPayload from the quote to submit an on-chain transaction. Its shape depends on the source chain:
The EVM example below uses viem; the Solana example uses
@solana/web3.js. Any signing library works.
EVM
Handle ERC-20 Approval
If you’re swapping an ERC-20 token (not a native token), you may need to approve the router to spend your tokens first. Check whether the router already has sufficient allowance — if not, send an approval transaction before the swap.approve.ts
Send the Swap Transaction
swap.ts
Solana
For Solana the quote returns a base64serializedTransaction instead of EVM calldata — a ready-to-sign transaction with the blockhash and any associated token account (ATA) creation already bundled in. There are no token approvals. Deserialize it, sign with your keypair, and broadcast as-is. Sign promptly: the quote’s blockhash has a limited lifetime, so request a fresh quote rather than reusing a stale one.
swap-solana.ts
Using Dynamic server wallets (MPC) instead of a local keypair? Sign the same
VersionedTransaction with DynamicSvmWalletClient.signTransaction, attach the returned signature to the transaction, then broadcast — no raw private key required. This mirrors what the SDK’s executeSwapTransaction does internally.Step 3: Poll for Status
For cross-chain swaps, use the status endpoint to track completion. For same-chain swaps (including Solana), the transaction confirmation alone is sufficient.chainName to "SOL" and chainId to "101", and pass the transaction signature as txHash:
Response
Status Values
Substatus Values (When Pending)
Poll every 3–5 seconds until
status is COMPLETED or FAILED.
Complete Example
A self-contained TypeScript script that gets a quote, signs, broadcasts, and polls for completion:swap-example.ts
Cross-Chain Example
Swap USDC on Ethereum to MATIC on Polygon — the API handles bridging automatically:cross-chain-swap.ts
Cross-chain swaps may take longer to complete. The
steps array in the quote shows each bridge and swap hop along the route.Solana Example
Swap native SOL to USDC on Solana. The quote returns a base64serializedTransaction; sign and broadcast it with the sendSolanaSwap helper from Step 2.
solana-swap.ts
This is an exact-output swap (
to.amount is set), so the route pins the minimum USDC out at that amount. A too-tight slippage can trip the aggregator’s minimum-out guard on thin routes — 0.1 (10%) reliably clears small swaps; tighten it for larger amounts. To swap an exact amount of SOL instead, set from.amount and omit to.amount.Supported Chains and Native Tokens
The Swap API supports the following chains (mainnet only). Use these values forchainName, chainId, and native token addresses in your requests.
Chain Reference
Native Token Addresses
For native tokens (ETH, SOL, BTC, SUI), use any of the accepted addresses below in thetokenAddress field:
For non-native tokens, use the token’s contract address on that chain (e.g.,
0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 for USDC on Ethereum).
Error Handling
Tips
- Quote freshness: Quotes are snapshots — prices and gas can shift. Sign promptly after receiving a quote.
- Slippage: Set slippage based on token liquidity. Stablecoins work well at
0.005(0.5%), volatile pairs may need0.01or more. - Order preference: Use
"CHEAPEST"to minimize fees or"FASTEST"to reduce execution time. Cross-chain routes benefit the most from this. - Price impact: Set
maxPriceImpactto filter out routes that would move the market too much for your trade size.
Related
- Fireblocks Flow API guide — Accept payments with automatic cross-chain settlement
- Cross-chain swaps with LI.FI — Frontend integration with LI.FI widget
getSwapQuote— JavaScript SDK swap quote referenceexecuteSwapTransaction— JavaScript SDK swap execution reference