tiagosiebler / binance

Node.js & JavaScript SDK for Binance REST APIs & WebSockets, with TypeScript & browser support, integration tests, beautification & more.
MIT License
730 stars 264 forks source link

How to connect to testnet? #209

Open skliarovartem opened 2 years ago

skliarovartem commented 2 years ago

How to connect to https://testnet.binance.vision/api instead of main net?

tiagosiebler commented 2 years ago

You can try passing this in the first parameter (REST client options) in any of the rest clients:

baseUrl: "https://testnet.binance.vision"
anymos commented 1 year ago

Hi there, any way to use it for the websocket also ?

Spot API URL Spot Test Network URL
https://api.binance.com/api https://testnet.binance.vision/api
wss://stream.binance.com:9443/ws wss://testnet.binance.vision/ws
wss://stream.binance.com:9443/stream wss://testnet.binance.vision/stream

Thank you in advance !

anymos commented 1 year ago

Actually it s very easy, by using the wsUrl in the options

let _opts:WSClientConfigurableOptions={
            // api_key: this.keys.API_Key,
            // api_secret: this.keys.Secret_Key,
            beautify: true,
            // Disable ping/pong ws heartbeat mechanism (not recommended)
            // disableHeartbeat: true
           wsUrl:'wss://testnet.binance.vision/ws',
           restOptions:{
                baseUrl:'https://testnet.binance.vision'
            }
        }

in case some one have the same question

guyhagemans commented 1 year ago

did anything change in the meantime by any chance?

I'm trying to connect to testnet via;

    const baseUrL = (testnet ? "https://testnet.binance.vision/api" : "https://api.binance.com/api");
    const client = new MainClient({
        api_key: apikey,
        api_secret: secret,
        baseUrl: baseUrL
    });

The response I'm getting is:

{ "body": "

404 Not found

", "headers": { "content-length": "48", "connection": "close", "date": "Thu, 17 Nov 2022 14:02:16 GMT", "server": "nginx", "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 6851e5f468b237438eae4078fbc9d3b8.cloudfront.net (CloudFront)", "x-amz-cf-pop": "AMS1-P2", "x-amz-cf-id": "af7sa1JRboWR1PINCs-DkUr3gHn63nw_Fa2IH9pDzHfvC0TESYflOQ==" }, "requestUrl": "https://testnet.binance.vision/api/sapi/v1/account/apiRestrictions?timestamp=1668693736113&recvWindow=5000&signature=aad906b3933767274ea689724d60c26bda90849101c979c3ef3cf0f8d6dd351f", "requestOptions": { "recvWindow": 5000, "syncIntervalMs": 3600000, "strictParamValidation": false, "baseUrl": "https://testnet.binance.vision/api" }

more or less the same reply without /api:

{ "body": "", "headers": { "content-type": "text/plain; charset=utf-8", "content-length": "0", "connection": "close", "date": "Thu, 17 Nov 2022 14:12:16 GMT", "server": "nginx", "x-content-type-options": "nosniff", "x-frame-options": "DENY", "x-xss-protection": "1; mode=block", "referrer-policy": "strict-origin-when-cross-origin", "cache-control": "no-cache, no-store, must-revalidate, no-cache, no-store, must-revalidate", "pragma": "no-cache, no-cache", "expires": "0", "content-security-policy": "style-src 'sha256-mcBXr4VjWlBf3SfyQ5+Wm92/O81FeZGc85fmmbhjj+k=' 'sha256-wSI3cAJwzKV+dPAgXMCTat869o5mA0HfdnKphAm7Gr8=' https://bin.bnbstatic.com; font-src https://bin.bnbstatic.com; img-src https://bin.bnbstatic.com; default-src 'self'", "strict-transport-security": "max-age=31536000; includeSubdomains", "x-cache": "Error from cloudfront", "via": "1.1 7b80fdb7de25e1eb41eb907750147f34.cloudfront.net (CloudFront)", "x-amz-cf-pop": "AMS1-P2", "x-amz-cf-id": "m5ucT6codf0PboVMStwuehuJvgdFcLDjyHVhr75uXuYwjYuSgltvSA==" }, "requestUrl": "https://testnet.binance.vision/sapi/v1/account/apiRestrictions?timestamp=1668694335777&recvWindow=5000&signature=cd79c6edf09708187c0212cb4dd7d5e1695fcc120e5518ba558cc0aff241d329", "requestOptions": { "recvWindow": 5000, "syncIntervalMs": 3600000, "strictParamValidation": false, "baseUrl": "https://testnet.binance.vision" } }

anymos commented 1 year ago

I believe it is still working

GeoffWilliams commented 1 year ago

I was getting 404s too but this was from using the wrong client for the operation - I was trying use the CoinMClient to get account information which won't work - you have to use MainClient(!) Here's a complete example that works at time of posting:

import {CoinMClient, MainClient, USDMClient} from "binance";
const testnet = true;
const baseUrl = (testnet ? "https://testnet.binance.vision" : "https://api.binance.com");
const client = new MainClient({
  api_key: process.env["BINANCE_API_KEY"],
  api_secret: process.env["BINANCE_API_SECRET"],
  baseUrl
});

// Get the account balance
client
    .getAccountInformation()
    .then((result) => {
        console.log('getSymbolOrderBookTicker result: ', result);

        console.log("time to die!");
        process.exit()
    })
    .catch((err) => {
        console.error('getSymbolOrderBookTicker error: ', err);
    });
Kos-M commented 3 months ago

Seems MainClient.getBalances() is using /sapi endpoints and when we are connecting to testnet , is not working..