Skip to main content

CDP Python SDK

Project description

CDP Python SDK

The CDP Python SDK enables the simple integration of crypto into your app. By calling Coinbase's CDP APIs, the SDK allows you to provision crypto wallets, send crypto into/out of those wallets, track wallet balances, and trade crypto from one asset into another.

CDP SDK v0 is a pre-alpha release, which means that the APIs and SDK methods are subject to change. We will continuously release updates to support new capabilities and improve the developer experience.

Documentation

Requirements

  • Python 3.10+

Checking Python Version

Before using the SDK, ensure that you have the correct version of Python installed. The SDK requires Python 3.10 or higher. You can check your Python version by running the following code:

python --version
pip --version

If you need to upgrade your Python version, you can download and install the latest version of Python from the official Python website. For pip, refer to the official pip documentation for installation instructions.

Installation

pip install cdp-sdk

Starting a Python REPL

To start a Python REPL:

python

Creating a Wallet

To start, create a CDP API key. Then, initialize the CDP SDK by passing your API key name and API key's private key via the configure method:

from cdp import *

api_key_name = "Copy your API key name here."

api_key_private_key = "Copy your API key's private key here."

Cdp.configure(api_key_name, api_key_private_key)

print("CDP SDK has been successfully configured with CDP API key.")

Another way to initialize the SDK is by sourcing the API key from the JSON file that contains your API key, downloaded from the CDP portal.

Cdp.configure_from_json("~/Downloads/cdp_api_key.json")

print("CDP SDK has been successfully configured from JSON file.")

This will allow you to authenticate with the Platform APIs.

If you are using a CDP Server-Signer to manage your private keys, enable it with

Cdp.use_server_signer = True

Now create a wallet. Wallets are created with a single default address.

# Create a wallet with one address by default.
wallet1 = Wallet.create()

print(f"Wallet successfully created: {wallet1}")

Wallets come with a single default address, accessible via default_address:

# A wallet has a default address.
address = wallet1.default_address

Funding a Wallet

Wallets do not have funds on them to start. For Base Sepolia testnet, we provide a faucet method to fund your wallet with testnet ETH. You are allowed one faucet claim per 24-hour window.

# Fund the wallet with a faucet transaction.
faucet_tx = wallet1.faucet()

print(f"Faucet transaction successfully completed: {faucet_tx}")

Transferring Funds

See Transfers for more information.

Now that your faucet transaction has successfully completed, you can send the funds in your wallet to another wallet. The code below creates another wallet, and uses the transfer function to send testnet ETH from the first wallet to the second:

# Create a new wallet wallet2 to transfer funds to.
wallet2 = Wallet.create()

print(f"Wallet successfully created: {wallet2}")

transfer = wallet1.transfer(0.00001, "eth", wallet2).wait()

print(f"Transfer successfully completed: {transfer}")

Gasless USDC Transfers

To transfer USDC without needing to hold ETH for gas, you can use the transfer method with the gasless option set to True.

# Create a new wallet wallet3 to transfer funds to.
wallet3 = Wallet.create()

print(f"Wallet successfully created: {wallet3}")

# Fund the wallet with USDC with a faucet transaction.
usdc_faucet_tx = wallet1.faucet("usdc")

print(f"Faucet transaction successfully completed: {usdc_faucet_tx}")

transfer = wallet1.transfer(0.00001, "usdc", wallet3, gasless=True).wait()

Listing Transfers

# Return list of all transfers. This will paginate and fetch all transfers for the address.
list(address.transfers())

Trading Funds

See Trades for more information.

wallet = Wallet.create("base-mainnet")

print(f"Wallet successfully created: {wallet}")
print(f"Send `base-mainnet` ETH to wallets default address: {wallet.default_address.address_id}")

trade = wallet.trade(0.00001, "eth", "usdc").wait()

print(f"Trade successfully completed: {trade}")

Listing Trades

# Return list of all trades. This will paginate and fetch all trades for the address.
list(address.trades())

Creating a Webhook

A webhook is a way to provide other applications with real-time information from the blockchain. When an event occurs on a blockchain address, it can send a POST request to a URL you specify. You can create a webhook to receive notifications about events that occur in your wallet or crypto address, such as when a user makes a transfer.

from cdp.client.models.webhook import WebhookEventType
from cdp.client.models.webhook import WebhookEventFilter

wh1 = Webhook.create(
    notification_uri="https://your-app.com/callback",
    event_type=WebhookEventType.ERC20_TRANSFER,
    event_filters=[WebhookEventFilter(from_address="0x71d4d7d5e9ce0f41e6a68bd3a9b43aa597dc0eb0")]
)
print(wh1)

Creating a Webhook On A Wallet

A webhook can be attached to an existing wallet to monitor events that occur on the wallet, i.e. all addresses associated with this wallet. A list of supported blockchain events can be found here.

import cdp

wallet1 = Wallet.create()
wh1 = wallet1.create_webhook("https://your-app.com/callback")
print(wh1)

Contributing

See CONTRIBUTING.md for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cdp_sdk-0.0.9.tar.gz (130.9 kB view details)

Uploaded Source

Built Distribution

cdp_sdk-0.0.9-py3-none-any.whl (268.6 kB view details)

Uploaded Python 3

File details

Details for the file cdp_sdk-0.0.9.tar.gz.

File metadata

  • Download URL: cdp_sdk-0.0.9.tar.gz
  • Upload date:
  • Size: 130.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for cdp_sdk-0.0.9.tar.gz
Algorithm Hash digest
SHA256 f7e20a64992fccc395c1a10492a874b3aab0670b4b817e513b1fffde79a04efd
MD5 cc71e29d61150098474d0b090d36c3fe
BLAKE2b-256 6775ba50068af8cd9a057956bdee78ea811c874a7630ea126cd7d637bc23de81

See more details on using hashes here.

File details

Details for the file cdp_sdk-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: cdp_sdk-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 268.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for cdp_sdk-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 69cc6b89c284765aa6a6ddabfdf94f171050d45096e924feef2278e439680fd3
MD5 c2a97ad71c321540a65d9aa20cf17585
BLAKE2b-256 b6fc86e2b9e4e62c58a3e73e61a8d4b011933f2115ef5342622bd8eb82f531d9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page