Solana accounts in the CDP SDK are server-managed accounts that provide secure key management for the Solana blockchain. These accounts support all standard Solana operations including transfers, transaction signing, and message signing.
Creating a Solana Account
Initialize the CDP Client
Set up your CDP client:import { CdpClient } from "@coinbase/cdp-sdk";
const cdp = new CdpClient();
from cdp import CdpClient
async with CdpClient() as cdp:
# Your code here
Create a Solana account
Create a new Solana account with an optional name:const account = await cdp.solana.createAccount({
name: "MySolanaAccount"
});
console.log(`Created Solana account: ${account.address}`);
account = await cdp.solana.create_account(name="MySolanaAccount")
print(f"Created Solana account: {account.address}")
Create with a policy
Apply a Solana-specific policy during creation:const account = await cdp.solana.createAccount({
name: "RestrictedSolanaAccount",
accountPolicy: "pol_sol_123..."
});
account = await cdp.solana.create_account(
name="RestrictedSolanaAccount",
account_policy="pol_sol_123..."
)
Retrieving Accounts
Get or Create Pattern
const account = await cdp.solana.getOrCreateAccount({
name: "MySolanaAccount"
});
account = await cdp.solana.get_or_create_account(
name="MySolanaAccount"
)
Get by Address or Name
// By address
const account = await cdp.solana.getAccount({
address: "9aE4..."
});
// By name
const account = await cdp.solana.getAccount({
name: "MySolanaAccount"
});
# By address
account = await cdp.solana.get_account(address="9aE4...")
# By name
account = await cdp.solana.get_account(name="MySolanaAccount")
List All Accounts
const response = await cdp.solana.listAccounts({
pageSize: 50,
pageToken: "optional-token"
});
for (const account of response.accounts) {
console.log(`${account.name}: ${account.address}`);
}
response = await cdp.solana.list_accounts(
page_size=50,
page_token="optional-token"
)
for account in response.accounts:
print(f"{account.name}: {account.address}")
Updating Accounts
Update account name or policies:
const updatedAccount = await cdp.solana.updateAccount({
address: account.address,
name: "NewSolanaName",
accountPolicy: "pol_sol_xyz..." // Optional
});
updated_account = await cdp.solana.update_account(
address=account.address,
name="NewSolanaName",
account_policy="pol_sol_xyz..." # Optional
)
Transferring SOL and SPL Tokens
Transfer SOL or SPL tokens to another address:
// Transfer SOL
const signature = await account.transfer({
to: "9aE4...",
amount: 1000000n, // 0.001 SOL (in lamports)
token: "sol",
network: "solana-devnet"
});
// Transfer USDC (SPL token)
const signature = await account.transfer({
to: "9aE4...",
amount: 1000000n, // 1 USDC (6 decimals)
token: "usdc",
network: "solana-devnet"
});
console.log(`Transaction signature: ${signature}`);
# Transfer SOL
signature = await account.transfer(
to="9aE4...",
amount=1000000, # 0.001 SOL (in lamports)
token="sol",
network="solana-devnet"
)
# Transfer USDC (SPL token)
signature = await account.transfer(
to="9aE4...",
amount=1000000, # 1 USDC (6 decimals)
token="usdc",
network="solana-devnet"
)
print(f"Transaction signature: {signature}")
SOL uses 9 decimal places (1 SOL = 1,000,000,000 lamports). Most SPL tokens use 6 or 9 decimals. Check the token’s decimals before transferring.
Signing Transactions
Sign a Solana transaction (base64-encoded):
const response = await account.signTransaction({
transaction: "base64-encoded-transaction",
idempotencyKey: "unique-key-123"
});
console.log(`Signed transaction: ${response.signedTransaction}`);
response = await account.sign_transaction(
transaction="base64-encoded-transaction",
idempotency_key="unique-key-123"
)
print(f"Signed transaction: {response.signed_transaction}")
Signing Messages
Sign arbitrary messages (off-chain signatures):
const response = await account.signMessage({
message: "Hello, Solana!",
idempotencyKey: "unique-key-456"
});
console.log(`Message signature: ${response.signature}`);
response = await account.sign_message(
message="Hello, Solana!",
idempotency_key="unique-key-456"
)
print(f"Message signature: {response.signature}")
Requesting Test Tokens
For devnet testing, request SOL or USDC from the faucet:
// Request SOL
const response = await account.requestFaucet("sol");
console.log(`Faucet transaction: ${response.transactionHash}`);
// Request USDC
const response = await account.requestFaucet("usdc");
# Request SOL
response = await account.request_faucet(token="sol")
print(f"Faucet transaction: {response.transaction_hash}")
# Request USDC
response = await account.request_faucet(token="usdc")
Faucets are only available on devnet and testnet networks.
Account Properties
The Solana public key address (Base58-encoded)
Optional name for the account
List of policy IDs applied to this account
Importing and Exporting
Import Account
const account = await cdp.solana.importAccount({
privateKey: "base58-encoded-private-key",
name: "ImportedSolanaAccount"
});
account = await cdp.solana.import_account(
private_key="base58-encoded-private-key",
name="ImportedSolanaAccount"
)
Export Account
const privateKey = await cdp.solana.exportAccount({
address: account.address
});
console.log(`Private key: ${privateKey}`);
private_key = await cdp.solana.export_account(
address=account.address
)
print(f"Private key: {private_key}")
Troubleshooting
Insufficient Lamports
Ensure your account has enough SOL to cover transaction fees (rent + gas):
// Request SOL from faucet on devnet
await account.requestFaucet("sol");
Transaction Failed
Solana transactions can fail due to:
- Insufficient funds for rent or fees
- Invalid program instructions
- Network congestion
- Blockhash expiration
Always check the transaction signature on a Solana explorer for detailed error messages.
Policy Restrictions
If a transaction is blocked, check the account’s applied policies. Solana policies can restrict:
- Destination addresses
- Program interactions
- Token transfers
- Maximum transaction amounts
Next Steps