An unofficial python package for interacting with the Coinbase Pro API
Project description
python-coinbasepro
An 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!
WARNING
Use at your own risk. This project is currently under development.
Installation
# Install from pypi (most common)
pip install cbp-client
# Install from Github
pip install git+https://github.com/TerrellV/python-coinbasepro.git
Public API Examples
No authentication required
>>> from cbp_client import PublicAPI
>>> 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 prices
This 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_prices
returns 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 products
the 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 API
The Authenticated API client provides access to account level details AND all PublicAPI
methods 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.
from cbp_client import AuthAPI
from pathlib import Path
import json
creds = json.loads(Path('credentials.json').read_text())['live']
api = AuthAPI(creds)
# credentials.json file loaded in python example above
{
"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 Variables
windows cmd.exe example
set api_key=INSERT_YOUR_INFO
set api_passphrase=INSERT_YOUR_INFO
set api_secret=INSERT_YOUR_INFO
bash example
export api_key=INSERT_YOUR_INFO
export api_passphrase=INSERT_YOUR_INFO
export api_secret=INSERT_YOUR_INFO
Sandbox credential example. If you are setting environment variables for your sandbox environment, prefix the variables names with "sandbox_" as shown.
set sandbox_api_key=INSERT_YOUR_INFO
set sandbox_api_passphrase=INSERT_YOUR_INFO
set sandbox_api_secret=INSERT_YOUR_INFO
Warning:
- Never store your api credentials directly in your code
- Never commit your credentials.json file to a git repository
Sandbox Authenticated API
You should test your code before you deploy it. One way to do this without spending real money is
to use the sandbox api. Set sandbox_mode=True
to 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.
from cbp_client import AuthAPI
from pathlib import Path
import json
creds = 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
>>> for record in hist:
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----'
}
}
...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file cbp-client-1.1.4.tar.gz
.
File metadata
- Download URL: cbp-client-1.1.4.tar.gz
- Upload date:
- Size: 14.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 90477dd380487af86b12b9dd95094ab478230c049a1022b0a93218948746049f |
|
MD5 | d688dfa07b96c84fc370cadbf45c23d7 |
|
BLAKE2b-256 | 286d168ba5757e979c9d0e3248ffa81dec5159ad2291f5a9e6c622d6f9132ce9 |
File details
Details for the file cbp_client-1.1.4-py3-none-any.whl
.
File metadata
- Download URL: cbp_client-1.1.4-py3-none-any.whl
- Upload date:
- Size: 15.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d0579d2c6001a8e39ee8ac167a53c8d48a93e2710af30697a45bc6f9662a9d13 |
|
MD5 | 67a27f08df2277af4687bd45591e0daa |
|
BLAKE2b-256 | 1e96a76eadbc18b27f130a1ff933c457100ba311b85554baa73ebb1c003219ae |