Skip to main content
On Solana, SPL token transfers require the recipient to have an Associated Token Account (ATA) for that token. Gas Station does not pay for ATA creation unless you use Solana ATA sponsorship. Without sponsorship, you must create the ATA and pay its rent before a transfer can succeed. This quickstart walks you through a server-side script that creates a USDC ATA. A payer wallet pays the one-time SOL rent, and an owner wallet owns the ATA and receives USDC.
All transactions in this guide take place on Solana Devnet. No real funds are required beyond testnet SOL for rent and fees. You can adapt the code for mainnet by setting CLUSTER to 'mainnet-beta' and using mainnet USDC mint addresses.

Prerequisites

Before you begin, ensure that you’ve:
  • Installed Node.js v18+.
  • Created a payer wallet and obtained its keypair (private key).
  • Funded the payer wallet with at least ~0.00204 SOL on Solana Devnet to cover ATA rent-exempt minimum and transaction fees:
  • Obtained the owner wallet’s public key (base58 address). The owner can be a wallet you create or any recipient’s address.

Step 1: Set up the project

This step sets up your project environment and installs the required dependencies.

1.1. Create a new project

Create a new directory and initialize a new Node.js project with default settings:
Shell

1.2. Install dependencies

Install the required dependencies for Solana and SPL token interactions, and set the start script:
Shell

1.3. Configure TypeScript (optional)

This step is optional. It helps prevent missing types in your IDE or editor.
Create a tsconfig.json file:
Then, update the tsconfig.json file:

1.4. Configure environment variables

Create a .env file with PAYER_PRIVATE_KEY (payer keypair as a JSON array) and OWNER_PUBLIC_KEY (base58 address of the wallet that will own the ATA):
Shell
This example uses one or more private keys for local testing. In production, use a secure key management solution and never expose or share private keys.
The PAYER_PRIVATE_KEY should be a JSON array of bytes representing your private key. You can export this from most Solana wallets.
Some wallets export Solana private keys as Base58 encoded strings. If you have a Base58 encoded private key, install bs58, save the following code as convert-key.ts, and run it to convert it to a JSON array:
Shell
TypeScript

Step 2: Create the script

This step creates the complete script. It adds imports and configuration, implements ATA creation, and runs the script.

2.1. Import dependencies

Create an index.ts file:
Shell
Then, add the imports and configuration constants:
index.ts

2.2. Add the ATA creation logic

Add the following code to index.ts. This code defines the createUSDCata function and the main function.
The createUSDCata function:
  • Derives the ATA address.
  • Builds the idempotent ATA instruction.
  • Sends the transaction.
The main function:
  • Loads the payer keypair and owner address from .env.
  • Initializes the Solana connection.
  • Calls createUSDCata.
  • Prints the final ATA address.
The payer covers the ATA rent-exempt amount and transaction fees.
index.ts

2.3. Run the script

Run the script with the following command:
Shell
You should see output similar to:
Open the Solana Explorer link to verify the ATA onchain. Run the script again to confirm idempotent behavior: the transaction still succeeds and the ATA address is unchanged.