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
Decimaltype instead offloatorintto maintain precision in financial calculations.
- Uses
- Unified Timestamp Handling
- All timestamps are normalized to UTC
datetimeobjects, regardless of exchange location or data format.
- All timestamps are normalized to UTC
- 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 likeexamples/bitflyer/custom_response_validator.py.
- Use default error handling like
- Error handling for exchange-detected errors is implemented as add-on callbacks.
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
- Python 3.13+
- See pyproject.toml for complete dependency list
Development
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
Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues for:
- Adding support for additional exchange endpoints
- Bug fixes and improvements
- Documentation enhancements
- New examples
License
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file crypto_api_client-0.6.83.tar.gz.
File metadata
- Download URL: crypto_api_client-0.6.83.tar.gz
- Upload date:
- Size: 92.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
02acebe1d1e5207573e670fb60043e3bc6b9bcd6d69f5b611239bffd2efa8efe
|
|
| MD5 |
8e36f94fe7593ded2829d279cfb9270e
|
|
| BLAKE2b-256 |
bfe0f24513bd42cc6898a49ff6ee05f00cbe279052bbd01d1ea8a4a6655118cf
|
File details
Details for the file crypto_api_client-0.6.83-py3-none-any.whl.
File metadata
- Download URL: crypto_api_client-0.6.83-py3-none-any.whl
- Upload date:
- Size: 192.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f3757663c41159a2a3393bc87731ee4c28f2ad16c0356f7d79bc969080d76bcf
|
|
| MD5 |
b99b77eccace551f1526d7d45bddd6e1
|
|
| BLAKE2b-256 |
2f94c17f5d546af4bbfdcbb171a979db385ea041bd721f55a068ed0d534253d8
|