Overview
This guide shows how to use Magic’s Express Server Wallet with the Li.Fi API to swap tokens on Base. Li.Fi aggregates DEXs and bridges, finding the best rate across multiple protocols. Your server fetches a quote, handles token approval, signs via the TEE, and broadcasts — the private key never leaves the TEE.Prerequisites
Before starting, ensure you have:- A Magic Secret Key — from your Magic Dashboard
- An OIDC Provider ID — configured for your auth provider (setup guide)
- A Base RPC endpoint (e.g., from Alchemy or QuickNode)
- A user JWT from your authentication provider
How It Works
- Your server authenticates the user and obtains a JWT
- JWT is sent to Magic’s TEE to get (or create) the user’s EOA
- Your server fetches a swap quote from Li.Fi’s
/v1/quoteendpoint - If swapping an ERC-20 token, an approval transaction is signed via the TEE and broadcast
- The swap transaction is signed via the TEE and broadcast
- Li.Fi routes the swap through the best available DEX
TEE Request Helper
All TEE calls use the same authentication headers. Create a reusable helper for your server-side code.TypeScript
Get or Create a Wallet
Fetch the user’s wallet address. If one doesn’t exist, it will be created automatically.TypeScript
Sign and Broadcast Transactions
Build the transaction locally, compute its unsigned hash, sign via the TEE, and broadcast.TypeScript
Getting a Swap Quote
Fetch a quote from Li.Fi to find the best swap route. No API key is required.TypeScript
The
slippage parameter is a decimal — 0.005 means 0.5%. Li.Fi returns toAmountMin which accounts for slippage, so users know the minimum they’ll receive.Token Approval
When swapping an ERC-20 token (not native ETH), approve Li.Fi’s contract to spend the user’s tokens. The approval address comes from the quote response.TypeScript
Executing the Swap
Combine the quote, approval, and transaction into a single flow. ThetransactionRequest from Li.Fi contains everything needed to execute the swap.
TypeScript
Example: Swap ETH for USDC
TypeScript
Example: Swap USDC for ETH
TypeScript
When swapping USDC for ETH, the approval step runs automatically. When swapping ETH for USDC, no approval is needed since ETH is the native token.
Token Addresses
Common tokens on Base (chain ID 8453):TypeScript
Li.Fi also accepts token symbols (e.g.,
"ETH", "USDC") instead of addresses, but using addresses is more reliable to avoid ambiguity.TEE Endpoints Used
| Endpoint | Purpose |
|---|---|
POST /v1/wallet | Get or create an EOA wallet |
POST /v1/wallet/sign/data | Sign a raw transaction hash |
Troubleshooting
TEE returns 401 or 403
TEE returns 401 or 403
Symptoms: Authentication errors when calling TEE endpoints.Solutions:
- Verify the JWT token is valid and not expired
- Check that
X-Magic-Secret-Keymatches your dashboard credentials - Ensure the OIDC Provider ID is correct
- Confirm your domain is allowlisted in the Magic Dashboard
Quote returns no route
Quote returns no route
Symptoms: The
/quote endpoint returns an error or empty result.Solutions:- Verify the token addresses are correct for Base (chain ID 8453)
- Check that
fromAmountis in the smallest unit (e.g., wei for ETH, 6 decimals for USDC) - Ensure the amount is large enough — very small swaps may not have viable routes
- Try increasing
slippageif the market is volatile
Swap transaction reverts
Swap transaction reverts
Symptoms: The transaction is sent but reverts on-chain.Solutions:
- Check that the token approval was confirmed before sending the swap
- The quote may have expired — quotes are only valid for a short time, so fetch a new one and retry
- Ensure the wallet has enough ETH for gas fees on Base
Approval transaction fails
Approval transaction fails
Symptoms: The ERC-20 approval transaction fails.Solutions:
- Verify the wallet holds the token being swapped
- Check that the
approvalAddressfrom the quote is being used - Ensure the wallet has ETH for the approval gas fee
Nonce errors
Nonce errors
Symptoms: Transaction fails with a nonce-related error.Solutions:
- Ensure no concurrent transactions are being sent for the same wallet
- If a previous transaction is pending, wait for it to confirm
- Fetch the nonce with
"pending"to account for in-flight transactions
Resources
Express API Docs
Learn about Magic’s Express Server Wallet API
Li.Fi Documentation
Official Li.Fi API and SDK documentation
EVM Data Preparation
Guide for preparing EVM transaction data for signing
Base Documentation
Official Base network documentation