Skip to main content

Coinbase Exchange API client library.

Project description

cb-exchange-lib

Coinbase Exchange API client library.


Installation:

python -m pip install [--upgrade] cb-exchange-lib

Endpoints:

Info:

Private endpoints require authentication using your Coinbase Exchange API key. You can generate API keys here.

Private:

  • Accounts
  • AddressBook
  • CoinbaseAccounts
  • Conversions
  • Deposits
  • PaymentMethods
  • Transfers
  • Withdrawals
  • Fees
  • Fills
  • Orders
  • Oracle
  • Profiles
  • Reports
  • Users

Public:

  • Time
  • Currencies
  • Products
  • WrappedAssets

Any endpoint can take these keyword arguments:

  • environment: str - The API environment: production or sandbox (defaults to: production);
  • retries: int - Total number of retries to allow (defaults to: 3);
  • backoff: int - A backoff factor to apply between attempts after the second try (defaults to: 1);
  • timeout: int - How long to wait for the server to send data before giving up (defaults to: 30);
  • cache: bool - Use caching (defaults to: True);
  • debug: bool - Set to True to log all requests/responses to/from server (defaults to: False);
  • logger: Logger - The handler to be used for logging. If given, and level is above DEBUG, all debug messages will be ignored.

For private endpoints only:

  • key: str - The API key;
  • passphrase: str - The API passphrase;
  • secret: str - The API secret.

Any of the endpoints can be instantiated or used as a context-manager:

from cb_exchange_lib import Time, Accounts

environment: str = "sandbox"
credentials: dict = {
    "key": "your key",
    "passphrase": "your passphrase",
    "secret": "your secret",
}  # be careful where you keep your credentials!


if __name__ == '__main__':

    print("*" * 80)

    endpoint = Accounts(**credentials, environment=environment)
    accounts = endpoint.get_accounts()

    for account in accounts:
        print(account)

    endpoint.close()

    # or

    with Time(environment=environment) as endpoint:

        print("*" * 80)
        time = endpoint.get_time()
        print(time)

Resources:

For each mapped resource you must read the documentation. All the parameters these resources can take are described in the official documentation.

Time

Accounts

  • get_accounts()

    Get a list of trading accounts from the profile of the API key.

  • get_account()

    Information for a single account. Use this endpoint when you know the account_id. API key must belong to the same profile as the account.

  • get_account_holds()

    List the holds of an account that belong to the same profile as the API key. Holds are placed on an account for any active orders or pending withdraw requests. As an order is filled, the hold amount is updated. If an order is canceled, any remaining hold is removed. For withdrawals, the hold is removed after it is completed.

  • get_account_ledger()

    Lists ledger activity for an account. This includes anything that would affect the accounts balance - transfers, trades, fees, etc. List account activity of the API key's profile. Account activity either increases or decreases your account balance.

  • get_account_transfers()

    Lists past withdrawals and deposits for an account.

AddressBook

CoinbaseAccounts

  • get_wallets()

    Gets all the user's available Coinbase wallets (These are the wallets/accounts that are used for buying and selling on www.coinbase.com)

  • generate_crypto_address()

    Generates a one-time crypto address for depositing crypto.

    Info:

    You can generate an address for crypto deposits. See the Coinbase Accounts section for information on how to retrieve your coinbase account ID.

Conversions

  • convert_currency()

    Converts funds from from currency to to currency. Funds are converted on the from account in the profile_id profile.

    Caution:

    Users whose USD and USDC accounts are unified do not have access to the conversion endpoint, and conversions from USDC to USD are automatic upon deposit.

  • get_conversion()

    Gets a currency conversion by id (i.e. 41554f00-5c34-4f09-b800-2a878e52f2ea).

Currencies

  • get_currencies()

    Gets a list of all known currencies.

    Note:

    Not all currencies may be currently in use for trading.

  • get_currency()

    Gets a single currency by id.

Deposits

PaymentMethods

Transfers

  • get_transfers()

    Gets a list of in-progress and completed transfers of funds in/out of any of the user's accounts.

  • get_transfer()

    Get information on a single transfer.

Withdrawals

Fees

  • get_fees()

    Get fees rates and 30 days trailing volume. This request returns your current maker & taker fee rates, as well as your 30-day trailing volume. Quoted rates are subject to change. For more information, see What are the fees on Coinbase Pro?.

Fills

  • get_fills()

    Get a list of fills. A fill is a partial or complete match on a specific order. Get a list of recent fills of the API key's profile.

Orders

  • get_orders()

    List your current open orders. Only open or un-settled orders are returned by default. As soon as an order is no longer open and settled, it will no longer appear in the default request. Open orders may change state between the request and the response depending on market conditions.

  • del_orders()

    With best effort, cancel all open orders. This may require you to make the request multiple times until all of the open orders are deleted.

  • create_order()

    Create an order. You can place two types of orders: limit and market. Orders can only be placed if your account has sufficient funds. Once an order is placed, your account funds will be put on hold for the duration of the order. How much and which funds are put on hold depends on the order type and parameters specified.

    Caution:

    Each profile can place a maximum of 500 open orders on a product. Once reached, the profile cannot place any new orders until the total number of open orders is below 500.

  • get_order()

    Get a single order by order_id.

  • del_order()

    Cancel a single open order by order_id.

Oracle

  • get_signed_prices()

    Get cryptographically signed prices ready to be posted on-chain using Compound's Open Oracle smart contract.

Products

  • get_products()

    Gets a list of available currency pairs for trading.

  • get_product()

    Get information on a single product.

  • get_product_book()

    Get a list of open orders for a product. The amount of detail shown can be customized with the level parameter.

  • get_product_candles()

    Historic rates for a product. Rates are returned in grouped buckets. Candle schema is of the form: [timestamp, price_low, price_high, price_open, price_close]

    Info:

    Historical rate data may be incomplete. No data is published for intervals where there are no ticks. Historical rates should not be polled frequently. If you need real-time information, use the trade and book endpoints along with the WebSocket feed.

  • get_product_stats()

    Gets 30day and 24hour stats for a product.

    Info:

    The volume property is in base currency units. Properties open, high, low are in quote currency units.

  • get_product_ticker()

    Gets snapshot information about the last trade (tick), best bid/ask and 24h volume.

    Real-time updates:

    Coinbase recommends that you get real-time updates by connecting with the WebSocket stream and listening for match messages, rather than polling.

  • get_product_trades()

    Gets a list the latest trades for a product.

Profiles

  • get_profiles()

    Gets a list of all of the current user's profiles.

  • create_profile()

    Create a new profile. Will fail if no name is provided or if user already has max number of profiles.

  • transfer_funds()

    Transfer an amount of currency from one profile to another.

  • get_profile()

    Information for a single profile. Use this endpoint when you know the profile_id.

  • rename_profile()

    Rename a profile. Names 'default' and 'margin' are reserved.

  • del_profile()

    Deletes the profile specified by profile_id and transfers all funds to the profile specified by to. Fails if there are any open orders on the profile to be deleted.

Reports

  • get_reports()

    Gets a list of all user generated reports.

    Info:

    Once a report request has been accepted for processing, you can poll the report resource endpoint at /reports/{report_id} for its status. When status is ready, the final report is uploaded and available at file_url.

  • create_report()

    Generates a report. You can create reports with historical data for all report types. Balance reports can be snapshots of historical or current data. Reports provide batches of historic information about your profile in various human and machine readable forms.

    Info:

    A report is generated when resources are available. You can poll the report resource endpoint at /reports/{report_id} for its status. When status is ready, the final report is uploaded and available at file_url.

  • get_report()

    Get a specific report by report_id.

    Info:

    Once a report request has been accepted for processing, you can poll the report resource endpoint at /reports/{report_id} for its status. When status is ready, the final report is uploaded and available at file_url.

Users

  • get_exchange_limits()

    Gets exchange limits information for a single user.

    Info:

    This request returns information on your payment method transfer limits, as well as buy/sell limits per currency.

WrappedAssets


Websocket clients:

Any of the websocket clients can take these keyword arguments:

  • channels: The channels for subscription;
  • product_ids: The products IDs for subscription;
  • environment: The API environment: production or sandbox (defaults to: production);
  • debug: Set to True to log all requests/responses to/from server (defaults to: False);
  • logger: The handler to be used for logging; If given, and level is above DEBUG, all debug messages will be ignored.

Note:

For information about Websocket feed channels visit the documentation.

For DirectMarketData only:

  • key: The API key;
  • passphrase: The API passphrase;
  • secret: The API secret;

Examples:

MarketData

from cb_exchange_lib import MarketData

environment: str = "sandbox"

if __name__ == '__main__':

    client = MarketData(
        environment=environment,
        debug=True,
        channels=["ticker"],
        product_ids=["BTC-USD"],
    )

    client.listen()

    try:
        for item in client.queue:
            print(item)
    except KeyboardInterrupt:
        client.close()

DirectMarketData

from cb_exchange_lib import DirectMarketData

environment: str = "sandbox"

credentials: dict = {
    "key": "your key",
    "passphrase": "your passphrase",
    "secret": "your secret",
}  # be careful where you keep your credentials!


if __name__ == '__main__':

    client = DirectMarketData(
        **credentials,
        environment=environment,
        debug=True,
        channels=[
            {
                "name": "ticker",
                "product_ids": [
                    "BTC-USD"
                ]
            }
        ]
    )
    client.listen()

    try:
        for item in client.queue:
            print(item)
    except KeyboardInterrupt:
        client.close()


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cb-exchange-lib-3.0.3.tar.gz (30.2 kB view hashes)

Uploaded Source

Built Distribution

cb_exchange_lib-3.0.3-py3-none-any.whl (27.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page