Signature Error

What’s wrong in this code:

ws.on("open", () => {
  console.log(`Connection Opened`);

  const newOrderRespType = "ACK";
  const price = 23416.1;
  const quantity = 0.000847;
  const recvWindow = 1000;
  const side = "SELL";
  const symbol = "BTCUSDT";
  const timeInForce = "GTC";
  const type = "LIMIT";

  const query_string = `apiKey=${API_KEY}&newOrderRespType=${newOrderRespType}&price=${price}&quantity=${quantity}&recvWindow=${recvWindow}&side=${side}&symbol=${symbol}&timeInForce=${timeInForce}&timestamp=${Date.now()}&type=${type}`;
  const signature = crypto.createHmac("sha256", API_SECRET).update(query_string).digest("hex");

  ws.send(
    JSON.stringify({
      id: "56374a46-3061-486b-a311-99ee972eb648",
      method: "order.place",
      params: {
        symbol,
        side,
        type,
        timeInForce,
        quantity,
        price,
        newOrderRespType,
        recvWindow,
        timestamp: Date.now(),
        apiKey: API_KEY,
        signature: signature,
      },
    })
  );
});

I get the error below:

{
  id: '56374a46-3061-486b-a311-99ee972eb648',
  status: 400,
  error: { code: -1022, msg: 'Signature for this request is not valid.' },
  rateLimits: ....
}

We don’t usually do programming analysis in this forum (at least from the maintainers), but please have a look on the available connectors at https://binance-docs.github.io/apidocs/spot/en/#enabling-accounts → API Library section.

1 Like

Please check that price and quantity are formatted exactly the same in request and signature. It is recommended to use decimal strings, i.e.

const price = "23416.10";
const quantity = "0.00084700";

Also, you should call Date.now() only once, so that exactly the same timestamp is used in signature and request.

1 Like

Thanks, now it works, I created a timestamp variable and set to Date.now(). Then I used this variable 2 times in the code.