Coinbase Developer Platform (CDP) SDK

repository·main·Indexed 41 days ago

https://github.com/coinbase/cdp-sdk

A collection of client libraries for TypeScript, Python, Go, Rust, and Java designed to help developers programmatically manage EVM and Solana wallets by offloading private key security to the Coinbase Developer Platform.

Tokens
49.9K
Snippets
147
Records
214
Agent score
52%

What's inside CDP SDK

  1. Overview of the CDP TypeScript SDK

    main

    The @coinbase/cdp-sdk is a client library for interacting with the Coinbase Developer Platform (CDP). It provides high-level abstractions for:

    • Account Management: Creating and managing EVM and Solana accounts.
    • Transaction Execution: Sending transactions on EVM and Solana networks.
    • Policy Governance: Using policy APIs to govern transaction permissions and spend controls.
    • Authentication: Tools for interacting directly with CDP APIs.
  2. Overview of the TypeScript CDP SDK

    main

    The TypeScript CDP SDK provides a client for interacting with the Coinbase Developer Platform (CDP). It is designed to help developers manage blockchain assets and permissions through several core capabilities:

    • CDP Client: Used for interacting with EVM and Solana APIs to create accounts and send transactions.
    • Policy APIs: Used to govern transaction permissions.
    • Authentication Tools: Used for interacting directly with the CDP APIs.

    For more detailed information, refer to the Wallet API v2 and the API Reference.

  3. Overview of the CDP SDK

    main
    The Coinbase Developer Platform (CDP) SDK provides client libraries that allow developers to programmatically create, manage, and use crypto wallets. A key feature of the SDK is that it enables wallet management while relying on the Coinbase Developer Platform to secure the underlying private keys, reducing the developer's responsibility for key security.
  4. Overview of the CDP Python SDK

    main

    The CDP Python SDK provides a client for interacting with the Coinbase Developer Platform (CDP). Key capabilities include:

    • Account Management: Creating and exporting EVM and Solana accounts.
    • Transaction Execution: Sending transactions and transferring tokens.
    • Policy Management: Using policy APIs to govern transaction permissions.
    • Authentication: Tools for interacting directly with CDP APIs.
    • Advanced Features: Support for EVM Smart Accounts, EIP-7702 delegation, and EVM Swaps.
  5. Overview of the Python cdp package modules

    main

    The cdp Python package provides a comprehensive suite of modules for interacting with the Coinbase Developer Platform. Key functional areas include:

    • Core Clients: cdp.cdp_client for general CDP operations and cdp.api_clients for specific API interactions.
    • EVM Support: Extensive modules for Ethereum Virtual Machine (EVM) operations, including cdp.evm_client, cdp.evm_local_account, cdp.evm_server_account, cdp.evm_smart_account, and modules for handling evm_call_types, evm_message_types, evm_transaction_types, and evm_token_balances.
    • Solana Support: Modules for Solana-specific operations via cdp.solana_account and cdp.solana_client.
    • Authentication & Security: Subpackages for cdp.auth and cdp.x402 (likely related to the x402 protocol).
    • Utilities & Constants: Core supporting modules like cdp.constants, cdp.utils, cdp.analytics, and cdp.export.
  6. Explore x402 Payment Protocol examples

    main

    The x402/ directory contains examples for paying for and gating x402-protected APIs using CDP-managed wallets.

    Key example types include:

    • API Clients: Using CdpX402Client to pay for protected APIs, including variations using axios, implementing spend controls (caps and allowlists), and migrating from self-managed private keys to CDP signers.
    • MCP Integration: Paying for MCP tool calls directly or via Claude-driven chatbots.
    • Resource Servers: Implementing gated routes using createX402Server with the CDP-hosted facilitator across different frameworks like Express, Hono, Next.js (App Router), and MCP servers.

    For detailed setup, funding instructions, and environment variables specific to x402, refer to the x402/README.md.

  7. Explore the Python CDP SDK API submodules

    main

    The cdp.openapi_client.api package provides access to various Coinbase Developer Platform (CDP) services through specialized submodules. Depending on your use case, you should import from the following modules:

    • EVM Accounts: cdp.openapi_client.api.evm_accounts_api for managing Ethereum Virtual Machine accounts.
    • EVM Smart Accounts: cdp.openapi_client.api.evm_smart_accounts_api for interacting with smart account abstractions on EVM chains.
    • EVM Swaps: cdp.openapi_client.api.evm_swaps_api for executing token swaps on EVM chains.
    • EVM Token Balances: cdp.openapi_client.api.evm_token_balances_api for querying token balances on EVM chains.
    • Faucets: cdp.openapi_client.api.faucets_api for requesting testnet tokens.
    • Payments (Alpha): cdp.openapi_client.api.payments_alpha_api for accessing early-access payment features.
    • Policy Engine: cdp.openapi_client.api.policy_engine_api for managing security or operational policies.
    • Solana Accounts: cdp.openapi_client.api.solana_accounts_api for managing Solana network accounts.
  8. Build an x402 server to monetize HTTP routes

    main

    Use the x402 protocol to charge for HTTP routes and receive USDC in a CDP-managed wallet. This is suitable for monetizing APIs, putting paywalls in front of endpoints, or accepting payments from agents.

    When to use this approach

    • You want to monetize an API or price a route per request.
    • You want to put a paywall in front of an endpoint.
    • You want to accept payments from agents.
    • You want to add x402 support to an existing server.

    When NOT to use this approach

    • Using Coinbase Business: If you want a hosted payment URL for people or a x402_url for agents without running a server, use the Business Checkouts API.
    • Charging for MCP tools: If you are charging for an MCP tool rather than an HTTP route, see Charge over MCP.
    • The user is the payer: If you are building the client side that makes payments, use the build-x402-client skill.
    • No-code monetization: If you want a deployed service with no code, use the monetize-service skill from the agentic-wallet CLI.
  9. Perform token swaps on EVM networks

    main

    The SDK supports three patterns for performing token swaps on EVM networks, working with both Regular Accounts (EOAs) and Smart Accounts.

    Use account.swap() or smartAccount.swap() for a simple, single-line execution. This handles everything automatically.

    2. Get pricing information

    Use cdp.evm.getSwapPrice() to get quick estimates and check liquidity without committing to a swap. This is suitable for UI displays and is less strictly rate-limited.

    3. Create and execute separately

    Use account.quoteSwap() or smartAccount.quoteSwap() when you need full control. This returns a quote object that you can inspect before calling .execute(). Note: quoteSwap() signals a soft commitment and may reserve funds on-chain, so it is more strictly rate-limited.

    Key Differences: EOA vs Smart Account

    • EOAs: Return transactionHash and execute immediately.
    • Smart Accounts: Return userOpHash and execute via user operations (ERC-4337), supporting gas sponsorship via paymasters.
    // Recommended: All-in-one swap for EOA
    const { transactionHash } = await account.swap({
      network: "base",
      toToken: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
      fromToken: "0x4200000000000000000000000000000000000006",
      fromAmount: BigInt("1000000000000000000"),
      slippageBps: 100,
    });
    
    // Recommended: All-in-one swap for Smart Account
    const { userOpHash } = await smartAccount.swap({
      network: "base",
      toToken: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
      fromToken: "0x4200000000000000000000000000000000000006",
      fromAmount: BigInt("1000000000000000000"),
      slippageBps: 100,
    });
  10. Manage Policy Engine with the CDP Python SDK

    main

    The Policy Engine allows you to manage sets of rules that govern the behavior of accounts and projects, such as enforcing allowlists or denylists. You can create policies at either the project scope (applying to all accounts) or the account scope.

    Supported rule types include various signing and sending operations for both EVM and Solana, as well as User Operations for smart accounts. Rules use criteria like EthValueCriterion, EvmAddressCriterion, SolanaAddressCriterion, and SolValueCriterion to define conditions.

    # Example: Create a project-level policy
    policy = await cdp.policies.create_policy(
        policy=CreatePolicyOptions(
            scope="project",
            description="Project-wide Allowlist Example",
            rules=[
                SignEvmTransactionRule(
                    action="accept",
                    criteria=[
                        EthValueCriterion(ethValue="1000000000000000000", operator="<="),
                        EvmAddressCriterion(addresses=["0x..."], operator="in"),
                    ],
                ),
            ],
        )
    )
  11. Manage CDP Client Lifecycle

    main

    The CdpClient wraps an aiohttp client and is designed to be long-lived. It handles connection pooling automatically. To ensure efficiency, avoid recreating the client for every request.

    • Long-lived services: Create a single instance at startup using async with CdpClient() as cdp:.
    • Serverless/Request-based runtimes: Create once per cold start or use a module-level singleton.
    • Concurrency: The client is safe to use across concurrent asynchronous operations.
  12. Use Account Actions instead of the CDP client

    main

    The CDP SDK provides Account objects that possess their own action methods. These methods allow you to perform operations directly on the account instance, serving as a more ergonomic alternative to calling methods on the top-level cdp client by passing the account address manually.

    For example, instead of passing an address to the client:

    const balances = await cdp.evm.listTokenBalances({
      address: account.address,
      network: "base-sepolia",
    });

    You can call the action directly on the account object:

    const account = await cdp.evm.createAccount();
    const balances = await account.listTokenBalances({ network: "base-sepolia" });