← All articles

Binance API -1021 “Timestamp for this request is outside of the recvWindow” in Node.js

If a signed Binance request fails with one of these, the problem is time, not your keys:

{ "code": -1021, "msg": "Timestamp for this request is outside of the recvWindow." }
{ "code": -1021, "msg": "Timestamp for this request was 1000ms ahead of the server's time." }

Binance checks every signed request against its own clock. The request carries a timestamp (your machine’s time in milliseconds) and a recvWindow (how long the request stays valid). If your timestamp is more than about a second ahead of Binance, or older than recvWindow by the time it arrives, the request is rejected with -1021. Public endpoints never hit this, which is why market data works while account calls fail.

This guide uses the Binance JavaScript SDK (binance on npm), which we maintain. It adds timestamp and recvWindow and signs each private request for you, so the fixes below are mostly options rather than code.

1. Check your clock first

Most -1021 errors come from a drifting system clock: a laptop after sleep, a container on a host with no NTP, or a cheap VPS. Measure the drift before changing anything:

import { MainClient } from 'binance';

const client = new MainClient();

async function checkDrift() {
  const before = Date.now();
  const serverTime = await client.getServerTime();
  const after = Date.now();

  const latency = (after - before) / 2;
  const drift = serverTime - (after - latency);

  console.log({ serverTime, localTime: after, latencyMs: latency, driftMs: drift });
}

checkDrift().catch(console.error);

A drift of a few milliseconds is normal. Hundreds of milliseconds or more means the machine’s clock needs fixing. On Linux, timedatectl status should show System clock synchronized: yes; if not, enable NTP (sudo timedatectl set-ntp true) or install chrony. In Docker, the container uses the host clock, so fix it on the host.

Fixing the clock is the real cure. The SDK options below help when you can’t control the machine.

2. Let the SDK sync with Binance’s server time

Time sync is off by default (disableTimeSync: true). Turn it on and the client measures the offset to Binance’s server time on start-up, then again every syncIntervalMs (default one hour), and adds that offset to every signed request:

import { MainClient } from 'binance';

const client = new MainClient({
  api_key: process.env.BINANCE_API_KEY,
  api_secret: process.env.BINANCE_API_SECRET,
  disableTimeSync: false,
  syncIntervalMs: 15 * 60 * 1000, // resync every 15 minutes
});

const account = await client.getAccountInformation();
console.log(account.balances.filter((b) => Number(b.free) > 0));

The same options work on USDMClient and CoinMClient for futures:

import { USDMClient } from 'binance';

const futures = new USDMClient({
  api_key: process.env.BINANCE_API_KEY,
  api_secret: process.env.BINANCE_API_SECRET,
  disableTimeSync: false,
});

console.log(await futures.getBalanceV3());

If you want control over when the sync happens (for example once before a batch of orders), read and set the offset yourself:

const offset = await client.fetchTimeOffset();
client.setTimeOffset(offset);
console.log('Offset applied (ms):', client.getTimeOffset());

3. Give slow networks a wider recvWindow

recvWindow defaults to 5000 ms in the SDK. If the clock is fine but requests sometimes take a long time to reach Binance (busy event loop, distant region, proxy), widen it a little:

const client = new MainClient({
  api_key: process.env.BINANCE_API_KEY,
  api_secret: process.env.BINANCE_API_SECRET,
  recvWindow: 10000,
});

Binance caps recvWindow at 60000 ms, but a huge window only hides the problem and lets a delayed order still execute much later than you meant. For order placement, keep it small and fix the clock instead.

Note that recvWindow only helps with requests that arrive late. If your clock runs ahead of Binance, the “ahead of the server’s time” message will keep coming back whatever the window is. That one needs the clock fix or time sync.

4. Catch -1021 and retry once

The SDK throws a parsed error with Binance’s code and message, so you can spot -1021, resync and retry a single time:

import { MainClient } from 'binance';

const client = new MainClient({
  api_key: process.env.BINANCE_API_KEY,
  api_secret: process.env.BINANCE_API_SECRET,
});

async function withTimeRetry<T>(call: () => Promise<T>): Promise<T> {
  try {
    return await call();
  } catch (e: any) {
    if (e?.code !== -1021) throw e;

    console.warn('Binance -1021, resyncing time:', e.message);
    client.setTimeOffset(await client.fetchTimeOffset());
    return call();
  }
}

const openOrders = await withTimeRetry(() =>
  client.getOpenOrders({ symbol: 'BTCUSDT' }),
);
console.log(openOrders);

Retry reads freely, but think before retrying order placement blindly: if the first attempt did reach Binance, a retry could duplicate it. Set your own newClientOrderId so a duplicate is rejected rather than filled twice.

5. WebSocket API sessions

The same -1021 code can come back from the Binance WebSocket API when you place orders over a WebSocket connection. The cause is identical: the machine’s clock. Fix it as in step 1; the WebsocketAPIClient in the same package signs those requests with your local time.

Quick checklist

Symptom Likely cause Fix
“ahead of the server’s time” Local clock fast NTP/chrony, or disableTimeSync: false
“outside of the recvWindow” every time Local clock slow NTP/chrony, or disableTimeSync: false
“outside of the recvWindow” now and then Slow delivery, blocked event loop Slightly larger recvWindow, fix the latency
Only after a laptop wakes from sleep Clock jumped Resync with fetchTimeOffset() and setTimeOffset()

Further reading