Skip to main content

CoW Protocol Python SDK

Build DeFi applications with MEV-protected trading on CoWSwap

What is CoW Protocol?

CoW Protocol (Coincidence of Wants) powers CoWSwap, a decentralized exchange aggregator that protects traders from MEV (Maximal Extractable Value) through batch auctions and intent-based trading. The cowdao_cowpy Python SDK enables developers to programmatically interact with CoWSwap and the CoW Protocol ecosystem.

Key Features

MEV-Protected Trading

Execute trades with built-in protection against front-running and sandwich attacks

Multi-Chain Support

Trade on Ethereum, Gnosis Chain, Arbitrum, Base, Polygon, Avalanche, BNB, and Lens

Order Management

Create, fetch, and cancel orders with full Python type hints

Subgraph Querying

Access trading data and analytics via GraphQL queries

Smart Contract Integration

Interact with CoW Protocol smart contracts using Web3.py

Advanced Orders

Build composable conditional orders and TWAP strategies

Use Cases

The CoW Protocol Python SDK is perfect for:
  • Trading Bots — Automate trading strategies with MEV protection
  • DeFi Applications — Integrate CoWSwap functionality into your projects
  • Data Analytics — Query trading volumes, order history, and market data
  • Solver Development — Build custom order matching solutions
  • Portfolio Management — Execute multi-token swaps across chains

Quick Example

Here’s a simple token swap using the SDK:
swap_example.py
import os
import asyncio
from web3 import Account, Web3
from web3.types import Wei
from cowdao_cowpy.cow.swap import swap_tokens
from cowdao_cowpy.common.chains import Chain

# Configuration
BUY_TOKEN = Web3.to_checksum_address("0xfFf9976782d46CC05630D1f6eBAb18b2324d6B14")  # WETH
SELL_TOKEN = Web3.to_checksum_address("0xbe72E441BF55620febc26715db68d3494213D8Cb")  # USDC
SELL_AMOUNT = Wei(5000000000000000000)  # 50 USDC with 18 decimals

# Load account
PRIVATE_KEY = os.getenv("PRIVATE_KEY")
account = Account.from_key(PRIVATE_KEY)

# Execute swap
asyncio.run(
    swap_tokens(
        amount=SELL_AMOUNT,
        account=account,
        chain=Chain.SEPOLIA,
        sell_token=SELL_TOKEN,
        buy_token=BUY_TOKEN,
    )
)

Why Choose CoW Protocol SDK?

Full Python type hints and Pydantic models throughout the SDK ensure compile-time safety and excellent IDE support.
Native asyncio support for building high-performance applications that can handle multiple concurrent operations.
Seamless integration with Web3.py for Ethereum and other EVM-compatible chains.
Pay transaction fees in sell tokens instead of ETH, making trading more accessible.

Next Steps

Installation

Install the SDK via pip and set up your environment

Quickstart

Execute your first token swap in minutes

Core Concepts

Learn about chains, orders, and trading mechanics

API Reference

Explore the complete API documentation

Community & Support

This SDK is community-maintained and not officially endorsed by CoW Protocol. For production use, thoroughly test your integration and understand the risks involved in DeFi trading.

Build docs developers (and LLMs) love