|
| 1 | +# Binance Python Alpha SDK |
| 2 | + |
| 3 | +[](https://github.com/binance/binance-connector-python/actions) |
| 4 | +[](https://github.com/binance/binance-connector-python/issues) |
| 5 | +[](https://black.readthedocs.io/en/stable/) |
| 6 | +[](https://pypi.python.org/pypi/binance-sdk-alpha) |
| 7 | +[](https://pypi.org/project/binance-sdk-alpha/) |
| 8 | +[](https://www.python.org/downloads/) |
| 9 | +[](https://github.com/binance/binance-connector-python/security) |
| 10 | +[](https://opensource.org/licenses/MIT) |
| 11 | + |
| 12 | +This is a client library for the Binance Alpha API, enabling developers to interact programmatically with Binance Alpha. The library provides tools to access curated early-stage token data, track Alpha project metrics and integrate discovery-focused market information into applications through the REST API: |
| 13 | +- [REST API](./src/binance_sdk_alpha/rest_api/rest_api.py) |
| 14 | + |
| 15 | +## Table of Contents |
| 16 | + |
| 17 | +- [Supported Features](#supported-features) |
| 18 | +- [Installation](#installation) |
| 19 | +- [Documentation](#documentation) |
| 20 | +- [REST APIs](#rest-apis) |
| 21 | +- [Testing](#testing) |
| 22 | +- [Contributing](#contributing) |
| 23 | +- [Licence](#licence) |
| 24 | + |
| 25 | +## Supported Features |
| 26 | + |
| 27 | +- REST API Endpoints: |
| 28 | + - `/bapi/defi/v1/*` |
| 29 | +- Inclusion of test cases and examples for quick onboarding. |
| 30 | + |
| 31 | +## Installation |
| 32 | + |
| 33 | +To use this library, ensure your environment is running Python version **3.9** or later. |
| 34 | + |
| 35 | +```bash |
| 36 | +pip install binance-sdk-alpha |
| 37 | +``` |
| 38 | + |
| 39 | +## Documentation |
| 40 | + |
| 41 | +For detailed information, refer to the [Binance API Documentation](https://developers.binance.com/docs/alpha/Introduction). |
| 42 | + |
| 43 | +### REST APIs |
| 44 | + |
| 45 | +All REST API endpoints are available through the [`rest_api`](./src/binance_sdk_alpha/rest_api/rest_api.py) module. The REST API enables you to fetch market data, manage trades, and access account information. Note that some endpoints require authentication using your Binance API credentials. |
| 46 | + |
| 47 | +```python |
| 48 | +import logging |
| 49 | + |
| 50 | +from binance_common.configuration import ConfigurationRestAPI |
| 51 | +from binance_common.constants import ALPHA_REST_API_PROD_URL |
| 52 | +from binance_sdk_alpha.alpha import Alpha |
| 53 | + |
| 54 | +logging.basicConfig(level=logging.INFO) |
| 55 | +configuration = ConfigurationRestAPI(api_key="your-api-key", api_secret="your-api-secret", base_path=ALPHA_REST_API_PROD_URL) |
| 56 | + |
| 57 | +client = Alpha(config_rest_api=configuration) |
| 58 | + |
| 59 | +try: |
| 60 | + response = client.rest_api.get_exchange_info() |
| 61 | + |
| 62 | + data = response.data() |
| 63 | + logging.info(f"get_exchange_info() response: {data}") |
| 64 | +except Exception as e: |
| 65 | + logging.error(f"get_exchange_info() error: {e}") |
| 66 | +``` |
| 67 | + |
| 68 | +More examples can be found in the [`examples/rest_api`](./examples/rest_api/) folder. |
| 69 | + |
| 70 | +#### Configuration Options |
| 71 | + |
| 72 | +The REST API supports the following advanced configuration options: |
| 73 | + |
| 74 | +- `timeout`: Timeout for requests in milliseconds (default: 1000 ms). |
| 75 | +- `proxy`: Proxy configuration: |
| 76 | + - `host`: Proxy server hostname. |
| 77 | + - `port`: Proxy server port. |
| 78 | + - `protocol`: Proxy protocol (http or https). |
| 79 | + - `auth`: Proxy authentication credentials: |
| 80 | + - `username`: Proxy username. |
| 81 | + - `password`: Proxy password. |
| 82 | +- `keep_alive`: Enable HTTP keep-alive (default: true). |
| 83 | +- `compression`: Enable response compression (default: true). |
| 84 | +- `retries`: Number of retry attempts for failed requests (default: 3). |
| 85 | +- `backoff`: Delay in milliseconds between retries (default: 1000 ms). |
| 86 | +- `https_agent`: Custom HTTPS agent for advanced TLS configuration. |
| 87 | +- `private_key`: RSA or ED25519 private key for authentication. |
| 88 | +- `private_key_passphrase`: Passphrase for the private key, if encrypted. |
| 89 | + |
| 90 | +##### Timeout |
| 91 | + |
| 92 | +You can configure a timeout for requests in milliseconds. If the request exceeds the specified timeout, it will be aborted. See the [Timeout example](./docs/rest_api/timeout.md) for detailed usage. |
| 93 | + |
| 94 | +##### Proxy |
| 95 | + |
| 96 | +The REST API supports HTTP/HTTPS proxy configurations. See the [Proxy example](./docs/rest_api/proxy.md) for detailed usage. |
| 97 | + |
| 98 | +##### Keep-Alive |
| 99 | + |
| 100 | +Enable HTTP keep-alive for persistent connections. See the [Keep-Alive example](./docs/rest_api/keepAlive.md) for detailed usage. |
| 101 | + |
| 102 | +##### Compression |
| 103 | + |
| 104 | +Enable or disable response compression. See the [Compression example](./docs/rest_api/compression.md) for detailed usage. |
| 105 | + |
| 106 | +##### Retries |
| 107 | + |
| 108 | +Configure the number of retry attempts and delay in milliseconds between retries for failed requests. See the [Retries example](./docs/rest_api/retries.md) for detailed usage. |
| 109 | + |
| 110 | +##### HTTPS Agent |
| 111 | + |
| 112 | +Customize the HTTPS agent for advanced TLS configurations. See the [HTTPS Agent example](./docs/rest_api/httpsAgent.md) for detailed usage. |
| 113 | + |
| 114 | +##### Key Pair Based Authentication |
| 115 | + |
| 116 | +The REST API supports key pair-based authentication for secure communication. You can use `RSA` or `ED25519` keys for signing requests. See the [Key Pair Based Authentication example](./docs/rest_api/key-pair-authentication.md) for detailed usage. |
| 117 | + |
| 118 | +##### Certificate Pinning |
| 119 | + |
| 120 | +To enhance security, you can use certificate pinning with the `https_agent` option in the configuration. This ensures the client only communicates with servers using specific certificates. See the [Certificate Pinning example](./docs/rest_api/certificate-pinning.md) for detailed usage. |
| 121 | + |
| 122 | +#### Error Handling |
| 123 | + |
| 124 | +The REST API provides detailed error types to help you handle issues effectively: |
| 125 | + |
| 126 | +- `ClientError`: Represents an error that occurred in the SDK client. |
| 127 | +- `RequiredError`: Thrown when a required parameter is missing or undefined. |
| 128 | +- `UnauthorizedError`: Indicates missing or invalid authentication credentials. |
| 129 | +- `ForbiddenError`: Access to the requested resource is forbidden. |
| 130 | +- `TooManyRequestsError`: Rate limit exceeded. |
| 131 | +- `RateLimitBanError`: IP address banned for exceeding rate limits. |
| 132 | +- `ServerError`: Internal server error, optionally includes a status code. |
| 133 | +- `NetworkError`: Issues with network connectivity. |
| 134 | +- `NotFoundError`: Resource not found. |
| 135 | +- `BadRequestError`: Invalid request or one that cannot be served. |
| 136 | + |
| 137 | +See the [Error Handling example](./docs/rest_api/error-handling.md) for detailed usage. |
| 138 | + |
| 139 | +If `base_path` is not provided, it defaults to `https://www.binance.com`. |
| 140 | + |
| 141 | +## Testing |
| 142 | + |
| 143 | +To run the tests, ensure you have [Poetry](https://python-poetry.org/) installed, then execute the following commands: |
| 144 | + |
| 145 | +```bash |
| 146 | +poetry install |
| 147 | +poetry run pytest ./tests |
| 148 | +``` |
| 149 | + |
| 150 | +The tests cover: |
| 151 | +* REST API endpoints |
| 152 | +* Error handling |
| 153 | +* Edge cases |
| 154 | + |
| 155 | +## Contributing |
| 156 | + |
| 157 | +Contributions are welcome! |
| 158 | + |
| 159 | +Since this repository contains auto-generated code, we encourage you to start by opening a GitHub issue to discuss your ideas or suggest improvements. This helps ensure that changes align with the project's goals and auto-generation processes. |
| 160 | + |
| 161 | +To contribute: |
| 162 | + |
| 163 | +1. Open a GitHub issue describing your suggestion or the bug you've identified. |
| 164 | +2. If it's determined that changes are necessary, the maintainers will merge the changes into the main branch. |
| 165 | + |
| 166 | +Please ensure that all tests pass if you're making a direct contribution. Submit a pull request only after discussing and confirming the change. |
| 167 | + |
| 168 | +Thank you for your contributions! |
| 169 | + |
| 170 | +## Licence |
| 171 | + |
| 172 | +This project is licensed under the MIT License. See the [LICENCE](./LICENCE) file for details. |
0 commit comments