@stellar/stellar-sdk script
you build in this guide will:
- Create and fund a recipient Stellar Testnet wallet
- Establish a USDC trustline on the recipient account
- Transfer USDC from your existing sender wallet to the recipient
Before a Stellar account can receive USDC, it must establish a trustline for the
asset. See
Set up a USDC trustline on Stellar
for more detail. This quickstart handles trustline setup automatically as part
of the script.
Prerequisites
Before you begin, ensure that you have:- Installed Node.js v22.6+
- Set up a terminal and code editor for running commands and editing files
- Created a Stellar Testnet wallet with the secret key (
S...) available- Funded with testnet XLM (for transaction fees) from Stellar Friendbot
- Funded with testnet USDC from the Circle Faucet
Contract addresses
You need the following Stellar Testnet USDC issuer address:Step 1: Set up the project
1.1. Create the project and install dependencies
Create a new directory and install the required dependencies:Shell
1.2. Configure TypeScript (optional)
Create atsconfig.json file:
tsconfig.json file:
1.3. Configure environment variables
Create a.env file in your project directory and add your sender’s secret key:
.env
Step 2: Create the transfer script
Addmain.ts at the project root. The script:
- Loads your existing sender wallet from the environment variable
- Creates and funds a new recipient wallet using Friendbot
- Establishes a USDC trustline on the recipient account
- Submits a USDC payment from the sender to the recipient
main.ts
Step 3: Run the script
From the project directory, run:Shell
Shell
Common errors you might encounter:
Friendbot funding failed: 400— The Friendbot rate limit was exceeded. Wait a few seconds and try again.op_no_trust— The recipient account does not have a USDC trustline. The script establishes one automatically, but if you’re adapting this example for your own recipient address, ensure the trustline exists first. See Set up a USDC trustline on Stellar.op_underfunded— The sender does not have enough USDC. Ensure your sender wallet is funded with testnet USDC from the Circle Faucet before running the script.