Skip to content

Conversation

@cullenwatson
Copy link

@cullenwatson cullenwatson commented Nov 20, 2025

Fixes "Unauthorized/Invalid api key" error


Note

Serialize the request body with compact JSON when building HMAC signatures to ensure cross-language consistency and prevent auth errors.

  • Signing:
    • Use json.dumps(..., separators=(",", ":")) to serialize body in build_hmac_signature, ensuring a canonical HMAC message across languages.

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

@cullenwatson cullenwatson requested a review from a team as a code owner November 20, 2025 12:11
@cullenwatson cullenwatson changed the title fix: hmac [fix] Unauthorized error Nov 20, 2025
@starling114
Copy link

@cullenwatson Thanks for this fix, I can confirm that it works. Post order doesn't throw unauthenticated errors anymore

@starling114
Copy link

@fednerpolymarket @JonathanAmenechi Could you please guys check this PR? It does fixes an issue with post_order throwing Unauthorized/Invalid api key

@felix5572
Copy link

same issue. @cullenwatson

and Remember to import json

@noahverner1995
Copy link

OH MY GOD THANK YOU SO MUCH FOR THIS WELL NEEDED PR @cullenwatson DEV.

Basically the Python client was doing

# from hmac.py file
if body:
    # old behavior
    message += str(body).replace("'", '"')

So the HMAC was computed over a non-canonical string representation of the body ("{'price': '0.3', 'size': 5}" style), which doesn’t match the canonical JSON used by the Go/TS servers. Result: the server thinks the signature is wrong and replies with:

{"error":"Unauthorized/Invalid api key"}

By changing hmac.py file with these few lines

import json
if body:
    message += json.dumps(body, separators=(",", ":"))

The server gets what it expects

I can confirmed it worked after modifying it. I'm no longer getting this API Exception when placing trades:

[order] PolyApiException: PolyApiException[status_code=401, error_message={'error': 'Unauthorized/Invalid api key'}]

@lightyLi
Copy link

Some issue, it works for me. Thanks

@fednerpolymarket
Copy link
Contributor

Going to test this today

@fednerpolymarket
Copy link
Contributor

Thanks we'll get this merged

@cullenwatson
Copy link
Author

i wouldnt merge yet actually, trying now and it breaks other orders. interesting..

@fednerpolymarket
Copy link
Contributor

i wouldnt merge yet actually, trying now and it breaks other orders. interesting..

Hey, it'd be great to know which ones broke for you. And alright I'll add some more unit testing to see if I can find your issue

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.

6 participants