If you're looking to contribute to the SDK, please see the Contributing Guide.
This module contains the TypeScript CDP SDK, which is a library that provides a client for interacting with the Coinbase Developer Platform (CDP). It includes a CDP Client for interacting with EVM and Solana APIs to create accounts and send transactions, as well as authentication tools for interacting directly with the CDP APIs.
npm install @coinbase/cdp-sdk
To start, create a CDP API Key. Save the API Key ID
and API Key Secret
for use in the SDK. You will also need to create a wallet secret in the Portal to sign transactions.
One option is to export your CDP API Key and Wallet Secret as environment variables:
export CDP_API_KEY_ID="YOUR_API_KEY_ID"
export CDP_API_KEY_SECRET="YOUR_API_KEY_SECRET"
export CDP_WALLET_SECRET="YOUR_WALLET_SECRET"
Then, initialize the client:
import { CdpClient } from "@coinbase/cdp-sdk";
const cdp = new CdpClient();
.env
fileAnother option is to save your CDP API Key and Wallet Secret in a .env
file:
touch .env
echo "CDP_API_KEY_ID=YOUR_API_KEY_ID" >> .env
echo "CDP_API_KEY_SECRET=YOUR_API_KEY_SECRET" >> .env
echo "CDP_WALLET_SECRET=YOUR_WALLET_SECRET" >> .env
Then, load the client config from the .env
file:
import { CdpClient } from "@coinbase/cdp-sdk";
import dotenv from "dotenv";
dotenv.config();
const cdp = new CdpClient();
Another option is to directly pass the API Key and Wallet Secret to the client:
const cdp = new CdpClient({
apiKeyId: "YOUR_API_KEY_ID",
apiKeySecret: "YOUR_API_KEY_SECRET",
walletSecret: "YOUR_WALLET_SECRET",
});
const evmAccount = await cdp.evm.createAccount();
const solanaAccount = await cdp.solana.createAccount();
You can use the faucet function to request testnet ETH or SOL from the CDP.
const faucetResp = await cdp.evm.requestFaucet({
address: evmAccount.address,
network: "base-sepolia",
token: "eth",
});
const faucetResp = await cdp.solana.requestFaucet({
address: fromAddress,
token: "sol",
});
You can use CDP SDK to send transactions on EVM networks.
import { CdpClient } from "@coinbase/cdp-sdk";
import { parseEther, createPublicClient, http } from "viem";
import { baseSepolia } from "viem/chains";
const publicClient = createPublicClient({
chain: baseSepolia,
transport: http(),
});
const cdp = new CdpClient();
const account = await cdp.evm.createAccount();
const faucetResp = await cdp.evm.requestFaucet({
address: account.address,
network: "base-sepolia",
token: "eth",
});
const faucetTxReceipt = await publicClient.waitForTransactionReceipt({
hash: faucetResp.transactionHash,
});
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! Explorer link: https://sepolia.basescan.org/tx/${transactionHash}`,
);
CDP SDK is fully viem-compatible, so you can optionally use a walletClient
to send transactions.
import { CdpClient } from "@coinbase/cdp-sdk";
import { parseEther, createPublicClient, http, createWalletClient, toAccount } from "viem";
import { baseSepolia } from "viem/chains";
const publicClient = createPublicClient({
chain: baseSepolia,
transport: http(),
});
const cdp = new CdpClient();
const account = await cdp.evm.createAccount();
const faucetResp = await cdp.evm.requestFaucet({
address: account.address,
network: "base-sepolia",
token: "eth",
});
const faucetTxReceipt = await publicClient.waitForTransactionReceipt({
hash: faucetResp.transactionHash,
});
const walletClient = createWalletClient({
account: toAccount(serverAccount),
chain: baseSepolia,
transport: http(),
});
// Step 3: Sign the transaction with CDP and broadcast it using the wallet client.
const hash = await walletClient.sendTransaction({
to: "0x4252e0c9A3da5A2700e7d91cb50aEf522D0C6Fe8",
value: parseEther("0.000001"),
});
console.log(`Transaction confirmed! Explorer link: https://sepolia.basescan.org/tx/${hash}`);
For Solana, we recommend using the @solana/web3.js
library to send transactions. See the examples.
For EVM, we support Smart Accounts which are account-abstraction (ERC-4337) accounts. Currently there is only support for Base Sepolia and Base Mainnet for Smart Accounts.
const evmAccount = await cdp.evm.createAccount();
const smartAccount = await cdp.evm.createSmartAccount({
owner: evmAccount,
});
const userOperation = await cdp.evm.sendUserOperation({
smartAccount: smartAccount,
network: "base-sepolia",
calls: [
{
to: "0x0000000000000000000000000000000000000000",
value: parseEther("0.000001"),
data: "0x",
},
],
});
const userOperation = await cdp.sendUserOperation({
smartAccount: smartAccount,
network: "base-sepolia",
calls: [
{
to: "0x0000000000000000000000000000000000000000",
value: parseEther("0"),
data: "0x",
},
],
paymasterUrl: "https://some-paymaster-url.com",
});
This SDK also contains simple tools for authenticating REST API requests to the Coinbase Developer Platform (CDP). See the Auth README for more details.
This SDK contains error reporting functionality that sends error events to the CDP. If you would like to disable this behavior, you can set the DISABLE_CDP_ERROR_REPORTING
environment variable to true
.
DISABLE_CDP_ERROR_REPORTING=true
This project is licensed under the MIT License - see the LICENSE file for details.
For feature requests, feedback, or questions, please reach out to us in the #cdp-sdk channel of the Coinbase Developer Platform Discord.
If you discover a security vulnerability within this SDK, please see our Security Policy for disclosure information.