Skip to content

cowswap Widget for Embedding Swaps: Inputs to Order Events

The cowswap widget is an npm library that embeds the CoW Swap trading interface in an iframe inside your dApp, configured through a params object. Embedding cowswap gives users intent-based swaps while the interface handles quotes and the order signing flow. Your app provides a container and receives order activity through listeners.

What the cowswap Widget Embeds

The widget embeds an interactive trading form in a container you choose. Take an illustrative portfolio dApp with an empty swap panel and a connected wallet. Before mounting, that panel is only a div. After mounting, the same panel contains the trading iframe; the surrounding portfolio view remains yours. The user can quote a trade and sign an intent there, while your host app keeps its existing account and layout.

Install @cowprotocol/widget-lib, create the container, and call createCowSwapWidget(container, { params }). An illustrative minimal mount is const params = { appCode: 'PORTFOLIO_APP', chainId: supportedChainId, tradeType: 'swap' }; createCowSwapWidget(document.getElementById('swap'), { params }). The swap div must exist when that call runs, and supportedChainId must be a supported chain. Choose a different supported tradeType for a limit or advanced order, including TWAP.

The params object can also set sell and buy assets, optional amounts, theme, and partnerFee. Set sell: { asset: selectedSellToken } and buy: { asset: selectedBuyToken } when your host already knows the intended pair. Those values initialize the form; the user chooses the final terms. The widget parameter reference gives the accepted shapes for these settings.

Inside the iframe, the interface quotes the chosen trade and asks the user to sign an intent specifying acceptable terms. Solver-run batch auctions seek execution for posted orders. A posted order has a UID that identifies that particular trade. Your integration supplies params and, optionally, a wallet provider; it receives the UID and status events to reflect the trade in your own application.

The appCode deserves a stable value because the widget writes it into every order’s appData. In the illustrative portfolio dApp, PORTFOLIO_APP marks orders initiated through that embed. Inspect one in CoW Explorer and its metadata attributes it to your integration. The order UID identifies the individual trade, while appCode identifies where it originated. That distinction lets you reconcile widget orders with your analytics or investigate a user’s report without mixing in orders placed elsewhere. Keep appCode consistent across deployments so orders from the same integration remain attributable together.

Pass a listeners array to receive events for orders posted, fulfilled, and cancelled. Capture the UID when an order is posted, then use fulfillment or cancellation events to update the corresponding row in your dApp’s activity view. Keep that UID in your own record so a later event changes the right trade. The widget’s event bus also allows listener updates when your host changes its notification handling.

Validate chainId and tokens before mounting. An unsupported chainId leaves the form unusable, as can a token list with no usable assets for that chain. Confirm that your requested sell and buy assets exist on the selected chain and that custom token lists load with compatible entries. Then your host can show a configuration error beside the same panel if its inputs cannot support a trade.

The widget’s wallet and partner fee settings determine how the embedded trade uses your existing account connection and fee configuration.

Can the widget use my app's connected wallet?

Yes, the widget uses your dApp’s connected wallet when you pass an EIP-1193 provider alongside params: createCowSwapWidget(container, { params, provider: connectedProvider }). The user signs through that wallet. Omit provider and the widget offers its own connection. In the portfolio panel, the source of the wallet connection changes; the container, appCode attribution, and order events stay the same. Ensure the provider is ready for the chain selected by chainId.

How do partner fees work in the widget?

The widget’s partnerFee parameter takes a rate in basis points and a recipient address. An illustrative setting, partnerFee: { bps: 50, recipient: feeRecipient }, expresses a 0.5% rate directed to that recipient when the fee applies. The configuration can vary by chain or trade type. Account for the configured fee when evaluating the quote presented for user approval. Verify that the recipient is valid on the selected chain and that the applicable partner program terms cover the trade.