Access issue while callling api

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:

  1. Incorrect API credentials - Wrong API key or secret

  2. Signature generation issues - Most common cause

  3. Timestamp problems - Clock synchronization or format issues

  4. Missing or incorrect headers - Required headers not included

  5. 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:

  • Production: https://api.india.delta.exchange

  • Testnet: https://cdn-ind.testnet.deltaex.org

Debugging Steps:

  1. 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}’")

  1. Test with a GET request first (like fetching account info) to isolate if it’s a POST-specific issue

  2. Verify your API key permissions - ensure it has trading permissions enabled

  3. Check if your API key is for the correct environment (production vs testnet)