Support for custom wallet types (including Smart Accounts with additional setup)
Continue reading below for examples
Both options include built-in slippage protection to ensure your swap executes at a fair price, even in volatile market conditions. Gas fees are automatically calculated and optimized for the most cost-effective route.
Slippage is the difference between the expected price of a trade and the actual price at which it executes.
More on slippage protection
In these examples, we set a 1% slippage tolerance (using slippageBps: 100 in TypeScript or slippage_bps: 100 in Python), meaning the trade will only execute if the final price is within 1% of the expected price.
This protects you from unfavorable trades if the price moves significantly between when you submit the transaction and when it’s executed.
To begin, let’s walk through an example of how to estimate a swap price with a regular account (EOA).
getSwapPrice for Typescript and get_swap_price for Python provide estimates only and do not reserve funds. They are suitable for frequent price updates but may be less precise than creating a swap quote.
Copy
Ask AI
import { CdpClient } from "@coinbase/cdp-sdk";const cdp = new CdpClient();// Get price for swapping 1 WETH to USDC on Baseconst swapPrice = await cdp.evm.getSwapPrice({ fromToken: "0x4200000000000000000000000000000000000006", // WETH toToken: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913", // USDC fromAmount: BigInt("1000000000000000000"), // 1 WETH (18 decimals) network: "base", taker: "0x1234567890123456789012345678901234567890" // Your EOA address});if (swapPrice.liquidityAvailable) { console.log(`You'll receive: ${swapPrice.toAmount} USDC`); console.log(`Minimum after slippage: ${swapPrice.minToAmount} USDC`);}
Once you’re ready to commit to a swap, you can create a swap quote using the CDP API. This gives you the transaction data needed for execution as opposed to the quick price estimate that we demonstrated above.
Creating a swap quote may reserve funds onchain. This action is strictly rate-limited.
Once you’re ready to commit to a swap, you can create a swap quote using the CDP API. This gives you the transaction data needed for execution as opposed to the quick price estimate that we demonstrated above.
Creating a swap quote may reserve funds onchain. This action is strictly rate-limited.
If you prefer to use your own wallet, signing infrastructure, and node for broadcasting transactions, you can use the core Swap APIs (like getSwapPrice and quoteSwap) without a CDP account.
If you’re using your own wallet infrastructure (e.g., viem, web3.py, etc.), you can execute swaps using the transaction data from the quote. For Smart Accounts with external wallets, additional ERC-4337 infrastructure setup is required.