Package: cbp-client
python-coinbaseproAn unofficial python package for interacting with the Coinbase Pro API. This package can be used to query both public and authenticated endpoints. This package enables: retrieving live price data, retrieving historical price data, placing market buy and sell orders, depositing fiat, and many other features. See below for examples.This has been a personal project of mine for some time now. Feel free to test it out and
let me know if you found it useful. Thank you!WARNINGUse at your own risk. This project is currently under development.Installation# Install from pypi (most common)pipinstallcbp-client# Install from Githubpipinstallgit+https://github.com/TerrellV/python-coinbasepro.gitPublic API ExamplesNo authentication required>>>fromcbp_clientimportPublicAPI>>>api=PublicAPI()Get current bitcoin price>>>api.price('btc')'32615.98'Get current ETH-BTC price>>>api.price(base_currency='eth',quote_currency='btc')'0.03934'Get historical pricesThis method is useful becuase it can return large sets of granular, historical,
price data. In the example below, we requested one year of hourly price data
for LTC-USD. In just 16 seconds 8,639 rows of price data are returned.api.historical_pricesreturns a generator object. This allows you to loop
through the data without loading the entire response into memory.>>>price_history=api.historical_prices(product_id='LTC-USD',candle_interval='hourly',start='2017-01-01',end='2018-01-01')# a generator object is returned>>>list(price_history)[Candle(start='2017-10-01T00:00:00',open='55.26',high='55.33',low='54.86',close='55.26',volume='18231.73237201'),Candle(start='2017-10-01T01:00:00',open='55.26',high='55.26',low='54.51',close='54.7',volume='19696.71425587'),Candle(start='2017-10-01T02:00:00',open='54.7',high='54.7',low='53.74',close='53.96',volume='20342.70035151'),Candle(start='2017-10-01T03:00:00',open='53.93',high='54.45',low='53.74',close='54.4',volume='7261.14770822'),Candle(start='2017-10-01T04:00:00',open='54.4',high='54.63',low='54.4',close='54.6',volume='4633.03379232'),...]Get all products>>>api.products()[Product(id='BCH-BTC',display_name='BCH/BTC',base_currency='BCH',quote_currency='BTC',...),Product(id='LINK-ETH',display_name='LINK/ETH',base_currency='LINK',quote_currency='ETH',...),...]Filter productsthe products method will filter the returned product list by any attribute specified. To filter, simply specify
the attribute as an argument and set it equal to the value you'd like to filter on. For example,
To retrieve only /USD trading pairs, set quote_currency='USD'. To retrieve only /BTC trading pairs, set quote_currency=BTC.>>>api.products(quote_currency='USD')[Product(id='BTC-USD',display_name='BTC/USD',base_currency='BTC',quote_currency='USD',...),Product(id='LINK-ETH',display_name='ETH/USD',base_currency='ETH',quote_currency='USD',...),...]Authenticated APIThe Authenticated API client provides access to account level details AND allPublicAPImethods referenced above. In order to use the live authenticated api, you must provide credentials through one of the following methods: pass a credentials dictionary to the AuthAPI class or set environment variables as shown below.fromcbp_clientimportAuthAPIfrompathlibimportPathimportjsoncreds=json.loads(Path('credentials.json').read_text())['live']api=AuthAPI(creds)#credentials.jsonfileloadedinpythonexampleabove{"live":{"secret":"replace_this_with_secret","passphrase":"replace_this_with_passphrase","api_key":"replace_this_with_api_key"},"sandbox":{"secret":"replace_this_with_secret","passphrase":"replace_this_with_passphrase","api_key":"replace_this_with_api_key"}}Setting Credentials Using Environment Variableswindows cmd.exe examplesetapi_key=INSERT_YOUR_INFOsetapi_passphrase=INSERT_YOUR_INFOsetapi_secret=INSERT_YOUR_INFObash exampleexportapi_key=INSERT_YOUR_INFOexportapi_passphrase=INSERT_YOUR_INFOexportapi_secret=INSERT_YOUR_INFOSandbox credential example.
If you are setting environment variables for your sandbox environment, prefix the variables names with "sandbox_" as shown.setsandbox_api_key=INSERT_YOUR_INFOsetsandbox_api_passphrase=INSERT_YOUR_INFOsetsandbox_api_secret=INSERT_YOUR_INFOWarning:Never store your api credentials directly in your codeNever commit your credentials.json file to a git repositorySandbox Authenticated APIYou should test your code before you deploy it. One way to do this without spending real money is
to use the sandbox api. Setsandbox_mode=Trueto instruct the authenticated api client to use the
sandbox api url. Additionally, when connecting with the authenticated sandbox api client,
ensure you are loading your sandbox credentials from your credentials.json file or else it wont work.fromcbp_clientimportAuthAPIfrompathlibimportPathimportjsoncreds=json.loads(Path('credentials.json').read_text())['sandbox']api=AuthAPI(creds,sandbox_mode=True)Get account balance>>>api.balance('btc')'0.0000000000000000'Place a market-buy order>>>api.market_buy(10,product_id='btc-usd')<Response[200]>Place a market-sell order>>>api.market_buy(size='0.01',product_id='eth-usd')<Response[200]>Deposit money into coinbase pro>>>payment_method=api.payment_methods(name='Focus Financial ******0000')>>>sandbox_auth_api.deposit(amount=10,currency='USD',payment_method_id=payment_method['id'])<Response[200]>Get account history>>>hist=api.account_history(symbol='btc',start_date='2021-01-01')# returns a generator object>>>forrecordinhist:print(record){'id':'activity_id','amount':'-0.0100000000000000','balance':'0','created_at':'----creation date----','type':'match','details':{'order_id':'----order id----','product_id':'BTC-USD','trade_id':'----trade id----'}}...