Skip to main content

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
This recipe uses raw HTTP calls so it works in any language or runtime — Node.js scripts, backend services, AI agents, or cron jobs.

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 a signingPayload you can send directly on-chain.

Request Fields

Exactly one of from.amount or to.amount must be provided. Sending both or neither returns a 400 error.

Response

The 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 the signingPayload 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 base64 serializedTransaction 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.
For a Solana source or destination, set 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 base64 serializedTransaction; 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 for chainName, 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 the tokenAddress 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 need 0.01 or 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 maxPriceImpact to filter out routes that would move the market too much for your trade size.
Last modified on June 8, 2026