Quickstart Guide
This guide will walk you through creating your first blockchain account and sending a transaction using the CDP SDK.
Complete Example
Here’s a complete working example that creates an account, requests testnet funds, and sends a transaction:
import { CdpClient } from "@coinbase/cdp-sdk";
import { parseEther, createPublicClient, http } from "viem";
import { baseSepolia } from "viem/chains";
import dotenv from "dotenv";
dotenv.config();
const publicClient = createPublicClient({
chain: baseSepolia,
transport: http(),
});
const cdp = new CdpClient();
// Step 1: Create an account
const account = await cdp.evm.createAccount();
console.log(`Created account: ${account.address}`);
// Step 2: Request testnet funds
const faucetResp = await cdp.evm.requestFaucet({
address: account.address,
network: "base-sepolia",
token: "eth",
});
await publicClient.waitForTransactionReceipt({
hash: faucetResp.transactionHash,
});
console.log("Received testnet ETH");
// Step 3: Send a transaction
const { transactionHash } = await cdp.evm.sendTransaction({
address: account.address,
network: "base-sepolia",
transaction: {
to: "0x4252e0c9A3da5A2700e7d91cb50aEf522D0C6Fe8",
value: parseEther("0.000001"),
},
});
await publicClient.waitForTransactionReceipt({ hash: transactionHash });
console.log(
`Transaction confirmed! https://sepolia.basescan.org/tx/${transactionHash}`
);
Step-by-Step Breakdown
Step 1: Initialize the Client
First, create a CDP client instance. The client will automatically use credentials from environment variables:
import { CdpClient } from "@coinbase/cdp-sdk";
const cdp = new CdpClient();
The client automatically reads CDP_API_KEY_ID, CDP_API_KEY_SECRET, and CDP_WALLET_SECRET from your environment variables.
Step 2: Create an Account
Create a new blockchain account. CDP manages the private key securely:
const account = await cdp.evm.createAccount();
console.log(`Account address: ${account.address}`);
You can also name your accounts for easier management:
const account = await cdp.evm.createAccount({
name: "MyFirstAccount"
});
Step 3: Request Testnet Funds
For testing, request free testnet tokens from the CDP faucet:
const faucetResp = await cdp.evm.requestFaucet({
address: account.address,
network: "base-sepolia",
token: "eth",
});
// Wait for the faucet transaction to complete
await publicClient.waitForTransactionReceipt({
hash: faucetResp.transactionHash,
});
Faucet functionality is only available on testnets. For mainnet, you’ll need to fund accounts through other means.
Step 4: Send a Transaction
Now send a transaction on the blockchain:
import { parseEther } from "viem";
const { transactionHash } = await cdp.evm.sendTransaction({
address: account.address,
network: "base-sepolia",
transaction: {
to: "0x4252e0c9A3da5A2700e7d91cb50aEf522D0C6Fe8",
value: parseEther("0.000001"),
},
});
// Wait for confirmation
await publicClient.waitForTransactionReceipt({ hash: transactionHash });
CDP automatically manages gas fees and nonces for you!
Solana Example
Here’s the same flow for Solana:
import { CdpClient } from "@coinbase/cdp-sdk";
import {
PublicKey,
SystemProgram,
SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
Transaction,
} from "@solana/web3.js";
const cdp = new CdpClient();
// Create account
const account = await cdp.solana.createAccount();
console.log(`Created account: ${account.address}`);
// Request testnet SOL
await cdp.solana.requestFaucet({
address: account.address,
token: "sol",
});
// Create and send transaction
const transaction = new Transaction();
transaction.add(
SystemProgram.transfer({
fromPubkey: new PublicKey(account.address),
toPubkey: new PublicKey("3KzDtddx4i53FBkvCzuDmRbaMozTZoJBb1TToWhz3JfE"),
lamports: 10000,
})
);
transaction.recentBlockhash = SYSVAR_RECENT_BLOCKHASHES_PUBKEY.toBase58();
transaction.feePayer = new PublicKey(account.address);
const serializedTx = Buffer.from(
transaction.serialize({ requireAllSignatures: false })
).toString("base64");
const txResult = await cdp.solana.sendTransaction({
network: "solana-devnet",
transaction: serializedTx,
});
console.log(`Transaction: https://explorer.solana.com/tx/${txResult.signature}?cluster=devnet`);
Transferring Tokens
CDP SDK provides convenient methods for transferring tokens:
import { parseUnits } from "viem";
// Transfer USDC
const { transactionHash } = await account.transfer({
to: "0x9F663335Cd6Ad02a37B633602E98866CF944124d",
amount: parseUnits("0.01", 6), // USDC has 6 decimals
token: "usdc",
network: "base-sepolia",
});
What’s Next?
Smart Accounts
Learn about account abstraction and gasless transactions
Token Swaps
Swap tokens directly in your application
Policy Management
Add transaction rules and permissions
Examples
Explore more code examples