CoW Protocol SDK - get quote, configure your order, and trade
npm install @cowprotocol/cow-sdk

| Statements | Branches | Functions | Lines |
| ------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- |
| !Statements | !Branches | !Functions | !Lines |
The SDK supports all CoW Protocol enabled networks:
- Ethereum (1) - SupportedChainId.MAINNET
- Gnosis Chain (100) - SupportedChainId.GNOSIS_CHAIN
- Arbitrum One (42161) - SupportedChainId.ARBITRUM_ONE
- Base (8453) - SupportedChainId.BASE
- Polygon (137) - SupportedChainId.POLYGON
- Avalanche (43114) - SupportedChainId.AVALANCHE
- Lens (232) - SupportedChainId.LENS
- BNB (56) - SupportedChainId.BNB
- Linea (59144) - SupportedChainId.LINEA
- Plasma (9745) - SupportedChainId.PLASMA
- Ink (57073) - SupportedChainId.INK
- Sepolia (11155111) - SupportedChainId.SEPOLIA (Testnet)
- CoW Protocol Documentation
- API Reference
- CoW Protocol Website
- Examples Repository
- Issues: GitHub Issues
- Discord: CoW Protocol Discord
- Documentation: docs.cow.fi
React Examples:
- React (viem)
- React (wagmi)
- React (ethers v6)
- React (ethers v5)
Node.js Examples:
- Node.js (viem)
- Node.js (ethers v6)
- Node.js (ethers v5)
``bash`
pnpm add @cowprotocol/cow-sdk
`bash`
yarn add @cowprotocol/cow-sdk
Using CoW Protocol, you can perform swaps, limit orders, TWAP orders, and many other operations.
The @cowprotocol/cow-sdk provides tools at different abstraction levels, allowing you to conveniently implement basic scenarios while maintaining the flexibility to control all possible cases.
> In most cases, you will only need to use the Trading SDK
- Main tool for swap and limit orders with built-in quote fetching, order signing, and posting - Utilities for signing orders and cancellations using cryptographic algorithms - Provides the ability to retrieve orders and trades from the CoW Protocol order book, as well as add and cancel them - API for accessing order metadata and additional information - Cross-chain token transfers and bridging functionality - SDK for Programmatic Orders such as TWAP (Read more) - Account abstraction that leverages EOA with smart contract capabilities, Ethers6, or Ethers5.Before (v6):
`typescript
import { SupportedChainId, TradingSdk, MetadataApi, OrderBookApi } from '@cowprotocol/cow-sdk'const options = {}
const sdk = new TradingSdk({
chainId: SupportedChainId.SEPOLIA,
appCode: 'YOUR_APP_CODE',
}, options)
`After (v7):
`typescript
import { SupportedChainId, OrderKind, TradeParameters, TradingSdk } from '@cowprotocol/cow-sdk'
import { ViemAdapter } from '@cowprotocol/sdk-viem-adapter'
import { createPublicClient, http, privateKeyToAccount } from 'viem'
import { sepolia } from 'viem/chains'// NEW: Instantiate and set adapter
const adapter = new ViemAdapter({
provider: createPublicClient({
chain: sepolia,
transport: http('YOUR_RPC_URL')
}),
// You can also set
walletClient instead of signer using useWalletClient from wagmi
signer: privateKeyToAccount('YOUR_PRIVATE_KEY' as 0x${string})
})const options = {}
const sdk = new TradingSdk({
chainId: SupportedChainId.SEPOLIA,
appCode: 'YOUR_APP_CODE',
}, options, adapter)
`Most other packages (e.g.,
MetadataApi or BridgingSdk) have a parameter to set an adapter.
You can also set an adapter using setGlobalAdapter:`typescript
import { setGlobalAdapter, CowShedSdk } from '@cowprotocol/cow-sdk'const adapter = {...}
// Set global adapter
setGlobalAdapter(adapter)
const cowShedSdk = new CowShedSdk()
`You will likely also need to bind the SDK to your app's account state.
When the account or network changes in the app/wallet, it should be updated in the SDK as well.
Here's an example for
WAGMI (see examples/react/wagmi):`typescript
import { useAccount, usePublicClient, useWalletClient } from 'wagmi'
import { useEffect, useState } from 'react'
import { ViemAdapter, ViemAdapterOptions } from '@cowprotocol/sdk-viem-adapter'
import { tradingSdk } from '../cowSdk.ts'
import { setGlobalAdapter } from '@cowprotocol/cow-sdk'export function useBindCoWSdkToWagmi(): boolean {
const { chainId } = useAccount()
const { data: walletClient } = useWalletClient()
const publicClient = usePublicClient()
const [isSdkReady, setIsSdkReady] = useState(false)
/**
* Sync Trading SDK with wagmi account state (chainId and signer)
*/
useEffect(() => {
if (!walletClient || !chainId) return
setGlobalAdapter(
new ViemAdapter({
provider: publicClient,
walletClient,
} as unknown as ViemAdapterOptions),
)
tradingSdk.setTraderParams({ chainId })
setIsSdkReady(true)
}, [publicClient, walletClient, chainId])
return isSdkReady
}
`Basic Use Case
This example demonstrates the main use case of creating a swap and shows how to:
- Get a quote
- Verify amounts
- Adjust swap parameters
- Sign and post an order`typescript
import { SupportedChainId, OrderKind, TradeParameters, TradingSdk } from '@cowprotocol/cow-sdk'
import { ViemAdapter } from '@cowprotocol/sdk-viem-adapter'
import { createPublicClient, http, privateKeyToAccount } from 'viem'
import { sepolia } from 'viem/chains'// EthersV5Adapter and EthersV6Adapter are also available
// @cowprotocol/sdk-ethers-v5-adapter, @cowprotocol/sdk-ethers-v6-adapter
const adapter = new ViemAdapter({
provider: createPublicClient({
chain: sepolia,
transport: http('YOUR_RPC_URL')
}),
// You can also set
walletClient instead of signer using useWalletClient from wagmi
signer: privateKeyToAccount('YOUR_PRIVATE_KEY' as 0x${string})
})const sdk = new TradingSdk({
chainId: SupportedChainId.SEPOLIA,
appCode: 'YOUR_APP_CODE',
}, {}, adapter)
const parameters: TradeParameters = {
kind: OrderKind.SELL,
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14',
sellTokenDecimals: 18,
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59',
buyTokenDecimals: 18,
amount: '120000000000000',
}
// Get quote
const { quoteResults, postSwapOrderFromQuote } = await sdk.getQuote(parameters)
const buyAmount = quoteResults.amountsAndCosts.afterSlippage.buyAmount
// Verify amount
if (confirm(
You will receive at least: ${buyAmount}. Proceed?)) {
// Sign and post order
const orderId = await postSwapOrderFromQuote() console.log('Order created, ID:', orderId)
}
`This example demonstrates the simplest way to trade on CoW Protocol.
For more advanced use cases, you can use additional parameters such as
receiver, partiallyFillable, validTo, and others.
Refer to the Trading SDK documentation for comprehensive details.Developer Attribution (UTM Tracking)
The SDK automatically includes UTM tracking in all orders to attribute trading volume to developers. Default parameters:
`typescript
{
utmSource: 'cowmunity',
utmMedium: 'cow-sdk@7.1.6', // SDK version
utmCampaign: 'developer-cohort',
utmContent: '',
utmTerm: 'js'
}
`Customize or disable via
advancedSettings.appData.metadata.utm:`typescript
// Custom UTM
await sdk.getQuote(parameters, {
appData: {
metadata: {
utm: { utmContent: 'my-integration-v2' }
}
}
})// Disable UTM
await sdk.getQuote(parameters, {
appData: {
metadata: {
utm: {}
}
}
})
`> Note: Providing any
utm field gives you full control - the SDK will not add defaults.> Tip: Use
utmContent for graffiti without affecting your appCode. The appCode parameter tracks your integration on CoW Protocol's Dune dashboards, while utmContent is available for custom identifiers or experimentation - all while attributing your volume to SDK integrators' collective impact.
Adapters
The CoW SDK supports multiple blockchain adapters to work with different Web3 libraries. You need to install and configure one of the following adapters:
$3
- EthersV6Adapter - For ethers.js v6
- EthersV5Adapter - For ethers.js v5
- ViemAdapter - For viem
$3
`bash
For ethers v6
pnpm add @cowprotocol/sdk-ethers-v6-adapter ethersFor ethers v5
pnpm add @cowprotocol/sdk-ethers-v5-adapter ethers@^5.7.0For viem
pnpm add @cowprotocol/sdk-viem-adapter viem
`$3
#### EthersV6Adapter
`typescript
import { EthersV6Adapter } from '@cowprotocol/sdk-ethers-v6-adapter'
import { JsonRpcProvider, Wallet } from 'ethers'const provider = new JsonRpcProvider('YOUR_RPC_URL')
const wallet = new Wallet('YOUR_PRIVATE_KEY', provider)
const adapter = new EthersV6Adapter({ provider, signer: wallet })
`#### EthersV5Adapter
`typescript
import { EthersV5Adapter } from '@cowprotocol/sdk-ethers-v5-adapter'
import { ethers } from 'ethers'const provider = new ethers.providers.JsonRpcProvider('YOUR_RPC_URL')
const wallet = new ethers.Wallet('YOUR_PRIVATE_KEY', provider)
const adapter = new EthersV5Adapter({ provider, signer: wallet })
`#### ViemAdapter
`typescript
import { ViemAdapter } from '@cowprotocol/sdk-viem-adapter'
import { http, createPublicClient, privateKeyToAccount } from 'viem'
import { sepolia } from 'viem/chains'const account = privateKeyToAccount('YOUR_PRIVATE_KEY' as
0x${string})
const transport = http('YOUR_RPC_URL')
const provider = createPublicClient({ chain: sepolia, transport })// You can also set
walletClient instead of signer using useWalletClient from wagmi
const adapter = new ViemAdapter({ provider, signer: account })
`Low-Level SDK Usage Example
This example demonstrates low-level API usage with a practical scenario:
exchanging
0.4 GNO for WETH on the Gnosis Chain network.We will perform the following operations:
1. Get a quote
2. Sign the order
3. Send the order to the order book
4. Get the data of the created order
5. Get trades for the order
6. Cancel the order (signing + sending)
> Try it live: https://codesandbox.io/p/devbox/cow-sdk-example-forked-x63k52
`typescript
import { OrderBookApi, OrderSigningUtils, SupportedChainId, OrderQuoteSideKindSell, SigningScheme, setGlobalAdapter } from '@cowprotocol/cow-sdk'
import { EthersV6Adapter } from '@cowprotocol/sdk-ethers-v6-adapter'
import { JsonRpcProvider, Wallet } from 'ethers'const account = 'YOUR_WALLET_ADDRESS'
const chainId = 100 // Gnosis Chain
const provider = new JsonRpcProvider('YOUR_RPC_URL')
const wallet = new Wallet('YOUR_PRIVATE_KEY', provider)
const adapter = new EthersV6Adapter({ provider, signer: wallet })
setGlobalAdapter(adapter)
const receiver = account
const quoteRequest = {
sellToken: '0x6a023ccd1ff6f2045c3309768ead9e68f978f6e1', // WETH on Gnosis Chain
buyToken: '0x9c58bacc331c9aa871afd802db6379a98e80cedb', // GNO on Gnosis Chain
from: account,
receiver,
sellAmountBeforeFee: (0.4 10 * 18).toString(), // 0.4 WETH
kind: OrderQuoteSideKindSell.SELL,
}
const orderBookApi = new OrderBookApi({ chainId: SupportedChainId.GNOSIS_CHAIN })
async function main() {
const { quote } = await orderBookApi.getQuote(quoteRequest)
const orderData = {
...quote,
// Add fee to sellAmount for sell orders
sellAmount: (BigInt(quote.sellAmount) + BigInt(quote.feeAmount)).toString(),
receiver,
feeAmount: "0",
};
const orderSigningResult = await OrderSigningUtils.signOrder(orderData, chainId, adapter.signer)
const orderId = await orderBookApi.sendOrder({
...orderData,
...orderSigningResult,
signingScheme: SigningScheme.EIP712,
})
const order = await orderBookApi.getOrder(orderId)
const trades = await orderBookApi.getTrades({ orderId })
const orderCancellationSigningResult = await OrderSigningUtils.signOrderCancellations([orderId], chainId, adapter.signer)
const cancellationResult = await orderBookApi.sendSignedOrderCancellations({
...orderCancellationSigningResult,
orderUids: [orderId],
})
console.log('Results:', { orderId, order, trades, orderCancellationSigningResult, cancellationResult })
}
``