Giter Site home page Giter Site logo

coinbasepro's Introduction

CoinbasePro: A Python API

Features

  • Full support of Coinbase Pro REST API

  • Rate-limiting - no more 429 error responses!

  • Pythonic abstractions for a clean interface
    • Return values are returned as Python data types instead of all string values:
    >>> import coinbasepro as cbp
    >>> client = cbp.PublicClient()
    # datetime and Decimal are among the return types in the dict returned
    # by this call:
    >>> client.get_product_ticker('BTC-USD')
    {'trade_id': 2845680,
    'price': Decimal('2496.69000000'),
    'size': Decimal('0.00100000'),
    'time': datetime.datetime(2019, 3, 20, 23, 53, 59, 596000),
    'bid': Decimal('2496.69'), 'ask': Decimal('2496.7'),
    'volume': Decimal('771.51495215')}
    
    • Paginated endpoints are abstracted as generators:
    >>> import itertools
    # get_product_trades is a generator
    >>> client.get_product_trades('BTC-USD')
    <generator object PublicClient.get_product_trades.<locals>.<genexpr> at 0x1098d6f68>
    
    # Get 2 most recent trades. For many trade requests (>100), multiple
    # HTTP requests will be made under the hood.
    >>> list(itertools.islice(client.get_product_trades('BTC-USD'), 2))
    [{'time': datetime.datetime(2019, 3, 21, 0, 2, 45, 609000),
    'trade_id': 2845779,
    'price': Decimal('2463.38000000'),
    'size': Decimal('0.00100000'),
    'side': 'buy'},
    {'time': datetime.datetime(2019, 3, 21, 0, 2, 39, 877000),
    'trade_id': 2845778,
    'price': Decimal('2463.39000000'),
    'size': Decimal('0.00100000'),
    'side': 'sell'}]
    
    • Warts in the Coinbase REST API are smoothed out:
    # CBPro API returns raw candles from this call as tuples, which would
    # require user to look up value meaning in API docs. This python API
    # returns candles as a list of dicts, similar to other API endpoints.
    
    # Get first candle:
    >>> client.get_product_historic_rates('BTC-USD')[0]
    {'time': datetime.datetime(2019, 3, 21, 0, 6),
    'low': Decimal('2463.3'),
    'high': Decimal('2463.31'),
    'open': Decimal('2463.3'),
    'close': Decimal('2463.31'),
    'volume': Decimal('0.006')}
    
    • Python API uses typing available in Python3:
    # Example function prototype in API
    def get_product_ticker(self, product_id: str) -> Dict[str, Any]:
    
  • Exceptions to enable easy handling of Coinbase error responses

>>> client.get_product_ticker(product_id='fake_product')
coinbasepro.exceptions.CoinbaseAPIError: NotFound
>>> auth_client = cbp.AuthenticatedClient(key='fake',
                                          secret='fake',
                                          passphrase='fake')
>>> auth_client.get_accounts()
coinbasepro.exceptions.BadRequest: Invalid API Key
# Authenticated client using API key which doesn't have withdrawal
# privileges:
>>> auth_client.withdraw_to_coinbase(0.01, 'BTC', 'fake_acct_id')
coinbasepro.exceptions.InvalidAuthorization: Forbidden
# This call throws a BadRequest exception
>>> auth_client.get_order('invalid_order_num')
coinbasepro.exceptions.BadRequest: Invalid order id

# CoinbaseAPIError is the parent exception for all exceptions the API
# throws, so catching this will catch anything
>>> try:
>>>     auth_client.get_order('invalid_order_num')
>>> except cbp.exceptions.CoinbaseAPIError as e:
>>>     print('Caught error: {}'.format(e))
Caught error: Invalid order id

Installation

$ pip install coinbasepro

coinbasepro's People

Contributors

acontry avatar imalovitsa-exos avatar khoo-j avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.