-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbybit_api_test.py
More file actions
112 lines (94 loc) · 3.71 KB
/
bybit_api_test.py
File metadata and controls
112 lines (94 loc) · 3.71 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
108
109
110
111
112
import time
import hmac
import hashlib
import requests
import json
def get_bybit_base_url():
return "https://api.bybit.com"
def sign_bybit_request(secret_key: str, param_str: str) -> str:
"""Sign Bybit API request correctly"""
signature = hmac.new(
secret_key.encode('utf-8'),
param_str.encode('utf-8'),
hashlib.sha256
).hexdigest()
return signature
def test_bybit_endpoints():
api_key = "8ASH9Yxn0CJf93pRZb"
secret_key = "GwHLhjJNcKTIwUVnhesAFN9J2jcVvcNhm0gI"
# Test different endpoints
endpoints_to_test = [
"/v5/position/list",
"/v5/position/list",
"/v5/account/wallet-balance",
"/v5/account/info",
"/v5/position/list"
]
for i, endpoint in enumerate(endpoints_to_test):
print(f"\n=== Test {i+1}: {endpoint} ===")
try:
base_url = get_bybit_base_url()
timestamp = str(int(time.time() * 1000))
recv_window = "5000"
# Different parameter sets for different endpoints
if "position" in endpoint:
params = {
"api_key": api_key,
"category": "linear",
"recv_window": recv_window,
"timestamp": timestamp
}
elif "account" in endpoint:
params = {
"api_key": api_key,
"accountType": "UNIFIED",
"recv_window": recv_window,
"timestamp": timestamp
}
else:
params = {
"api_key": api_key,
"recv_window": recv_window,
"timestamp": timestamp
}
# Convert params to query string for signature
param_str = "&".join([f"{k}={v}" for k, v in sorted(params.items()) if k != "api_key"])
# Sign the request
signature = sign_bybit_request(secret_key, param_str)
params["sign"] = signature
# Make the request
url = f"{base_url}{endpoint}"
headers = {"Content-Type": "application/json"}
print(f"URL: {url}")
print(f"Params: {json.dumps(params, indent=2)}")
response = requests.post(url, json=params, headers=headers, timeout=30)
print(f"Status Code: {response.status_code}")
print(f"Response: {response.text[:500]}...")
if response.status_code == 200:
data = response.json()
if data.get("retCode") == 0:
print("✅ SUCCESS!")
else:
print(f"❌ API Error: {data.get('retMsg')}")
else:
print(f"❌ HTTP Error: {response.status_code}")
except Exception as e:
print(f"❌ Exception: {str(e)}")
def test_public_endpoints():
"""Test public endpoints to verify API connectivity"""
print("\n=== Testing Public Endpoints ===")
try:
# Test public ticker endpoint
url = "https://api.bybit.com/v5/market/tickers?category=linear&symbol=HYPEUSDT"
response = requests.get(url, timeout=10)
print(f"Public ticker status: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"Public ticker result: {data.get('retCode')}")
else:
print(f"Public ticker error: {response.text}")
except Exception as e:
print(f"Public endpoint error: {str(e)}")
if __name__ == "__main__":
test_public_endpoints()
test_bybit_endpoints()