Are you a financial application developer or cryptocurrency trader looking to automate your data access and trading operations on Coinbase?
This tutorial is for you. It covers the prerequisites and issues with the Python Coinbase wrapper to access the Coinbase API in Python and get cryptocurrency information, and trade crypto assets.
Take the first step in automating your Coinbase operations by reading this tutorial and learning how to access the Coinbase API in Python, get cryptocurrency information, and trade crypto assets.
What Is Coinbase?
Coinbase is an online cryptocurrency exchange platform where you can buy, sell and trade cryptocurrencies. Coinbase lets you get the latest cryptocurrency prices, exchange rates, and other important information.
Brian Armstrong and Fred Ehrsam founded Coinbase in May 2012. San Francisco, USA, hosts the Coinbase headquarters.
What Is the Coinbase API?
Coinbase API is a set of REST functions that users can call in code to access information and perform operations on the Coinbase exchange. With the Coinbase API, you can develop customized financial software that performs various trading operations on Coinbase.
Coinbase Pros and Cons
Pros:
- You can trade more than 160 cryptocurrencies.
- Low minimum amount to fund your account.
- Allows quick withdrawals.
- User-friendly with a simple interface.
- Extensive and easy-to-use REST API.
Cons:
- Higher fees than other cryptocurrency exchanges.
- Customer support is below par.
Coinbase API Pricing Plans
Coinbase API is free to use, and the endpoints you can call depend upon various factors, e.g., account type, wallet types, existing funds, and cryptocurrencies.
How to Get Started with Coinbase API in Python?
Go to the Coinbase home page and click the “Get Started” button from the top right corner of the page.
You will see the following sign-up form. Fill out the form and click the “Create free account” button.”
Click the verification link you receive in your email and set up two-step verification using your mobile number.
You will see the following window. You can set up your bank card at this point if you want. If you are only interested in fetching market data. You can set up your bank card later if you want.
The following window allows you to transfer cryptocurrencies to your account. Skip this step if you do not want to transfer cryptocurrencies.
The next step is to generate your API Key. Log in to your Coinbase account, and click your account avatar from the top right corner of your account home page. A drop-down list will appear. Click “Settings. You will see the following window. Click the “API” option.
Click the “+ New API Key” button in the following window.
You will receive an SMS verification code on your mobile. Enter the verification code in the SMS verification window.
The will following window will appear. You can select the endpoints you want to access via your API key. The Coinbase free account allows you to access wallet endpoints, as seen in the following window. I selected all endpoints. You can select the endpoints of your choice and click the “Create” button.
Note: For more advanced features, sign up for Coinbase Pro account, which allows you to access the Coinbase Pro API.
You will see the following window, which contains your API key and API secret. Save these values in a secure place. Usually, I save these values in system environment variables.
Simple Example Fetching Data Using Coinbase API in Python
There are a couple of ways to access the Coinbase API:
- Using the Python coinbase library.
- Using the Python requests module.
Coinbase API Python Library Example
Run the following pip command to install the Python coinbase library.
To call a Coinbase API endpoint, import the Client class from the coinbase.wallet.client module. Pass your Coinbase API key and API secret to the Client class.
You can call any Coinbase API via the Client class methods. For instance, the following script calls get_currencies() method to call the currencies endpoint function that returns currency information for all Coinbase currencies.
Coinbase API Requests Module Example
The requests library can be handy since other third-party API endpoints may take some time or avoid implementing API endpoints altogether. In such cases, you can always count on the Python requests module, as it directly sends an HTTP request to the REST API function.
Run the following command to install the requests library.
Check the official Coinbase API documentation to see requests URLs for Coinbase API calls.
The following example demonstrates how you can call the Coinbase API endpoints via the requests library.
Coinbase API Endpoints
Coinbase API endpoints broadly fall into two categories: Market Data Endpoints and Wallet Endpoints.
- Market Data Endpoints
- Currencies
- Exchange Rates
- Prices
- Time
- Wallet Endpoints
- Users
- Accounts
- Addresses
- Transactions
- Buys
- Sells
- Deposits
- Withdrawals
- Payment Methods
Market Data Endpoints
As the name suggests, market data endpoints return market information, e.g., currency prices, exchange rates, etc.
Currencies Endpoint
Currencies endpoint has only one function that returns all the currencies you can buy or sell at Coinbase.
Get Coinbase Currencies
The get_curencies() method returns a list of all Coinbase-supported currencies.
The following stores the response from the get_currencies() method into a Pandas data frame. You can store the data frame values in a CSV file using the to_csv() function.
Exchange Rates Endpoint
The exchange rate endpoint contains a single function that returns a currency’s exchange rate against all other currencies.
Get Coinbase Exchange Rates
You can use the get_exchange_rates() method to get exchange rates for a currency. The default currency is USD.
You can get exchange rates for any other currency by passing the id of the currency to the get_exchange_rates() method. For example, the following script returns exchange rates for Bitcoin.
The get_exchange_rates() method returns a dictionary of dictionaries. You can get the exchange rate in any currency using the following script.
Price Endpoint
The price endpoint functions return the buy, sell, and spot prices for a currency. The endpoint also contains a function returning historical cryptocurrency prices.
Get Coinbase Buy Price
The get_buy_price() method returns the buy price of a currency. The buy price includes Coinbase fee of 1%. By default, the get_buy_price() method returns the buy price of a Bitcoin in USD.
If you want the buy price of one currency against another, you can pass the currency pair to the get_buy_price() method.
Get Sell Price
The get_sell_price() returns the latest selling price of a currency. By default, the get_sell_price() method returns the sell price for Bitcoin in USD.
As the following script demonstrates, you can get sell prices of other currency pairs by passing the pair values to the get_sell_price() method.
Get Coinbase Spot Price
You can get the current market price of a currency using the get_spot_price() method. The current spot price is generally between selling and buying prices.
Get Historical Cryptocurrency Prices
You can retrieve a currency’s historical market price on a specific date by passing the date string to the date attribute of the get_spot_price() method.
Currency prices frequently fluctuate. You can fetch a currency’s latest half-hour price trends using the get_historic_prices() method. In return, you will receive a dictionary of currency prices with 10 seconds time difference between each price value.
Time Endpoint
Get Current Server Time
The get_time() method returns the Coinbase API server time in UNIX and ISO formats.
Wallet Endpoints
Users Endpoint
Get Coinbase User Info
You can use the get_user() method to retrieve any Coinbase user’s public information using the user’s id.
Get Current User Info
If you want to retrieve information of the user accessing the Coinbase API via an API key, you can use the get_current_user() method. In return, you can see information such as whether a user’s buy, sell funds, send, and receive cryptocurrency options are enabled or disabled.
Accounts Endpoint
The account endpoint returns account information, such as all user accounts. The endpoint also allows you to create and delete an account, etc.
Get All Coinbase Accounts
The get_accounts() endpoint returns all accounts, including cryptocurrency wallet accounts, FIAT currency accounts, and vaults of a user. In the output, you can see the currency type of your account along with the account balance.
Get Coinbase Account by ID
You can get information about your currency accounts by passing the currency ID to the get_account() method.
Get Coinbase Primary Account
A primary account is a custodial wallet used on your main Coinbase account.
You can get the primary Coinbase account using the get_primary_account() function.
Delete an Account by ID
Addresses Endpoint
Addresses endpoints contain functions that allow you to create and get addresses for receiving or sending any Coinbase-supported asset. You can also retrieve transactions on a particular address using an addressee endpoint function.
Create a New Receive Address
The create_address() method creates an address to receive an asset. You must pass the asset ID to the create_addresses() function.
You can get the id of an asset via the get_account() function, as you saw in the “Get Coinbase Account by ID” section. For instance, you can get the id of the Ethereum using the get_account(“ETH”) method. You can then pass the id to the create_address()method, as the following script demonstrates.
The script below creates an address for receiving Ethereum.
Similarly, the script demonstrates how to create a receive address for Bitcoin. In this case, I use the id BTC for the Bitcoin address.
Get Coinbase Receive Addresses
You can get a receive address for an asset by passing its id to the get_addresses() method. You can pass multiple asset ids to get addresses for all the assets.
Get Transactions for an Address
You can get a list of all transactions for different assets in your accounts. To do so, you must pass address ids to the get_address_transactions() function.
Remember, in this case, you need to pass address IDs, not asset IDs. I made this mistake initially when I passed asset Ids instead of address Ids, and I got an exception that IDs were not found.
You will see a dictionary of all transactions in the output. Since I do not have any transactions in my addresses, you can see an empty dictionary in the output below.
Transactions Endpoint
The transactions endpoint contains functions to carry out various transactions via your Coinbase wallet.
Get All Coinbase Transactions
You can retrieve all transactions for an account via the get_transactions() method. You must pass the account ID as the parameter value to the get_transactions() method.
Send Money via Coinbase
You can send money via Coinbase to a network address for any Coinbase supported asset, or the recipient’s email address. The send_money() function allows you to do so.
You must pass the source account id, the network address or email of the receiver, the amount, and the currency you want to send, to the send_money() function.
If the address or email you send money to is incorrect, you will receive an exception: Please enter a valid email or Ethereum address.
Transfer Money Between Coinbase Accounts
The transfer_money() function allows you to transfer Coinbase supported digital assets between two accounts of a single user.
Cancel Request
Finally, you can cancel any pending request for sending or receiving money via the cancel_request() method.
Buys and Sells Endpoints
Buying and selling events represent the purchase or sale of Coinbase-supported assets via a Bank or a FIAT currency account.
Get Coinbase Buys
You can all get a list of all buy transactions in your account via the get_buys() method. You must pass the account ID to the get_buys() method.
Buy Cryptocurrency with Coinbase
The buy() method from the Client class allows you to buy cryptocurrencies via Coinbase. You must pass the account ID where you want to receive a currency, along with the amount and the currency ID, to the buy() method.
If you do not have sufficient funds in your account, you will get the exception: User is unable to buy.
Get Coinbase Sells
You can get all sell transactions from your Coinbase account using the get_sells() function. You must pass the account ID to the function.
Sell Bitcoins with Coinbase
You can sell cryptocurrencies with Coinbase using the sell() method. You must pass to the sell() method the ID of the account containing your cryptocurrency, the amount, and the ID of the cryptocurrency.
If you do not have sufficient funds in your account, you will get the exception: User is unable to sell.
Deposits Endpoint
The deposit endpoint contains functions to retrieve all the deposit transactions that you commit via a payment method. The endpoint also provides a function to deposit funds into your Coinbase account.
Get All Coinbase Deposits
You can get a list of all deposit transactions in an account via the get_deposits() method. You must pass the account ID to the get_deposits() method.
Deposit Funds via Coinbase
You can deposit funds to a Coinbase account via the deposit() method.
The deposit() method accepts four parameters:
- The account ID where you want to deposit funds.
- The amount and the ID of the currency to deposit.
- The payment method.
You must add a payment method by signing in to your Coinbase account before you execute the following script.
Withdrawals Endpoint
Withdrawal endpoint functions let you access all your withdrawal operations from an account. You can also use these functions to withdraw funds.
Get Coinbase Withdrawals
The get_withdrawals() method returns a list of all withdrawal transactions. You must pass the account ID to the method.
Withdraw Funds from Coinbase
The withdraw() method let’s you withdraw funds from Coinbase. You must pass to the withdraw() method, the account from which you want to withdraw funds, the amount and ID of the currency, and the payment method that receives your funds.
You must add a payment method by signing in to your coinbase.com account
before you execute the following script.
Payment Methods Endpoint
Functions from this endpoint allow you to list all payment methods you added to your Coinbase account. You can also get details of a payment method via its ID.
Get All Binance Payment Methods
The get_payment_methods() method returns all payment methods attached to your Coinbase account.
You must add a payment method by signing in to your Coinbase account before you execute the following script, or you will receive an empty output.
Get Payment Method by ID
You can retrieve details of a payment method by ID using the get_payment_method() method.
You must pass a valid payment method ID to the get_payment_method() method, failing which you will receive an error. You can add a payment method by signing in to your Coinbase account.
Common Errors and Issues Accessing Coinbase API?
Create a New Account
Creating a new account via the Python coinbase library results in error:
Though I enable the wallet account permissions while generating an API, I still get this error.
Request Money Endpoint Parameter Error
You can request money from another user in your account via the request_money() method. As per the official documentation, you need to pass a receive address, the email of the user from whom you request money, the amount, and the requested currency.
However, you will receive an error when you run the following script.
As per several users, this is a known API bug that is not fixed yet.
API Key Disabled Error
You will not be able to execute any Coinbase script while your Coinbase API key is disabled. You will see the following error in this case. A newly created Coinbase API key takes upto 48 hours before it is enabled.
Card Decline Error:
Your payment method must be issued from the same country where you created your Coinbase account. Else, you will see the following error while adding your payment method.
Coinbase Alternatives
Following are some alternatives to Coinbase:
Frequently Asked Questions
What Client Libraries Are Available for Coinbase API?
Coinbase API libraries are available for Python, Node.js, Go, and .NET. See the official documentation for further details.
Does Coinbase Have a Trading Bot?
Coinbase doesn’t have a trading bot of its own. However, you can integrate third-party trading bots, e.g., Bitsgap, Coinrule, and TradeSanta, with Coinbase.
What Crypto Bots Work with Coinbase?
Following are some trading bots that work with Coinbase:
Is Coinbase API Safe?
Coinbase claims to store 97% of the bitcoins in encrypted and geographically separated offline storage. In addition, all Coinbase insures users’ cryptocurrency assets. Thus Coinbase is generally considered a safe cryptocurrency exchange.
Will Coinbase Refund If Hacked?
Crime insurance protection enables Coinbase to refund accounts hacked due to identity thefts and cybersecurity breaches.
Does Coinbase Have a Secret Phrase?
Coinbase generates a 12-word recovery phase that only you can access. You should note down your recovery/secret phase at a safe and secure place since losing your secret phase will block your access to your Coinbase wallet.
Does Coinbase Report to IRS?
Yes, Coinbase reports to IRS before the start of the tax filing season. If you pay taxes in US and earn crypto gains over $600, you will receive a form 1099 for tax filing from Coinbase.
What Is Coinbase Withdrawal Limit?
Minimum withdrawal limits are generally less than $0.10. There are no maximum withdrawal limits. You can withdraw all your funds at any point. See the Coinbase help for details on withdrawal limits.
Is Binance Better than Coinbase?
There is no straightforward answer to this question. As a rule of thumb, Coinbase is more suited to beginners aiming for ease of use and convenience. Binance is better for people familiar with cryptocurrency lingo and advanced investment strategies.
Is Coinbase Pro Free?
Coinbase Pro is not free and charges between $0% to $0.50% per trade.
What Are Access and Refresh Tokens in the Coinbase API?
Access and Refresh tokens are optional OAuth2 security features for accessing the Coinbase API. You will get access and refresh tokens on signing up with the Coinbase API, The access token allows you to authenticate your API requests, but it expires in two hours. You can get your new access token using your refresh token. A refresh token never expires itself. You can use it to generate new access and refresh tokens. See the official documentation for more information on access and refresh tokens.
The Bottom Line
Coinbase is a digital currency exchange platform allowing users to trade cryptocurrency and get important cryptocurrency information. It offers a GUI dashboard and a REST API for monitoring and trading cryptocurrency prices.
This article explained how to call the Coinbase REST API in Python via the Python Coinbase library and the Python requests module. You can develop financial applications and crypto trading platforms with the information provided in the article. Data scientists can also benefit from this article for financial data research.