Skip to main content

Quickstart

The Coinbase Platform SDK lets you interact with Coinbase Platform APIs.

This quickstart shows you how to access Coinbase APIs from Ruby applications by installing the Coinbase Ruby SDK. You’ll also learn how to use the Interactive Ruby Shell (irb) to quickly explore the functionality of our SDK.

info

Currently, the Coinbase Platform SDK is only available in Ruby, but more languages will be available soon.

What You'll Learn

  • How to install the Coinbase Ruby server-side SDK
  • How to create a wallet and send your first SDK request
  • How to fetch the default address of your wallet
  • How to fund your wallet with testnet funds
  • How to transfer funds between wallets
  • How to create a wallet backup and re-instantiate it

Requirements

The Coinbase server-side SDK requires Ruby 2.7 or higher. Visit the Ruby homepage to install Ruby and get started.

ruby -v

We recommend managing Ruby versions with rbenv and not using the system version of Ruby shipped with your operating system.

Coinbase’s documentation is written for a Unix-based command-line such as Mac or Linux. On Windows, Coinbase recommends using the Windows Bash Shell.

Set up the SDK with Interactive Ruby Shell

Use the Interactive Ruby Shell (irb) to leverage Ruby’s built-in REPL and quickly explore the functionality of our SDK.

In a terminal run:

gem install coinbase-sdk

After running irb, require the Gem:

require 'coinbase'

Install the SDK into Your Ruby Project

Alternatively, if you want to install your Coinbase SDK gem to your Ruby project, add the following line to your Gemfile:

gem 'coinbase-sdk'

Or if you are using a Gemspec:

Gem::Specification.new do |spec|
spec.add_runtime_dependency 'coinbase-sdk'
end

Then run:

bundle install

Create Wallet & Send Funds

The code below walks you through creating a wallet & sending funds.

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

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

Coinbase.configure do |config|
config.api_key_name = api_key_name
config.api_key_private_key = api_key_private_key
end

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

Coinbase.configure_from_json('~/Downloads/coinbase_cloud_api_key.json')

This will allow you to authenticate with the Platform APIs and get access to the default_user:

u = Coinbase.default_user

Now, create a wallet from the User. wallets are created with a single default address.

# Create a wallet with one address by default.
w1 = u.create_wallet

Next, view the default address of your wallet. You will need this default address in order to fund the wallet for your first transfer.

# A wallet has a default address.
a = w1.default_address
a.to_s

Wallets do not have funds on them to start. In order to fund the address, you will need to send funds to the wallet you generated above. If you don't have testnet funds, get funds from a faucet. We provide a faucet method to fund your address with ETH on Base Sepolia testnet. You are allowed one faucet claim per address in a 24-hour window.

# Create a faucet request for address a in wallet w1.
faucet_tx = a.faucet

# Use the transaction_hash in the faucet_tx to track the status.
faucet_tx.transaction_hash

# Create a new wallet w2 to transfer funds to.
w2 = u.create_wallet

# Then, we can transfer 0.00001 ETH from wallet w1 to wallet w2.
w1.transfer(0.00001, :eth, w2).wait!

Back-up & Re-instantiate Wallet

The SDK creates wallets with developer managed keys, which means you are responsible for securely storing the keys required to re-instantiate wallets. The following code explains how to export a wallets and store it in a secure location.

# Optional: Create a new wallet if you do not already have one.
# Export the data required to re-instantiate the wallet.
w3 = u.create_wallet
data = w3.export

In order to persist the data for the wallet, you will need to implement a store method to store the data export in a secure location. If you do not store the wallet in a secure location you will lose access to the wallet and all of the funds on it.

# At this point, you should implement your own "store" method to securely persist
# the data required to re-instantiate the wallet at a later time.
store(data)

# For development purposes, you can use the save_wallet functionality in the SDK to store the wallet data in your local file system.
u.save_wallet(w3)

The below code demonstrates how to re-instantiate a wallet from the data export.

# The wallet can be re-instantiated using the exported data.
# w2 will be equivalent to w1.
w4 = u.import_wallet(data)

# For development purposes, you can use the load_wallets functionality in the SDK to load the wallet data from your local file system, which is stored using the save_wallet function.
wallets = u.load_wallets

Was this helpful?