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 a swap quote
  • Generating transaction bytecode
  • Executing the swap transaction
  • 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 Swap Quote

3. Generate Bytecode

4. 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 POST /v2/swap/bytecode instead.

Error Handling

Running the Example

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