Prerequisites
Before you begin, make sure you have:- Node.js 16 or higher installed
- A wallet with a private key for signing transactions
- An RPC endpoint URL (e.g., from Alchemy, Infura, or Tenderly)
- Test tokens on Sepolia testnet
Step 1: Install Dependencies
Install the CoW Protocol SDK and a Web3 adapter:Step 2: Set Up Environment Variables
Create a.env file in your project root:
.env
Step 3: Configure the Adapter
Create a fileswap.ts and set up your Web3 adapter:
- Viem
- Ethers v6
- Ethers v5
swap.ts
Step 4: Initialize the TradingSdk
Add the SDK initialization to yourswap.ts file:
swap.ts
The
appCode parameter is used to track your integration on CoW Protocol’s Dune dashboards. Choose a unique identifier for your application.Step 5: Define Token Addresses
Define the tokens you want to swap. For Sepolia testnet:swap.ts
These are real Sepolia testnet token addresses. Get test WETH from the Sepolia faucet.
Step 6: Create Your First Swap
Now let’s create a complete swap function:swap.ts
Step 7: Run Your Swap
Execute your script:Complete Example
Here’s the completeswap.ts file:
Understanding the Code
Let’s break down the key components:Order Types
Use
SELL when you want to sell a specific amount of tokens. Use BUY when you want to receive a specific amount of tokens.Slippage Tolerance
Quote Results
The quote returns detailed information:Token Approval
Before your first swap, you need to approve the CoW Protocol to spend your tokens:Advanced Options
Custom Receiver
Send tokens to a different address:Order Validity Period
Control how long the order is valid:Partially Fillable Orders
Allow partial order fills:Monitoring Your Order
After posting an order, you can track its status:open- Order is waiting to be filledfulfilled- Order is completely filledexpired- Order expired before being filledcancelled- Order was cancelled
Next Steps
Creating Swap Orders
Learn advanced swap order configuration
Creating Limit Orders
Create limit orders with specific prices
Token Approvals
Manage token approvals efficiently
Order Management
Query, monitor, and cancel orders
Troubleshooting
Insufficient balance error
Insufficient balance error
Make sure you have enough tokens in your wallet. Get test WETH from the Sepolia faucet.
Insufficient allowance error
Insufficient allowance error
You need to approve the CoW Protocol to spend your tokens first. See the Token Approval section above.
Quote expired error
Quote expired error
Quotes are only valid for a limited time (default 5 minutes). Get a new quote if yours expired.
RPC connection error
RPC connection error
Verify your RPC URL is correct in the
.env file. Try using a different RPC provider like:https://sepolia.gateway.tenderly.cohttps://eth-sepolia.g.alchemy.com/v2/YOUR_KEYhttps://sepolia.infura.io/v3/YOUR_KEY
Invalid private key error
Invalid private key error
Ensure your private key in
.env starts with 0x and is a valid 64-character hex string.Additional Resources
- CoW Explorer (Sepolia) - Track your orders
- Sepolia Faucet - Get test ETH and tokens
- Node.js Example - Full working example
- React Example - Frontend integration