Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions docs/cow-protocol/integrate/api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,53 @@ The primary API for creating and managing orders on CoW Protocol.
curl -X POST "https://api.cow.fi/mainnet/api/v1/quote" \
-H "Content-Type: application/json" \
-d '{
"sellToken": "0xA0b86a33E6411Ec5d0b9dd2E7dC15A9CAA6C1F8e",
"sellToken": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
"buyToken": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
"sellAmountBeforeFee": "1000000",
"sellAmountBeforeFee": "1000000000",
"kind": "sell",
"from": "0xYourWalletAddress"
}'
```

### 2. Sign and Submit Order
### 2. Apply Slippage and Sign Order

:::caution Important
Before signing, you must apply slippage tolerance to protect against price movements. See the [Quote to Order Tutorial](../tutorials/quote-to-order.mdx) for detailed examples.
:::

```javascript
// After getting a quote, sign the order
import { domain, signOrder, OrderKind, OrderBalance, SigningScheme } from '@cowprotocol/contracts'

// Apply slippage to the quote before signing
// For sell orders: reduce buyAmount by slippage (e.g., 0.5%)
const buyAmountWithSlippage = BigInt(quoteResponse.quote.buyAmount) * 995n / 1000n

// Build order object for signing (uses enums, not strings)
const order = {
...quoteResponse,
signature: await signOrder(quoteResponse, signer),
signingScheme: "eip712"
...quoteResponse.quote,
buyAmount: buyAmountWithSlippage.toString(),
receiver: walletAddress,
kind: OrderKind.SELL,
sellTokenBalance: OrderBalance.ERC20,
buyTokenBalance: OrderBalance.ERC20,
}

// Submit the signed order
// Sign using @cowprotocol/contracts
const orderDomain = domain(1, '0x9008D19f58AAbD9eD0D60971565AA8510560ab41')
const signature = await signOrder(orderDomain, order, signer, SigningScheme.EIP712)

// Submit - API expects strings for kind/balance fields
const response = await fetch('https://api.cow.fi/mainnet/api/v1/orders', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(order)
body: JSON.stringify({
...quoteResponse.quote,
buyAmount: buyAmountWithSlippage.toString(),
receiver: walletAddress,
signature: signature.data,
signingScheme: 'eip712',
from: walletAddress
})
})

const orderId = await response.text()
Expand Down Expand Up @@ -155,11 +179,13 @@ try {

For complete API documentation including all endpoints, parameters, and response schemas, see:

- **[Quote to Order Tutorial](../tutorials/quote-to-order.mdx)** - Step-by-step guide with slippage handling
- **[Order Book API Reference](/cow-protocol/reference/apis/orderbook)** - Complete endpoint documentation
- **[API Documentation](https://api.cow.fi/docs/)** - Interactive API explorer

## Resources

- **[Quote to Order Tutorial](../tutorials/quote-to-order.mdx)** - Complete guide with slippage handling
- **[Order Book API Reference](/cow-protocol/reference/apis/orderbook)** - Detailed API documentation
- **[API Explorer](https://api.cow.fi/docs/)** - Interactive documentation
- **[GitHub Examples](https://github.com/cowprotocol/cow-sdk/tree/main/examples)** - Code examples
Expand Down
Loading