Getting this error: data: { code: -1022, msg: 'Signature for this request is not valid.' }

Hi I am trying to get a list of orders by hitting this endpoint: https://api.binance.us/api/v3/allOrders

I keep getting the error message that the signature for the request is not valid. Do you know what I am doing wrong? I have included my node js code below. Thank you!

router.get("/transactions", async (req, res) => {
  const symbol = "LTCBTC";

  const apiSecret = process.env.BINANCE_SECRET_KEY;
  const query_string = `timestamp=${new Date().getTime()}`;

  const signature = crypto
    .createHmac("sha256", apiSecret)
    .update(query_string)
    .digest("hex");

  var config = {
    method: "get",
    url: `https://api.binance.us/api/v3/allOrders?symbol=${symbol}&recvWindow=5000&timestamp=${new Date().getTime()}&signature=${signature}`,
    headers: {
      "Content-Type": "application/json",
      "X-MBX-APIKEY": process.env.BINANCE_API_KEY,
    },
  };

  let result = await axios(config)
    .then((res) => {
      console.log(res.data);
      return res.data;
    })
    .catch((err) => console.log(err));

  res.send({ result });
});

you should give the same timestamp in query string and url.

The query string that you use in the API request (without the signature query parameter) must be same as that which you use to calculate the signature, including the timestamp. You should calculate the timestamp only once and re-use it in the query string in both the signature calculation and the request.

router.get("/transactions", async (req, res) => {
  const symbol = "LTCBTC";

  const apiSecret = process.env.BINANCE_SECRET_KEY;
  const timestamp = new Date().getTime();
  const query_string = `symbol=${symbol}&recvWindow=5000&timestamp=${timestamp}`;

  const signature = crypto
    .createHmac("sha256", apiSecret)
    .update(query_string)
    .digest("hex");

  var config = {
    method: "get",
    url: `https://api.binance.us/api/v3/allOrders?${queryString}&signature=${signature}`,
    headers: {
      "Content-Type": "application/json",
      "X-MBX-APIKEY": process.env.BINANCE_API_KEY,
    },
  };

  let result = await axios(config)
    .then((res) => {
      console.log(res.data);
      return res.data;
    })
    .catch((err) => console.log(err));

  res.send({ result });
});