Skip to content

Conversation

@rinadelph
Copy link

@rinadelph rinadelph commented Dec 2, 2025

Problem

The Python client was throwing PolyApiException[status_code=401, error_message={'error': 'Unauthorized/Invalid api key'}] when calling post_order(), even with valid API credentials.

Root Cause

The HMAC signature was computed using Python's str() representation of the body dictionary, which created non-canonical JSON that didn't match the canonical JSON used by Go/TypeScript servers, causing signature validation to fail.

Solution

Changed to use canonical JSON serialization with json.dumps(body, separators=(',', ':')) to ensure compact, canonical JSON that matches across all languages.

Changes

  • File: py_clob_client/signing/hmac.py
  • Added import json
  • Replaced str(body).replace("'", '"') with json.dumps(body, separators=(',', ':'))

Testing

✅ Successfully tested with signature_type=1 (Email/Magic wallet proxy)
✅ Order posting now works - placed and filled order successfully
✅ Order response: {'status': 'matched', 'success': True}

This is the same fix confirmed working by multiple users in related discussions.


Note

Switch HMAC body serialization to canonical JSON (json.dumps with compact separators) to ensure cross-language signature consistency.

  • Signing:
    • Update build_hmac_signature in py_clob_client/signing/hmac.py to serialize body with json.dumps(..., separators=(",", ":")) for canonical JSON.
    • Add import json and remove reliance on Python str() representation.

Written by Cursor Bugbot for commit 4e87b8a. This will update automatically on new commits. Configure here.

Fixes 'Unauthorized/Invalid api key' error when posting orders.

The issue was that the HMAC signature was computed using Python's
str() representation of the body dictionary (e.g., "{'price': '0.3'}")
with single quotes replaced by double quotes. This creates a non-canonical
JSON representation that doesn't match what the server expects.

Changed to use json.dumps(body, separators=(',', ':')) to ensure
canonical, compact JSON serialization that matches Go/TypeScript clients.

This ensures cross-language HMAC consistency and fixes the 401
authentication errors when calling post_order().

Tested and verified working with signature_type=1 (Email/Magic wallet proxy).
@rinadelph rinadelph requested a review from a team as a code owner December 2, 2025 11:29
@fednerpolymarket
Copy link
Contributor

@rinadelph a fix has been merged and version 0.30.0 has been created: #176

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants