polymarket-cpp-client icon indicating copy to clipboard operation
polymarket-cpp-client copied to clipboard

401 Unauthorized/Invalid api key

Open dato0011 opened this issue 10 months ago • 2 comments

I'm having trouble using the library. I have provided the correct private_key and credentials (I know because those same keys work fine in another application). But when I call get_balance_allowance(), it fails. I debugged and noticed the server returns 401 "error" : "Unauthorized/Invalid api key". I'm sure I'm missing something obvious, but can't seem to figure this out on my own. Any help would be greatly appreciated. Thanks.

    polymarket::ClobClient client("https://clob.polymarket.com", 137, private_key, creds,
                                  polymarket::SignatureType::POLY_PROXY);

    auto balance = client.get_balance_allowance();

Here's how I populate credential vars:

    std::string private_key = "{PRIVATE_KEY}";
    polymarket::ApiCredentials creds{
        "{API_KEY}",
        "{API_SECRET}",
        "{API_PASSPHRASE}"
    };

dato0011 avatar Jan 20 '26 15:01 dato0011

It took me hours, but I found the bug.

  1. The parameters of "/balance-allowance" should not be included when computing hash. Only the path ""/balance-allowance"
  2. "/balance-allowance" API requires signature_type parameter which is not provided.

Here is the fix (tested locally and works). Can't push to your repository to create a pull request, probably permissions are not configured as such.

    std::optional<BalanceAllowance> ClobClient::get_balance_allowance(const std::string &asset_type)
    {
        std::string base_path = "/balance-allowance";
        std::string sig_type = std::to_string(static_cast<int>(sig_type_));
        std::string path_with_params = base_path + "?asset_type=COLLATERAL&signature_type=" + sig_type;
        auto headers = get_l2_headers("GET", base_path, "");
        auto response = http_.get(path_with_params, headers);

        if (!response.ok())
            return std::nullopt;
...

dato0011 avatar Jan 20 '26 18:01 dato0011

Thanks for the detailed report and the tested repro notes. You were right: the client was signing the query-string form of /balance-allowance and it was also missing signature_type on the balance/allowance request.

Fixed in 80f87f9 by:

  • signing the bare /balance-allowance request path for L2 auth
  • appending asset_type and signature_type only to the actual GET URL
  • switching update_balance_allowance to the upstream endpoint shape: GET /balance-allowance/update?...
  • defaulting the asset type to COLLATERAL

I verified the C++ build and local test suite in a clean CMake build.

SebastianBoehler avatar May 08 '26 19:05 SebastianBoehler