← All articles

Gate API INVALID_SIGNATURE, REQUEST_EXPIRED and INVALID_KEY: fixing signed requests in Node.js

When a signed Gate request fails, the response body tells you which check it failed. For a bad key it looks like this:

{ "label": "INVALID_KEY", "message": "Invalid key provided" }

Gate API v4 says you should branch on label, not on message, because the wording of the message can change. The label is the useful part. This post goes through the authentication labels you’re most likely to hit and what usually causes each one.

The examples use the Gate JavaScript SDK (gateio-api on npm), which we maintain. It builds the KEY, Timestamp and SIGN headers for you, so most of the fixes are about keys, clocks and IP settings rather than signing code.

How Gate checks a signed request

Every private request carries three headers:

  • KEY: your APIv4 key.
  • Timestamp: the current Unix time in seconds. Gate rejects it if it is more than 60 seconds away from its own clock.
  • SIGN: a hex HMAC-SHA512 of five lines joined with \n: the method in upper case, the path including /api/v4, the raw query string, the SHA512 hex of the body (or of an empty string), and the same timestamp.

The request must also come from an IP on the key’s whitelist, if you set one. Each failed check has its own label.

The labels and what to check

Label Usual cause Check first
INVALID_KEY The key doesn’t exist, was deleted, has expired, or has a stray space Copy the key again from the APIv4 Keys page and trim it. Make sure it’s an APIv4 key, not an old v2 key.
INVALID_SIGNATURE The signature doesn’t match what Gate computed The secret (trimmed, complete), then anything that changes the request after signing: a proxy rewriting the path, a reordered query string, or a body serialised twice.
REQUEST_EXPIRED The Timestamp header is more than 60 seconds from Gate’s time Your system clock. If you sign by hand, check you’re sending seconds, not milliseconds.
MISSING_REQUIRED_HEADER One of the three headers was missing Usually hand-rolled signing, or a private call made without passing credentials.
IP_FORBIDDEN The request IP isn’t on the key’s whitelist The public IP your server actually uses. Cloud functions and containers often change it.
READ_ONLY A write call with a read-only key The key’s permissions for that product (spot, futures, wallet).
FORBIDDEN The account has no permission for that operation Key permissions, sub-account limits, and whether the product is open to your account.

Catch the label in Node.js

When a request fails, gateio-api throws an object with the HTTP status in code and Gate’s response in body, so the label is at e.body.label:

import { RestClient } from 'gateio-api';

const client = new RestClient({
  apiKey: process.env.GATE_API_KEY,
  apiSecret: process.env.GATE_API_SECRET,
});

try {
  const balances = await client.getSpotAccounts({ currency: 'USDT' });
  console.log(balances);
} catch (e) {
  const label = e?.body?.label;

  switch (label) {
    case 'INVALID_KEY':
    case 'INVALID_SIGNATURE':
      console.error('Check the key and secret in your env vars', label);
      break;
    case 'REQUEST_EXPIRED':
      console.error('Local clock is off. Sync it or use a time offset (below).');
      break;
    case 'IP_FORBIDDEN':
      console.error('Add this server IP to the key whitelist on Gate.');
      break;
    default:
      console.error('Gate error', e?.code, label, e?.body?.message);
  }
}

With a dummy key, this logs INVALID_KEY with HTTP status 401. That’s a handy check that your network path to Gate works before you put real keys in.

Fixing REQUEST_EXPIRED without touching the server clock

The proper fix is to keep the machine’s clock in sync with NTP. When you can’t (a locked-down host, or a laptop that drifts after sleep), measure the offset from Gate’s clock and sign with Gate’s time instead. The SDK reads the signing time from getSignTimestampMs(), which you can override in a subclass:

import { RestClient } from 'gateio-api';

// How far the local clock is from Gate's clock, in ms
async function measureOffsetMs() {
  const publicClient = new RestClient();
  const before = Date.now();
  const { server_time } = await publicClient.getServerTime();
  const after = Date.now();
  return server_time - Math.round((before + after) / 2);
}

// A RestClient that signs with Gate's time instead of the local clock
class SyncedRestClient extends RestClient {
  offsetMs = 0;

  getSignTimestampMs() {
    return Date.now() + this.offsetMs;
  }
}

const client = new SyncedRestClient({
  apiKey: process.env.GATE_API_KEY,
  apiSecret: process.env.GATE_API_SECRET,
});

client.offsetMs = await measureOffsetMs();
console.log('Clock offset (ms):', client.offsetMs);

getServerTime() is a public call, so it works without keys. For a long-running process, measure the offset again every so often, for example every 30 minutes, because clocks keep drifting.

One thing that won’t help here: raising recvWindow. Gate’s window is fixed at 60 seconds on the server side, so the only fix is a timestamp that’s close to Gate’s.

If INVALID_SIGNATURE only happens on some calls

If getSpotAccounts() works but another call fails with INVALID_SIGNATURE, the key and secret are fine. Something about that request is changing between signing and sending. Common causes:

  • A corporate or reverse proxy that rewrites the path or query string.
  • Calling Gate through a custom baseUrl that adds or drops the /api/v4 prefix.
  • Undefined values in the parameters. Set strictParamValidation: true in the client options to make the SDK throw on undefined values rather than send something unexpected.

Quick checklist

  1. Read e.body.label, not the message.
  2. INVALID_KEY: copy the APIv4 key again and trim it.
  3. INVALID_SIGNATURE: check the secret, then anything between your code and Gate.
  4. REQUEST_EXPIRED: sync the clock, or sign with a measured offset.
  5. IP_FORBIDDEN: whitelist the IP your server really uses.
  6. READ_ONLY or FORBIDDEN: check the key’s permissions for that product.

For more on clients, WebSockets and order placement, see the Gate JavaScript tutorial, the Siebly SDK docs and the gateio-api source on GitHub.