Skip to main content

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.

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

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)

Installation

# Install the SDK
npm install @cowprotocol/cow-sdk

# Install an 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

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