Skip to main content
This example demonstrates how to swap tokens on the same blockchain network using the Pods API with JavaScript/TypeScript.

Overview

This example covers:
  • Getting an executable quote in one call (GET /v2/swap/quote with wallet addresses)
  • Executing the swap as one atomic EIP-7702 batch
  • Handling approvals

Prerequisites

  • Node.js 18+
  • Pods API key
  • Ethereum wallet with tokens to swap
  • Basic understanding of async/await

Choose Your Library

Using ethers.js v6 for blockchain interactions - the most popular Ethereum library.

Installation

Environment Variables

Create a .env file:
Never commit your .env file to version control. Add it to .gitignore.

Step-by-Step Walkthrough

1. Initialize API Client

2. Get an Executable Quote (one call)

Pass originAddress and destinationAddress on the quote so the response already includes executable transactionData. New integrations should not call the legacy POST /v2/swap/bytecode.

3. Execute Transactions (atomic batch)

Batch all transactionData legs into one transaction on the origin chain. See Executing Bytecode and same-chain-swap.js (EIP-7702 via eip7702-batch.js).
Smart accounts: request output=userOperation on the quote (GET /v2/swap/quote) instead.

Error Handling

Running the Example

Download both files into the same folder to run this example: same-chain-swap.js and the shared EIP-7702 batch helper eip7702-batch.js that it imports.

Expected Output

Next Steps

Cross-Chain Swap

Execute cross-chain token swaps

Strategy Deposit

Deposit into yield strategies

Swap Guide

Learn more about swaps

API Reference

View complete API documentation