This Python module allows users to interact with the CEX.IO API using RESTful GET
and POST
requests. The module supports both public and private API calls, ensuring a broad range of functionalities from fetching market data to managing account details.
To use the CEX.IO API Python module, you'll need to have Python 3 installed. You can install the required dependencies using pip
.
pip install requests urllib3
First, import the Api
class and initialize it with your CEX.IO username, API key, and API secret. You can obtain your API key and secret from your CEX.IO profile.
from your_module_name import Api
api = Api('your_username', 'your_api_key', 'your_api_secret')
Fetch the latest price for the BTC/USD pair:
result = api.get_last_price('BTC', 'USD')
print(result)
Fetch your account balance:
balance = api.get_balance()
print(balance)
get_currency_limits()
: Get currency limits.get_ticker(symbol1='BTC', symbol2='USD')
: Get ticker for a pair.get_tickers(symbols=None)
: Get tickers for given markets.get_last_price(symbol1='BTC', symbol2='USD')
: Get the last price for a pair.get_last_prices(symbols)
: Get last prices for given markets.get_order_book(symbol1, symbol2, depth=10)
: Get order book.get_trade_history(symbol1, symbol2, since)
: Get trade history.
get_balance()
: Get account balance.get_open_orders(symbol1='', symbol2='')
: Get open orders.convert(symbol1, symbol2, amount)
: Convert one symbol to another.place_order(order_type, amount, price, symbol1, symbol2)
: Place an order.cancel_order(order_id)
: Cancel an order.get_order_details(order_id)
: Get order details.get_crypto_address(currency)
: Get crypto address.get_my_fee()
: Get user's fee.
Logging is configured at the INFO level. You can change the logging level to DEBUG to get more detailed output for debugging purposes.
import logging
logging.basicConfig(level=logging.DEBUG)
Custom exceptions are provided to handle different error scenarios:
ApiError
: Base class for all API-related errors.NetworkError
: Raised for network-related errors.InvalidCredentialsError
: Raised for invalid API credentials.ApiResponseError
: Raised for errors in API responses.
Example of handling errors:
try:
result = api.get_last_price('BTC', 'USD')
print(result)
except ApiError as e:
logging.error(f"An API error occurred: {e}")
The module provides an option to enable or disable logging dynamically when initializing the Api
class instance. To enable logging, pass logging_enabled=True
during initialization.
api = Api('your_username', 'your_api_key', 'your_api_secret', logging_enabled=True)
api = Api('your_username', 'your_api_key', 'your_api_secret', logging_enabled=False)
api = Api('your_username', 'your_api_key', 'your_api_secret')
try:
last_price = api.get_last_price('BTC', 'USD')
print(last_price)
except ApiError as e:
logging.error(f"An API error occurred: {e}")
try:
order = api.place_order('buy', 1.0, 50000, 'BTC', 'USD')
print(order)
except ApiError as e:
logging.error(f"An API error occurred: {e}")
Contributions are welcome! Please fork the repository and submit a pull request.
This project is licensed under the MIT License.