pixtron / bybit-api

An unofficial node.js lowlevel wrapper for the Bybit Cryptocurrency Derivative exchange API
MIT License
21 stars 16 forks source link

Error 401 on Webscocket connection (noob...) #11

Closed Woutvan closed 4 years ago

Woutvan commented 4 years ago

Hi,

I'm a beginner and just installed your package. My goal is to output each action that is happening on my Bybit account. So when a trade is placed and closed I want to see it in my code. Not sure if this is possible or not.

Now my first test is failing already. I'm trying this:

const {WebsocketClient} = require('@pxtrn/bybit-api');

const API_KEY = 'JIsxxxxxxxqfck';
const PRIVATE_KEY = '1DZ7DiBxxxxxxxxxviDqvZ0sdnLnsr';

const ws = new WebsocketClient({key: API_KEY, secret: PRIVATE_KEY});

My API keys are new and are having full access (so not a read only key). The output i get is:

[Arguments] {
  '0': 'Starting authenticated websocket client.',
  '1': { category: 'bybit-ws' }
}
[Arguments] {
  '0': 'Websocket error',
  '1': {
    category: 'bybit-ws',
    err: Error: Unexpected server response: 401
        at ClientRequest.<anonymous> (/home/xxx/node_modules/ws/lib/websocket.js:580:7)
        at ClientRequest.emit (events.js:310:20)
        at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:603:27)
        at HTTPParser.parserOnHeadersComplete (_http_common.js:119:17)
        at TLSSocket.socketOnData (_http_client.js:476:22)
        at TLSSocket.emit (events.js:310:20)
        at addChunk (_stream_readable.js:286:12)
        at readableAddChunk (_stream_readable.js:268:9)
        at TLSSocket.Readable.push (_stream_readable.js:209:10)
        at TLSWrap.onStreamRead (internal/stream_base_commons.js:186:23)
  }
}

Anyone that can give me a hint? Thanks !

pixtron commented 4 years ago

401 means your not authenticated. Check those two:

  1. Did you create the API key and secret for the testnet (https://testnet.bybit.com/) or the livenet (https://bybit.com/)? If you created them for the livenet, add livenet: true to the options: const ws = new WebsocketClient({key: API_KEY, secret: PRIVATE_KEY, livenet: true});

  2. Is your IP from which you try to access the websocket whitelisted for the key you use?