SDK Integration
The CoW SDK provides programmatic access to CoW Protocol's functionality, enabling you to build custom trading applications with full control over the user interface and trading logic.
Overview
The CoW SDK is a TypeScript library that offers multiple levels of abstraction, from high-level trading functions to low-level API access. It supports multiple blockchain adapters (Ethers v5/v6, Viem) and works across all CoW Protocol-enabled networks.
For Solana, use @cowprotocol/sdk-trading-solana to fetch quotes, build orders, and manage approvals. See Integrate on Solana for more details.
Key Features
- Trading SDK: High-level API for swaps and limit orders
- Multi-network support: Ethereum, Gnosis Chain, Arbitrum, Base, Polygon, and more
- Flexible adapters: Works with Ethers v5/v6 and Viem
- Order management: Create, sign, and manage orders programmatically
- Quote fetching: Get real-time pricing before executing trades
Quick Start Example (EVM)
import { SupportedChainId, OrderKind, TradeParameters, TradingSdk } from '@cowprotocol/cow-sdk'
import { ViemAdapter } from '@cowprotocol/sdk-viem-adapter'
import { createPublicClient, http, privateKeyToAccount } from 'viem'
import { mainnet } from 'viem/chains'
// Setup adapter
const adapter = new ViemAdapter({
provider: createPublicClient({
chain: mainnet,
transport: http('YOUR_RPC_URL')
}),
signer: privateKeyToAccount('YOUR_PRIVATE_KEY' as `0x${string}`)
})
// Initialize SDK
const sdk = new TradingSdk({
chainId: SupportedChainId.MAINNET,
appCode: 'YOUR_APP_CODE',
}, {}, adapter)
// Define trade parameters
const parameters: TradeParameters = {
kind: OrderKind.SELL,
sellToken: '0xA0b86a33E6411Ec5d0b9dd2E7dC15A9CAA6C1F8e', // USDC
sellTokenDecimals: 6,
buyToken: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', // WETH
buyTokenDecimals: 18,
amount: '1000000', // 1 USDC
}
// Get quote and execute trade
const { quoteResults, postSwapOrderFromQuote } = await sdk.getQuote(parameters)
const orderId = await postSwapOrderFromQuote()
console.log('Order created:', orderId)
Quick Start Example (Solana)
Both flows use a connected wallet and a Solana mainnet-beta connection.
User-funded Orders
The wallet needs USDC to sell and SOL for fees and account rent.
import { SolanaTradingSdk } from '@cowprotocol/sdk-trading-solana'
import { OrderKind } from '@cowprotocol/sdk-order-book'
import { Transaction } from '@solana/web3.js'
// Initialize SDK
const sdk = new SolanaTradingSdk({ env: 'prod' })
const sellToken = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v' // USDC
const buyToken = '11111111111111111111111111111111' // Native SOL
// Get a quote
const { solanaQuote, buildOrder, postSponsoredOrder } = await sdk.getQuote({
ownerAddress: wallet.publicKey,
sellTokenAddress: sellToken,
sellTokenDecimals: 6,
buyTokenAddress: buyToken,
buyTokenDecimals: 9,
amount: 1_000_000n, // 1 USDC in base units
kind: OrderKind.SELL,
slippageBps: 50, // 0.5%
})
// Approve tokens and build the order
const approval = sdk.approveCowProtocol({
ownerAddress: wallet.publicKey,
sellTokenAddress: sellToken,
approveAmount: solanaQuote.intent.sellAmount,
})
const { instruction, orderId } = await buildOrder()
// Sign and submit the creation transaction
const { blockhash } = await connection.getLatestBlockhash('confirmed')
const transaction = new Transaction({
feePayer: wallet.publicKey,
recentBlockhash: blockhash,
}).add(approval, instruction)
const signed = await wallet.signTransaction(transaction)
await connection.sendRawTransaction(signed.serialize(), {
preflightCommitment: 'confirmed',
})
console.log('Order submitted:', orderId)
Sponsored Orders
Use the same quote and approval above, replacing the order-building and submission steps with:
if (!solanaQuote.funder) throw new Error('Sponsorship is unavailable')
const { instruction, feePayer } = await buildOrder(undefined, {
sponsor: solanaQuote.funder,
})
const { blockhash } = await connection.getLatestBlockhash('confirmed')
const transaction = new Transaction({
feePayer,
recentBlockhash: blockhash,
}).add(approval, instruction)
const signed = await wallet.signTransaction(transaction)
const bytes = signed.serialize({ requireAllSignatures: false })
const orderId = await postSponsoredOrder(btoa(String.fromCharCode(...bytes)))
console.log('Order submitted:', orderId)
The SDK sends the partially signed transaction to the Order Book API. CoW adds the sponsor's signature and submits it. Use orderId to track the order in either flow.
Installation
# Install the EVM SDK
npm install @cowprotocol/cow-sdk
# Install an EVM adapter (choose one)
npm install @cowprotocol/sdk-viem-adapter viem
npm install @cowprotocol/sdk-ethers-v6-adapter ethers
npm install @cowprotocol/sdk-ethers-v5-adapter ethers@^5.7.0
# Install the Solana SDK
npm install @cowprotocol/sdk-trading-solana @cowprotocol/sdk-order-book @solana/web3.js
When to Use the SDK
- Custom UI requirements: Building unique trading interfaces
- Advanced features: Implementing complex trading logic or order types
- Backend integration: Server-side order management
- DApp development: Integrating trading into existing applications
Next Steps
For comprehensive documentation, examples, and advanced usage patterns, visit the CoW SDK Documentation.
Resources
- Complete SDK Documentation - Detailed guides and API reference
- Solana Integration - Solana swaps with
@cowprotocol/sdk-trading-solana - GitHub Repository - Source code and examples
- API Reference - REST API documentation
- Examples - React and Node.js examples