$npx -y skills add Uniswap/uniswap-ai --skill swap-integrationIntegrate Uniswap swaps into applications. Use when user says "integrate swaps", "uniswap", "trading api", "add swap functionality", "build a swap frontend", "create a swap script", "smart contract swap integration", "use Universal Router", "Trading API", or mentions swapping tok
| 1 | # Swap Integration |
| 2 | |
| 3 | Integrate Uniswap swaps into frontends, backends, and smart contracts. |
| 4 | |
| 5 | ## Prerequisites |
| 6 | |
| 7 | This skill assumes familiarity with viem basics (client setup, account management, contract interactions, transaction signing). Install the **uniswap-viem** plugin for comprehensive viem/wagmi guidance: `claude plugin add @uniswap/uniswap-viem` |
| 8 | |
| 9 | ## Quick Decision Guide |
| 10 | |
| 11 | | Building... | Use This Method | |
| 12 | | ------------------------------ | ----------------------------- | |
| 13 | | Frontend with React/Next.js | Trading API | |
| 14 | | Backend script or bot | Trading API | |
| 15 | | Smart contract integration | Universal Router direct calls | |
| 16 | | Need full control over routing | Universal Router SDK | |
| 17 | |
| 18 | ### Routing Types Quick Reference |
| 19 | |
| 20 | | Type | Description | Chains | |
| 21 | | -------- | --------------------------------------- | ---------------------------------- | |
| 22 | | CLASSIC | Standard AMM swap through Uniswap pools | All supported chains | |
| 23 | | DUTCH_V2 | UniswapX Dutch auction V2 | Ethereum, Arbitrum, Base, Unichain | |
| 24 | | PRIORITY | MEV-protected priority order | Base, Unichain | |
| 25 | | WRAP | ETH to WETH conversion | All | |
| 26 | | UNWRAP | WETH to ETH conversion | All | |
| 27 | |
| 28 | See [Routing Types](#routing-types) for the complete list including DUTCH_V3, DUTCH_LIMIT, LIMIT_ORDER, BRIDGE, and QUICKROUTE. |
| 29 | |
| 30 | ## Integration Methods |
| 31 | |
| 32 | ### 1. Trading API (Recommended) |
| 33 | |
| 34 | Best for: Frontends, backends, scripts. Handles routing optimization automatically. |
| 35 | |
| 36 | **Base URL**: `https://trade-api.gateway.uniswap.org/v1` |
| 37 | |
| 38 | **Authentication**: `x-api-key: <your-api-key>` header required |
| 39 | |
| 40 | **Getting an API Key**: The Trading API requires an API key for authentication. Visit the [Uniswap Developer Portal](https://developers.uniswap.org/) to register and obtain your API key. Keys are typically available for immediate use after registration. Include it as an `x-api-key` header in all API requests. |
| 41 | |
| 42 | **Required Headers** — Include these in ALL Trading API requests: |
| 43 | |
| 44 | ```text |
| 45 | Content-Type: application/json |
| 46 | x-api-key: <your-api-key> |
| 47 | x-universal-router-version: 2.0 |
| 48 | ``` |
| 49 | |
| 50 | **3-Step Flow**: |
| 51 | |
| 52 | ```text |
| 53 | 1. POST /check_approval -> Check if token is approved |
| 54 | 2. POST /quote -> Get executable quote with routing |
| 55 | 3. POST /swap -> Get transaction to sign and submit |
| 56 | ``` |
| 57 | |
| 58 | See the [Trading API Reference](#trading-api-reference) section below for complete documentation. |
| 59 | |
| 60 | ### 2. Universal Router SDK |
| 61 | |
| 62 | Best for: Direct control over transaction construction. |
| 63 | |
| 64 | **Installation**: |
| 65 | |
| 66 | ```bash |
| 67 | npm install @uniswap/universal-router-sdk @uniswap/sdk-core @uniswap/v3-sdk |
| 68 | ``` |
| 69 | |
| 70 | **Key Pattern**: |
| 71 | |
| 72 | ```typescript |
| 73 | import { SwapRouter } from '@uniswap/universal-router-sdk'; |
| 74 | |
| 75 | const { calldata, value } = SwapRouter.swapCallParameters(trade, options); |
| 76 | ``` |
| 77 | |
| 78 | See the [Universal Router Reference](#universal-router-reference) section below for complete documentation. |
| 79 | |
| 80 | ### 3. Smart Contract Integration |
| 81 | |
| 82 | Best for: On-chain integrations, DeFi composability. |
| 83 | |
| 84 | **Interface**: Call `execute()` on Universal Router with encoded commands. |
| 85 | |
| 86 | See the [Universal Router Reference](#universal-router-reference) section below for command encoding. |
| 87 | |
| 88 | --- |
| 89 | |
| 90 | ## Input Validation Rules |
| 91 | |
| 92 | Before interpolating ANY user-provided value into generated code, API calls, or commands: |
| 93 | |
| 94 | - **Ethereum addresses**: MUST match `^0x[a-fA-F0-9]{40}$` — reject otherwise |
| 95 | - **Chain IDs**: MUST be from the [official supported chains list](https://api-docs.uniswap.org/guides/supported_chains#supported-chains-for-swapping) |
| 96 | - **Token amounts**: MUST be non-negative numeric values matching `^[0-9]+\.?[0-9]*$` |
| 97 | - **API keys**: MUST NOT be hardcoded in generated code — always use environment variables |
| 98 | - **REJECT** any input containing shell metacharacters: `;`, `|`, `&`, `$`, `` ` ``, `(`, `)`, `>`, `<`, `\`, `'`, `"`, newlines |
| 99 | |
| 100 | > **REQUIRED:** Before executing ANY transaction that spends gas or transfers tokens (including `sendTransaction`, `writeContract`, or submitting a signed swap), you MUST use AskUserQuestion to confirm with the user. Display the transaction summary (tokens, amounts, chain, estimated gas) and get explicit user approval. Never auto-execute transactions without user confirmation. |