connect to binance testnet by nodejs @binance/connector library

I’m trying to connect to testnet by the node.js library (Binance API Documentation) and didn’t find any document about how to do it!

what I tried:

const { Spot } = require('@binance/connector')
const apiKey = 'my api key'
const apiSecret = 'my api secret'
const client = new Spot(apiKey, apiSecret, { baseURL: 'https://testnet.binance.vision', timeout: 1000})

client.account({ recvWindow: 2000 }).then(response => client.logger.log(response.data)).catch(error => client.logger.error(error))

response:

[AxiosError: Request failed with status code 401] {
  code: 'ERR_BAD_REQUEST',
  config: {
    transitional: {
      silentJSONParsing: true,
      forcedJSONParsing: true,
      clarifyTimeoutError: false
    },
    adapter: [Function: httpAdapter],
    transformRequest: [ [Function: transformRequest] ],
    transformResponse: [ [Function: transformResponse] ],
    timeout: 1000,
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    maxBodyLength: -1,
    env: { FormData: [Function] },
    validateStatus: [Function: validateStatus],
    headers: {
      Accept: 'application/json, text/plain, */*',
      'Content-Type': 'application/json',
      'X-MBX-APIKEY': 'myapi',
      'User-Agent': '@binance/connector-node/2.0.0-rc.2'
    },
    baseURL: 'https://testnet.binance.vision',
    proxy: false,
    method: 'get',
    url: '/api/v3/account?recvWindow=2000&timestamp=1665373925119&signature=f7c206f9560ffb19039f7f78387d4950ed8d9ef7e0d7a41ec719aa03b47dd4c4',
    data: undefined
  },
  request: <ref *1> ClientRequest {
    _events: [Object: null prototype] {
      abort: [Function (anonymous)],
      aborted: [Function (anonymous)],
      connect: [Function (anonymous)],
      error: [Function (anonymous)],
      socket: [Function (anonymous)],
      timeout: [Function (anonymous)],
      prefinish: [Function: requestOnPrefinish]
    },
    _eventsCount: 7,
    _maxListeners: undefined,
    outputData: [],
    outputSize: 0,
    writable: true,
    destroyed: false,
    _last: true,
    chunkedEncoding: false,
    shouldKeepAlive: false,
    _defaultKeepAlive: true,
    useChunkedEncodingByDefault: false,
    sendDate: false,
    _removedConnection: false,
    _removedContLen: false,
    _removedTE: false,
    _contentLength: 0,
    _hasBody: true,
    _trailer: '',
    finished: true,
    _headerSent: true,
    socket: TLSSocket {
      _tlsOptions: [Object],
      _secureEstablished: true,
      _securePending: false,
      _newSessionPending: false,
      _controlReleased: true,
      secureConnecting: false,
      _SNICallback: null,
      servername: 'testnet.binance.vision',
      alpnProtocol: false,
      authorized: true,
      authorizationError: null,
      encrypted: true,
      _events: [Object: null prototype],
      _eventsCount: 10,
      connecting: false,
      _hadError: false,
      _parent: null,
      _host: 'testnet.binance.vision',
      _readableState: [ReadableState],
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: false,
      _sockname: null,
      _pendingData: null,
      _pendingEncoding: '',
      server: undefined,
      _server: null,
      ssl: [TLSWrap],
      _requestCert: true,
      _rejectUnauthorized: true,
      parser: null,
      _httpMessage: [Circular *1],
      timeout: 1000,
      [Symbol(res)]: [TLSWrap],
      [Symbol(verified)]: true,
      [Symbol(pendingSession)]: null,
      [Symbol(async_id_symbol)]: 5,
      [Symbol(kHandle)]: [TLSWrap],
      [Symbol(kSetNoDelay)]: false,
      [Symbol(lastWriteQueueSize)]: 0,
      [Symbol(timeout)]: Timeout {
        _idleTimeout: 1000,
        _idlePrev: [TimersList],
        _idleNext: [TimersList],
        _idleStart: 566,
        _onTimeout: [Function: bound ],
        _timerArgs: undefined,
        _repeat: null,
        _destroyed: false,
        [Symbol(refed)]: false,
        [Symbol(kHasPrimitive)]: false,
        [Symbol(asyncId)]: 14,
        [Symbol(triggerId)]: 9
      },
      [Symbol(kBuffer)]: null,
      [Symbol(kBufferCb)]: null,
      [Symbol(kBufferGen)]: null,
      [Symbol(kCapture)]: false,
      [Symbol(kBytesRead)]: 0,
      [Symbol(kBytesWritten)]: 0,
      [Symbol(connect-options)]: [Object],
      [Symbol(RequestTimeout)]: undefined
    },
    _header: 'GET /api/v3/account?recvWindow=2000&timestamp=1665373925119&signature=f7c206f9560ffb19039f7f78387d4950ed8d9ef7e0d7a41ec719aa03b47dd4c4 HTTP/1.1\r\n' +
      'Accept: application/json, text/plain, */*\r\n' +
      'Content-Type: application/json\r\n' +
      'X-MBX-APIKEY: myapikey\r\n' +
      'User-Agent: @binance/connector-node/2.0.0-rc.2\r\n' +
      'Host: testnet.binance.vision\r\n' +
      'Connection: close\r\n' +
      '\r\n',
    _keepAliveTimeout: 0,
    _onPendingData: [Function: noopPendingOutput],
    agent: Agent {
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      defaultPort: 443,
      protocol: 'https:',
      options: [Object],
      requests: {},
      sockets: [Object],
      freeSockets: {},
      keepAliveMsecs: 1000,
      keepAlive: false,
      maxSockets: Infinity,
      maxFreeSockets: 256,
      scheduling: 'lifo',
      maxTotalSockets: Infinity,
      totalSocketCount: 1,
      maxCachedSessions: 100,
      _sessionCache: [Object],
      [Symbol(kCapture)]: false
    },
    socketPath: undefined,
    method: 'GET',
    maxHeaderSize: undefined,
    insecureHTTPParser: undefined,
    path: '/api/v3/account?recvWindow=2000&timestamp=1665373925119&signature=f7c206f9560ffb19039f7f78387d4950ed8d9ef7e0d7a41ec719aa03b47dd4c4',
    _ended: true,
    res: IncomingMessage {
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 4,
      _maxListeners: undefined,
      socket: [TLSSocket],
      httpVersionMajor: 1,
      httpVersionMinor: 1,
      httpVersion: '1.1',
      complete: true,
      headers: [Object],
      rawHeaders: [Array],
      trailers: {},
      rawTrailers: [],
      aborted: false,
      upgrade: false,
      url: '',
      method: null,
      statusCode: 401,
      statusMessage: 'Unauthorized',
      client: [TLSSocket],
      _consuming: false,
      _dumped: false,
      req: [Circular *1],
      responseUrl: 'https://testnet.binance.vision/api/v3/account?recvWindow=2000&timestamp=1665373925119&signature=f7c206f9560ffb19039f7f78387d4950ed8d9ef7e0d7a41ec719aa03b47dd4c4',
      redirects: [],
      [Symbol(kCapture)]: false,
      [Symbol(RequestTimeout)]: undefined
    },
    aborted: false,
    timeoutCb: null,
    upgradeOrConnect: false,
    parser: null,
    maxHeadersCount: null,
    reusedSocket: false,
    host: 'testnet.binance.vision',
    protocol: 'https:',
    _redirectable: Writable {
      _writableState: [WritableState],
      _events: [Object: null prototype],
      _eventsCount: 3,
      _maxListeners: undefined,
      _options: [Object],
      _ended: true,
      _ending: true,
      _redirectCount: 0,
      _redirects: [],
      _requestBodyLength: 0,
      _requestBodyBuffers: [],
      _onNativeResponse: [Function (anonymous)],
      _currentRequest: [Circular *1],
      _currentUrl: 'https://testnet.binance.vision/api/v3/account?recvWindow=2000&timestamp=1665373925119&signature=f7c206f9560ffb19039f7f78387d4950ed8d9ef7e0d7a41ec719aa03b47dd4c4',
      _timeout: null,
      [Symbol(kCapture)]: false
    },
    [Symbol(kCapture)]: false,
    [Symbol(kNeedDrain)]: false,
    [Symbol(corked)]: 0,
    [Symbol(kOutHeaders)]: [Object: null prototype] {
      accept: [Array],
      'content-type': [Array],
      'x-mbx-apikey': [Array],
      'user-agent': [Array],
      host: [Array]
    }
  },
  response: {
    status: 401,
    statusText: 'Unauthorized',
    headers: {
      'content-type': 'application/json;charset=UTF-8',
      'content-length': '70',
      connection: 'close',
      date: 'Mon, 10 Oct 2022 03:52:05 GMT',
      server: 'nginx',
      'x-mbx-uuid': '19d410de-4ddd-4fe7-95d9-5bc13979b8d1',
      'x-mbx-used-weight': '10',
      'x-mbx-used-weight-1m': '10',
      'strict-transport-security': 'max-age=31536000; includeSubdomains',
      'x-frame-options': 'SAMEORIGIN',
      'x-xss-protection': '1; mode=block',
      'x-content-type-options': 'nosniff',
      'content-security-policy': "default-src 'self'",
      'x-content-security-policy': "default-src 'self'",
      'x-webkit-csp': "default-src 'self'",
      'cache-control': 'no-cache, no-store, must-revalidate',
      pragma: 'no-cache',
      expires: '0',
      'x-cache': 'Error from cloudfront',
      via: '1.1 c6a5e9d6e582c5b5b5d4cc992d25047a.cloudfront.net (CloudFront)',
      'x-amz-cf-pop': 'SGN50-P1',
      'x-amz-cf-id': 'YlvPYnugDvYRILv_JE6sSCT3rWw-RgmmsiZM6LIDWJtNlzquM5n02g=='
    },
    config: {
      transitional: [Object],
      adapter: [Function: httpAdapter],
      transformRequest: [Array],
      transformResponse: [Array],
      timeout: 1000,
      xsrfCookieName: 'XSRF-TOKEN',
      xsrfHeaderName: 'X-XSRF-TOKEN',
      maxContentLength: -1,
      maxBodyLength: -1,
      env: [Object],
      validateStatus: [Function: validateStatus],
      headers: [Object],
      baseURL: 'https://testnet.binance.vision',
      proxy: false,
      method: 'get',
      url: '/api/v3/account?recvWindow=2000&timestamp=1665373925119&signature=f7c206f9560ffb19039f7f78387d4950ed8d9ef7e0d7a41ec719aa03b47dd4c4',
      data: undefined
    },
    request: <ref *1> ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 7,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      destroyed: false,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: false,
      _defaultKeepAlive: true,
      useChunkedEncodingByDefault: false,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      _contentLength: 0,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      socket: [TLSSocket],
      _header: 'GET /api/v3/account?recvWindow=2000&timestamp=1665373925119&signature=f7c206f9560ffb19039f7f78387d4950ed8d9ef7e0d7a41ec719aa03b47dd4c4 HTTP/1.1\r\n' +
        'Accept: application/json, text/plain, */*\r\n' +
        'Content-Type: application/json\r\n' +
        'X-MBX-APIKEY: myapikey\r\n' +
        'User-Agent: @binance/connector-node/2.0.0-rc.2\r\n' +
        'Host: testnet.binance.vision\r\n' +
        'Connection: close\r\n' +
        '\r\n',
      _keepAliveTimeout: 0,
      _onPendingData: [Function: noopPendingOutput],
      agent: [Agent],
      socketPath: undefined,
      method: 'GET',
      maxHeaderSize: undefined,
      insecureHTTPParser: undefined,
      path: '/api/v3/account?recvWindow=2000&timestamp=1665373925119&signature=f7c206f9560ffb19039f7f78387d4950ed8d9ef7e0d7a41ec719aa03b47dd4c4',
      _ended: true,
      res: [IncomingMessage],
      aborted: false,
      timeoutCb: null,
      upgradeOrConnect: false,
      parser: null,
      maxHeadersCount: null,
      reusedSocket: false,
      host: 'testnet.binance.vision',
      protocol: 'https:',
      _redirectable: [Writable],
      [Symbol(kCapture)]: false,
      [Symbol(kNeedDrain)]: false,
      [Symbol(corked)]: 0,
      [Symbol(kOutHeaders)]: [Object: null prototype]
    },
    data: {
      code: -2015,
      msg: 'Invalid API-key, IP, or permissions for action.'
    }
  }
}

p/s: It response 401 error, however I thought I already gave it my api key and my api secret so I have no idea about what is going wrong

Anyway, I have solve the problem myself and this is some tips for people who have the same problem

  1. The testnet API is different from mainnet API! to create the Testnet API, follow the document below:
    Cách thử nghiệm các chức năng trên Binance Testnet | Binance Support
  1. if you have the same problem on mainnet, check your API permission first, if your API didn’t have permission to do the thing you’re doing then it will response 401 error
  1. If the problem still not solve, it may because you set the wrong IP to the API.

Is the API key and secret coming from testnet?

@dino Does testnet have different APIkey and secret??! How could I get it

https://testnet.binance.vision