In a few minutes you’ll swap a little ETH for USDC on Base using the Tern API. It’s the shortest path to a real swap: one quote, one transaction, one status to follow. No API key required.

Before you start

  • Node.js 18 or later.
  • A wallet with about $5 of ETH on Base. Use a wallet made for testing, and keep its private key in an environment variable.
  • viem, to send the transaction: npm install viem.
Steps 2 to 4 build one script. Save them in swap.ts and run it with npx tsx swap.ts.
1

Get a price

Ask what 0.001 ETH is worth in USDC right now. Amounts are whole numbers in the token’s smallest unit, so 0.001 ETH is 1000000000000000 wei.
The user would get about 2.05 USDC (to.amount), and never less than 2.03 (to.minAmount). The swap costs $0.01 and takes about 8 seconds. This is just a price: nothing is reserved yet.
2

Get a quote you can execute

Add who sends and who receives. The answer now has an id and the transaction to send.
The quote is good for about a minute, so send the transaction right away.
3

Send the transaction

Send what the quote lists in execute.transactions, in order:
When selling ETH, that’s a single transaction. Selling a token adds an approval before it.
4

Track it

Check the quote’s status until it’s final:
completed means the USDC is in the wallet. destination.amount is exactly how much arrived.
That’s a full swap: a quote, a transaction, and a tracked result. Cross-chain swaps follow the same pattern, with one extra step that makes them safe across two chains.

Next steps

Cross-chain swaps

Swap between chains, funded from a wallet or a deposit address.

How Tern works

What happens behind a swap, and why it can’t half-finish.

Fees and pricing

What a swap costs, and what to show your users.

API reference

Every endpoint, with API keys, limits, and errors.