Prerequisites
Before you begin, ensure that you’ve:- Installed Node.js v22+
- Prepared a testnet wallet on the selected chain funded with:
- Testnet USDC for the transfer
- Testnet native tokens for gas fees
You can get testnet USDC from Circle’s faucet.
Step 1. Set up the project
This step shows you how to prepare your project and environment.1.1. Set up your development environment
Create a new directory and install the required dependencies:Shell
1.2. Initialize and configure the project
This command creates atsconfig.json file:
Shell
tsconfig.json file:
Shell
1.3. Configure environment variables
Create a.env file in the project directory and add your wallet private key,
replacing {YOUR_PRIVATE_KEY} with the private key from your EVM wallet and
{YOUR_RECIPIENT_ADDRESS} with the address of the recipient.
Shell
Step 2: Create the transfer script
In this step, you’ll build a script in TypeScript that transfers USDC on the same EVM testnet. You’ll configure the chain and contract, load environment variables, initialize Viem clients, and implement the transfer logic.2.1. Create the script file
2.2. Configure chain and contract
Inindex.ts, import required modules and define constants for your selected
EVM chain and USDC contract:
Learn more: Chain and contract configuration
Learn more: Chain and contract configuration
- Imports required modules from Viem and your
.envfile. - Loads the selected EVM testnet configuration. - Defines the USDC contract address, decimals and minimal ABI. - Only includes thebalanceOfandtransferfunctions, since they’re all that’s needed for this guide.
2.3. Load environment variables
Next, load your private key and recipient address from.env:
Typescript
Learn more: Environment variables
Learn more: Environment variables
- Loads values from the
.envfile so you don’t hardcode sensitive information. - Checks that both the private key and recipient address are set. - Validates the recipient address format with a regex. - Ensures the private
key is properly prefixed with
0xso Viem can use it.
2.4. Initialize Viem clients
Create a public client for reading blockchain data and a wallet client for sending transactions:TypeScript
Learn more: Viem clients
Learn more: Viem clients
privateKeyToAccountcreates an account object from your private key. -createPublicClientis used for reading data from the blockchain (no private key needed). -createWalletClientuses your account to sign and send transactions. - Together, these clients give you read and write access to the selected EVM testnet.
2.5. Implement the transfer logic
Now, write the main logic to check your balance and send a transfer:TypeScript
Learn more: Transfer logic
Learn more: Transfer logic
- Reads your USDC balance using the
balanceOffunction. - Converts the balance into a human-readable format withformatUnits. - Sets the transfer amount (1 USDC in this example). - Validates that your balance is sufficient. - Converts the transfer amount back into smallest units with
parseUnits. - Calls thetransferfunction on the USDC contract to send tokens. - Logs the transaction hash and a block explorer link so you can verify the transfer. balance into a human-readable format withformatUnits. - Sets the transfer amount (1 USDC in this example).
Step 3: Run the script
Run the script using the following command:Shell
Explorer: line
and open it in your browser. This will take you to the testnet’s block explorer,
where you can view full transaction details.
Summary
In this quickstart, you learned how to check balances and transfer USDC on EVM testnets using Viem and Node.js. Here are the key points to remember:- Testnet only. Testnet USDC has no real value.
- Gas fees. You need a small amount of the testnet’s native token for gas.
- Security. Keep your private key in
.env. Never commit secrets. - Minimal ABI. The script only uses
balanceOfandtransferfor simplicity.