Skip to main content
This quickstart guide will walk you through creating a complete swap from setup to execution using the CoW Protocol SDK.

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
Testnet First: This guide uses Sepolia testnet. Always test on testnets before using mainnet.

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
Security: Never commit your .env file to version control. Add it to .gitignore.

Step 3: Configure the Adapter

Create a file swap.ts and set up your Web3 adapter:
swap.ts

Step 4: Initialize the TradingSdk

Add the SDK initialization to your swap.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:
You should see output similar to:

Complete Example

Here’s the complete swap.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

Slippage protects you from price movements between quote and execution.

Quote Results

The quote returns detailed information:

Token Approval

Before your first swap, you need to approve the CoW Protocol to spend your tokens:
See the Token Approvals Guide for more details.

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:
Order statuses:
  • open - Order is waiting to be filled
  • fulfilled - Order is completely filled
  • expired - Order expired before being filled
  • cancelled - 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

Make sure you have enough tokens in your wallet. Get test WETH from the Sepolia faucet.
You need to approve the CoW Protocol to spend your tokens first. See the Token Approval section above.
Quotes are only valid for a limited time (default 5 minutes). Get a new quote if yours expired.
Verify your RPC URL is correct in the .env file. Try using a different RPC provider like:
  • https://sepolia.gateway.tenderly.co
  • https://eth-sepolia.g.alchemy.com/v2/YOUR_KEY
  • https://sepolia.infura.io/v3/YOUR_KEY
Ensure your private key in .env starts with 0x and is a valid 64-character hex string.

Additional Resources