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.
import { CdpClient } from "@coinbase/cdp-sdk";import dotenv from "dotenv";dotenv.config();const cdp = new CdpClient();const accountName = "my-account";// If the account doesn't exist, it will be created.let evmAccount = await cdp.evm.getOrCreateAccount({name: accountName});console.log(`Created account with name ${evmAccount.name}.`);// If the account already exists, it will be retrieved.evmAccount = await cdp.evm.getOrCreateAccount({name: accountName});console.log(`Retrieved account with name ${evmAccount.name}.`);
You can also create and manage smart accounts that are owned by Externally Owned Accounts. 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 from "dotenv"; dotenv.config(); const cdp = new CdpClient(); const owner = await cdp.evm.getOrCreateAccount(); console.log("Created owner account:", owner.address); // Get or create a smart account with the owner // Note: Each owner can only have one smart account const account = await cdp.evm.getOrCreateSmartAccount({ name: "MyAccount", owner }); console.log("EVM Smart Account Address:", account.address); // Subsequent calls to getOrCreateSmartAccount with the same owner will return the existing account const sameAccount = await cdp.evm.getOrCreateSmartAccount({ name: "MyAccount", owner }); console.log("Retrieved same account:", sameAccount.address);