-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api_connection.py
More file actions
107 lines (89 loc) · 4.26 KB
/
test_api_connection.py
File metadata and controls
107 lines (89 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
import sys
import time
from binance.client import Client
from binance.exceptions import BinanceAPIException, BinanceRequestException, BinanceOrderException
from dotenv import load_dotenv
def print_section(title):
print(f"\n{'='*50}")
print(f"{title.upper():^50}")
print(f"{'='*50}")
def print_step(step):
print(f"\n🔹 {step}...")
def test_connection():
print_section("Binance Testnet API Connection Test")
# Load environment variables
load_dotenv()
# Get API keys from environment variables
api_key = os.getenv("BINANCE_TESTNET_API_KEY") or os.getenv("BINANCE_API_KEY")
api_secret = os.getenv("BINANCE_TESTNET_API_SECRET") or os.getenv("BINANCE_API_SECRET")
if not api_key or not api_secret:
print("❌ Error: API keys not found in environment variables")
print("Please create a .env file with your Binance Testnet API keys")
print("See .env.example for reference")
return False
print(f"🔑 Using API Key: {api_key[:8]}...{api_key[-4:]}")
try:
# Test 1: Initialize client
print_step("Initializing Binance Client")
client = Client(api_key, api_secret, testnet=True)
print("✅ Client initialized successfully")
# Test 2: Check server time
print_step("Checking server time")
server_time = client.get_server_time()
print(f"✅ Server time: {server_time['serverTime']} ({time.ctime(server_time['serverTime']/1000)})")
# Test 3: Get exchange info
print_step("Fetching exchange info")
exchange_info = client.futures_exchange_info()
print(f"✅ Exchange info received. Rate limits:")
for limit in exchange_info.get('rateLimits', [])[:2]: # Show first 2 rate limits
print(f" - {limit['rateLimitType']}: {limit['limit']} requests per {limit['intervalNum']} {limit['interval']}")
# Test 4: Get account balance
print_step("Fetching account balance")
try:
account = client.futures_account_balance()
print("✅ Account balance received")
# Filter and print non-zero balances
non_zero_balances = [b for b in account if float(b['balance']) > 0 or float(b['withdrawAvailable']) > 0]
if non_zero_balances:
print("\n💰 Non-zero balances:")
for balance in non_zero_balances:
print(f" - {balance['asset']}: {balance['balance']} (Available: {balance['withdrawAvailable']})")
else:
print("ℹ️ No non-zero balances found")
except Exception as e:
print(f"⚠️ Could not fetch account balance: {str(e)}")
# Test 5: Get order book
print_step("Testing order book")
try:
symbol = 'BTCUSDT'
order_book = client.futures_order_book(symbol=symbol, limit=5)
print(f"✅ {symbol} Order Book (Top 5):")
print(f" 🔼 Bids: {order_book['bids'][0][0]:>10} (Qty: {order_book['bids'][0][1]:.4f})")
print(f" 🔽 Asks: {order_book['asks'][0][0]:>10} (Qty: {order_book['asks'][0][1]:.4f})")
except Exception as e:
print(f"⚠️ Could not fetch order book: {str(e)}")
return True
except BinanceRequestException as e:
print("\n❌ Binance Request Exception:")
print(f" - Status Code: {e.status_code}")
print(f" - Message: {e.message}")
print(f" - Request: {e.request}")
except BinanceAPIException as e:
print("\n❌ Binance API Exception:")
print(f" - Status Code: {e.status_code}")
print(f" - Error Code: {e.code}")
print(f" - Message: {e.message}")
if e.status_code == 401:
print(" 🔒 Possible issues:")
print(" 1. Invalid API key/secret")
print(" 2. IP not whitelisted")
print(" 3. Missing required permissions")
elif e.status_code == 429:
print(" ⚠️ Rate limit exceeded. Please wait and try again.")
except Exception as e:
print(f"\n❌ Unexpected error: {type(e).__name__}")
print(f" - {str(e)}")
return False
if __name__ == "__main__":
test_connection()