Hi Team I am using access issue Status Code: 401
Response Body: {“error”:“unauthorized”,“success”:false}
def place_order(side=“buy”, size=0.9):
endpoint = "/v2/orders"
url = BASE_URL + endpoint
payload = {
"product_id": PRODUCT_ID,
"side": side, # 'buy' or 'sell'
"size": size, # e.g., 0.9 ETH
"order_type": "market"
}
payload_str = json.dumps(payload, separators=(",", ":"))
headers = sign_request("POST", endpoint, payload_str)
response = requests.post(url, headers=headers, data=payload_str)
Common Causes of 401 Unauthorized Error:
-
Incorrect API credentials - Wrong API key or secret
-
Signature generation issues - Most common cause
-
Timestamp problems - Clock synchronization or format issues
-
Missing or incorrect headers - Required headers not included
-
Payload formatting - JSON formatting issues
Key Issues to Check:
1. Signature Generation
The signature must be generated using this exact format:
signature_data = method + timestamp + path + query_string + payload
For your POST request:
-
method = “POST”
-
timestamp = current Unix timestamp as string
-
path = “/v2/orders”
-
query_string = “” (empty for POST)
-
payload = your JSON payload string
2. Required Headers
Make sure your sign_request function returns these headers:
headers = {
‘api-key’: your_api_key,
‘timestamp’: timestamp,
‘signature’: signature,
‘User-Agent’: ‘rest-client’,
‘Content-Type’: ‘application/json’
}
3. Timestamp Synchronization
Ensure your system clock is synchronized. The timestamp should be current Unix time:
timestamp = str(int(time.time()))
4. Payload Formatting
Your JSON formatting looks correct with separators=(",", ":"), but make sure there are no extra spaces.
5. API Environment
Verify you’re using the correct base URL:
Debugging Steps:
- Print your signature components to verify they match the expected format:
print(f"Method: ‘{method}’")
print(f"Timestamp: ‘{timestamp}’")
print(f"Path: ‘{path}’")
print(f"Query string: ‘{query_string}’")
print(f"Payload: ‘{payload_str}’")
print(f"Signature data: ‘{signature_data}’")
-
Test with a GET request first (like fetching account info) to isolate if it’s a POST-specific issue
-
Verify your API key permissions - ensure it has trading permissions enabled
-
Check if your API key is for the correct environment (production vs testnet)