You can assign a name to an account to make it easier to access. Account names can consist of alphanumeric characters and hyphens, and must be between 2 and 36 characters long. Account names must be unique within a single CDP project for each account type (e.g., all Solana accounts).
You can assign an account name at the time of account creation, and retrieve it later using the name. The getOrCreateAccount method will create an account if it doesn’t exist, and return the existing account if it does.
You can also create and manage smart accounts that are owned by an EVM account. Each owner account can only have one smart account associated with it. The getOrCreateSmartAccount method will create a smart account if it doesn’t exist for the owner, and return the existing smart account if it does.
Copy
Ask AI
import { CdpClient } from "@coinbase/cdp-sdk";import "dotenv/config";const cdp = new CdpClient();// Create a couple of owners, one for each smart account we will create.const firstOwner = await cdp.evm.getOrCreateAccount({ name: "ExampleOwner1"});const secondOwner = await cdp.evm.getOrCreateAccount({ name: "ExampleOwner2"});const account = await cdp.evm.createSmartAccount({ owner: firstOwner,});console.log("Created Smart Account. Address:", account.address);const namedSmartAccount = await cdp.evm.getOrCreateSmartAccount({ name: "MySmartAccount", owner: secondOwner});console.log("Created Smart Account:", sameAccount.address);
You can retrieve a specific account by its address or name using the getAccount method.
Copy
Ask AI
import { CdpClient } from "@coinbase/cdp-sdk";import "dotenv/config";const cdp = new CdpClient();// Returns the account with the given address, if it exists.const account = await cdp.evm.getAccount({ address: "0x1234567890123456789012345678901234567890"});// Returns the account with the given name, if it exists.const namedAccount = await cdp.evm.getAccount({ name: "MyAccount"});
You can attach policies to accounts to govern their behavior, such as restricting transactions to specific addresses or limiting transaction values. Policies can be attached during account creation or added later using the updateAccount method:
Copy
Ask AI
import { CdpClient } from "@coinbase/cdp-sdk";import "dotenv/config";const cdp = new CdpClient();// Get an existing accountconst account = await cdp.evm.getOrCreateAccount({ name: "policy-account"});const policyId = "your-policy-id"; // Replace with your actual policy ID// Attach a policy to the accountconst updatedAccount = await cdp.evm.updateAccount({ address: account.address, update: { accountPolicy: policyId, },});console.log(`Updated account ${updatedAccount.address} with policy: ${updatedAccount.policies}`);
To learn more about creating and managing policies, see the Policies documentation.
You can also manage account in the CDP Portal Account Manager UI. From here you can
view your accounts by chain and type, and you can click into an account to view more information like balances and policies.
Assistant
Responses are generated using AI and may contain mistakes.