Skip to main content

A client for interacting with crypto APIs.

Project description

crypto-api-client

A simple client library for multiple cryptocurrency exchange APIs.

Features

  • Multi-Exchange Support
    • Supports BINANCE, bitbank, bitFlyer, Coincheck, GMO Coin, and Upbit (Most exchanges currently support ticker and order book retrieval. Additional endpoints can be added upon request or via pull requests).
  • Async/Await Support
    • Fully asynchronous implementation using Python's asyncio.
  • HTTP/2 and Connection Pooling
    • Built-in support for HTTP/2 and connection pooling for efficient API communication.
  • Decimal Precision
    • Uses Decimal type instead of float or int to maintain precision in financial calculations.
  • Unified Timestamp Handling
    • All timestamps are normalized to UTC datetime objects, regardless of exchange location or data format.
  • Callback System
    • Register callbacks to execute before and after HTTP requests.
  • Automatic Secret Masking
    • Automatically masks sensitive information (within reasonable limits) to prevent accidental exposure in logs or error monitoring services.
  • Shared Rate Limiting
    • Provides a mechanism to share rate limit state across multiple clients using Redis.
  • Proxy Support
    • Full support for HTTP/HTTPS proxies with optional authentication.

Examples

Public API

Retrieve ticker from BINANCE:

uv run python examples/binance/ticker.py --pair BTCUSDT --zone Asia/Tokyo

Retrieve order book from bitFlyer:

uv run python examples/bitflyer/board.py --pair BTC_JPY --price-band 100000

Documentation

Generate HTML documentation with the following commands:

# Generate API documentation
uv run sphinx-apidoc -f -o docs/source src/crypto_api_client

# Clean build directory
uv run sphinx-build -M clean docs/source docs/build

# Build English HTML documentation
uv run sphinx-build -M html docs/source docs/build

# Build Japanese HTML documentation
uv run sphinx-build -M html docs/source docs/build/ja -D language=ja

The generated documentation will be available at:

  • English: docs/build/html/index.html
  • Japanese: docs/build/ja/html/index.html

For terminology used throughout this library, please refer to the glossary.

Non-Goals

This library explicitly does not aim to:

  • Synchronous Support: Only asynchronous connections are supported. Synchronous connections are not provided.
  • High-Frequency Trading (HFT): This library is not designed for HFT use cases.
  • Business Logic Error Handling: Error handling and recovery strategies driven by business logic are not implemented.
  • Intelligent Types: Advanced types better suited for higher-level abstractions are not implemented.
  • Default Error Validation: HTTP request precondition/postcondition error handling is not invoked by default.
    • Error handling for exchange-detected errors is implemented as add-on callbacks.
      • Use default error handling like examples/bitflyer/default_response_validator.py, or integrate custom error handling like examples/bitflyer/custom_response_validator.py.

Debugging with Visual Studio Code

Sample debug configurations are defined in launch.json.sample. Run them from the Run and Debug panel.

Installation

pip install crypto-api-client

Or using uv:

uv add crypto-api-client

Quick Start

Public API Example

import asyncio
from crypto_api_client import Exchange, create_session
from crypto_api_client.bitflyer import TickerRequest

async def main():
    async with create_session(Exchange.BITFLYER) as session:
        ticker = await session.api.ticker(TickerRequest(product_code="BTC_JPY"))
        print(f"Last Price: {ticker.ltp}")

asyncio.run(main())

Private API Example (with Authentication)

import asyncio
import os
from pydantic import SecretStr
from crypto_api_client import Exchange, create_session

async def main():
    async with create_session(
        Exchange.BITFLYER,
        api_key=SecretStr(os.environ["BITFLYER_API_KEY"]),
        api_secret=SecretStr(os.environ["BITFLYER_API_SECRET"])
    ) as session:
        balances = await session.api.getbalance()
        for balance in balances:
            print(f"{balance.currency_code}: {balance.available}")

asyncio.run(main())

Requirements

Development

Getting the Development Dockerfile

% curl -L -O https://raw.githubusercontent.com/uraitakahito/hello_python_uv/refs/tags/1.1.1/Dockerfile
% curl -L -O https://raw.githubusercontent.com/uraitakahito/hello_python_uv/refs/tags/1.1.1/docker-entrypoint.sh
% chmod 755 docker-entrypoint.sh

Detailed instructions for setting up the development environment are documented as comments within the downloaded Dockerfile.

Running Tests

uv run pytest tests/ -v

Running Examples

# Public API examples
uv run python examples/bitflyer/ticker.py --pair BTC_JPY --zone Asia/Tokyo
uv run python examples/binance/ticker.py --pair BTCUSDT --zone Asia/Tokyo

# Private API examples (requires API credentials)
export BITFLYER_API_KEY="your_api_key"
export BITFLYER_API_SECRET="your_api_secret"
uv run python examples/bitflyer/balances.py

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

crypto_api_client-0.6.84.tar.gz (92.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

crypto_api_client-0.6.84-py3-none-any.whl (192.9 kB view details)

Uploaded Python 3

File details

Details for the file crypto_api_client-0.6.84.tar.gz.

File metadata

  • Download URL: crypto_api_client-0.6.84.tar.gz
  • Upload date:
  • Size: 92.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for crypto_api_client-0.6.84.tar.gz
Algorithm Hash digest
SHA256 23360bb8ab587863f66af075a210021f8e7ffb2e5aec5cb6481a1d458d8e28f1
MD5 37ffdb082308c81c5c0599a257130fda
BLAKE2b-256 e6b0005e149928d617e8b80e8540bba366ce0a3b67a326f950357aacd1c453d1

See more details on using hashes here.

File details

Details for the file crypto_api_client-0.6.84-py3-none-any.whl.

File metadata

File hashes

Hashes for crypto_api_client-0.6.84-py3-none-any.whl
Algorithm Hash digest
SHA256 f93c74c341a8ebdcc7b062626d24b39713d3a41de50f7e55914e3d2299b87076
MD5 712d970dcd5d350d55cc1b6876e00c66
BLAKE2b-256 47241b79e0951d5cc7bbef6957071d0a3bdf979ad079d47948d30e3b7fe6c19b

See more details on using hashes here.

Supported by

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