An open API service indexing awesome lists of open source software.

https://github.com/xsa-dev/weex-client

Modern async-first Weex API client for Python 3.14+
https://github.com/xsa-dev/weex-client

Last synced: 6 days ago
JSON representation

Modern async-first Weex API client for Python 3.14+

Awesome Lists containing this project

README

          

# Weex Python API Client

A secure and robust Python client for interacting with the Weex cryptocurrency exchange API.

## Features

- ✅ **Secure Authentication**: HMAC-SHA256 signature-based authentication with environment variable configuration
- ✅ **Error Handling**: Comprehensive error handling with proper logging
- ✅ **Type Hints**: Full type annotation support for better code completion
- ✅ **Logging**: Structured logging for debugging and monitoring
- ✅ **Timeout Protection**: Configurable request timeouts
- ✅ **Multiple APIs**: Support for both Contract and Spot trading APIs

## Installation

1. Install dependencies using UV:
```bash
uv sync
```

Or with pip:
```bash
pip install -r requirements.txt
```

## Configuration

### 1. Create Environment File

Copy the example environment file:
```bash
cp .env.example .env
```

### 2. Add Your API Credentials

Edit the `.env` file with your actual Weex API credentials:

```env
# Your Weex API Key (starts with 'weex_')
WEEX_API_KEY=your_actual_api_key_here

# Your Weex Secret Key
WEEX_SECRET_KEY=your_actual_secret_key_here

# Your Weex API Passphrase
WEEX_PASSPHRASE=your_actual_passphrase_here

# Environment: development, staging, or production
WEEX_ENVIRONMENT=development

# Optional: Custom timeout settings (seconds)
API_TIMEOUT=30

# Optional: Enable debug logging
DEBUG=false
```

### 3. Getting API Credentials

To get your Weex API credentials:

1. Log into your Weex account at [weex.com](https://weex.com)
2. Navigate to API Management in your account settings
3. Create a new API key with the required permissions
4. Copy the API Key, Secret Key, and Passphrase to your `.env` file

## Usage

### Basic Usage

```python
from main import WeexAPIClient

# Initialize the client
client = WeexAPIClient()

# Get position data
position_data = client.get_contract_data(
"/capi/v2/account/position/singlePosition",
"?symbol=cmt_btcusdt"
)

print(position_data)
```

### Examples

The client includes example functions:

```python
# Get position data
python main.py

# This will run the example_get_position() function
```

### Available Methods

#### Contract API (Futures Trading)
```python
client = WeexAPIClient()

# GET requests to contract API
data = client.get_contract_data("/capi/v2/account/position/singlePosition", "?symbol=cmt_btcusdt")

# POST requests to contract API
order_data = client.post_contract_data("/capi/v2/order/placeOrder", {
"symbol": "cmt_btcusdt",
"client_oid": "unique_order_id",
"size": "0.01",
"type": "1",
"order_type": "0",
"match_price": "1",
"price": "80000"
})
```

#### Spot API (Regular Trading)
```python
client = WeexAPIClient()

# GET requests to spot API
data = client.get_spot_data("/api/v2/spot/account", "")
```

## Security Features

- ✅ **No Hardcoded Credentials**: API keys are loaded from environment variables
- ✅ **Git Protection**: `.env` file is automatically ignored by Git
- ✅ **Input Validation**: All inputs are validated before API calls
- ✅ **Secure Headers**: Proper User-Agent and locale headers
- ✅ **Logging Protection**: Sensitive data is never logged

## Error Handling

The client includes comprehensive error handling:

- **Connection Errors**: Network connectivity issues
- **Timeout Errors**: Requests that exceed the timeout period
- **Authentication Errors**: Invalid API credentials
- **API Errors**: HTTP error responses from the API
- **JSON Errors**: Invalid response format

All errors are logged with detailed information for debugging.

## Logging

The client logs to both console and file (`weex_api.log`):

- **INFO**: Successful requests and general information
- **ERROR**: Failed requests and error details
- **DEBUG**: Detailed request/response information (when DEBUG=true)

## Development

### Project Structure

```
py_startup/
├── main.py # Main API client implementation
├── .env.example # Environment file template
├── .gitignore # Git ignore rules
├── pyproject.toml # Project dependencies
├── uv.lock # Dependency lock file
├── README.md # This file
└── weex_api.log # Generated log file
```

### Running Tests

The example functions serve as basic integration tests:

```bash
# Test with your actual API credentials
python main.py
```

## Troubleshooting

### Common Issues

1. **"Missing required API credentials"**
- Ensure your `.env` file is properly configured
- Check that all three values are present: API key, secret key, and passphrase

2. **"Request timeout"**
- Increase the `API_TIMEOUT` value in your `.env` file
- Check your internet connection

3. **"Connection error"**
- Verify Weex API status at [status.weex.com](https://status.weex.com)
- Check if your API keys have the required permissions

4. **Authentication errors**
- Verify your API credentials are correct
- Ensure your API keys are enabled for the endpoints you're accessing

### Debug Mode

Enable debug logging by setting:
```env
DEBUG=true
```

This will provide detailed request/response information for troubleshooting.

## API Reference

For complete Weex API documentation, visit:
- [Weex API Documentation](https://www.weex.com/api-doc/)
- [API Status](https://status.weex.com)
- [GitHub Repository](https://github.com/weex-exchange/api-docs)

## Support

If you encounter issues:

1. Check the log file `weex_api.log` for detailed error information
2. Verify your API credentials and permissions
3. Check Weex API status for any service disruptions
4. Review the Weex API documentation for endpoint-specific requirements

## License

This project is provided as-is for educational and development purposes.