How to use testnet API

Hi,

I successfully get the balance of my real binance account with my API Key, but I would like to use a test account to make fake order. I found this post :

I get the API key for the SPOT Testnet application, but when I try to use it in my web application, I have an error which say that API key are not correct.

Could you help me to use this test account ?

Thanks for your help.

Do you use API BASE URL like this: “https://testnet.binance.vision/api” ?

I am using this API : https://www.npmjs.com/package/node-binance-api.

Where should I use this API BASE URL ?

Thanks

The npm package you use does not support spot testnet. Only futures testnet is supported there. https://github.com/jaggedsoft/node-binance-api/blob/master/node-binance-api.js#L28

You may have to write your own API call to the testnet.

Thanks, and can I use the futures testnet API key to make orders ? Because I try to use the API key from the futures testnet and it doesn’t seem to work either…

I don’t think you did it the right way. How did you initialize an instance? More details and what error did you get?

I have this error :
code: -2015
msg: “Invalid API-key, IP, or permissions for action, request ip: xx.xx.xx.xx”

But I can’t find where we can choose the IP parameters

I have this error :
code: -2015
msg: “Invalid API-key, IP, or permissions for action, request ip: xx.xx.xx.xx”

But I can’t find where we can choose the IP parameters

@RD2 We have an article about that error message: why do I see this error "Invalid API-key, IP, or permissions for action."

“But I can’t find where we can choose the IP parameters” -

  1. You’re still pointing to fapi.binance.com using testnet key/secret
  2. You’re using prod key/secret to query testnet.binancefuture.com

Let me ask you again - How did you initialize your instance?

Here is my code :

import Binance from "node-binance-api";

const binance = new Binance().options({
  APIKEY: functions.config()?.binance?.apikey,
  APISECRET: functions.config()?.binance?.apisecret,
  useServerTime: true,
});

export const futuresExchangeInfo = functions.https.onCall(async () => {
  return binance.futuresExchangeInfo();
});

export const getBalances = functions.https.onCall(async (data) => {
  return getBalance();
});

function getBalance() {
  return new Promise((resolve, reject) => {
    binance.balance((error, balances) => {
      if (error) {
        console.log(error);
        return reject(error);
      }
      resolve(balances);
    });
  });
}

export const prices = functions.https.onCall(async (data) => {
  return binance.prices();
});

export const futuresAllOrders = functions.https.onCall(async (data) => {
  return binance.futuresAllOrders();
});

API key and secret are is a separate file.

How could you expect to connect to testnet when you didn’t explicitly say you want to connect to it?

Use this:

const binance = new Binance().options({
  APIKEY: functions.config()?.binance?.apikey,
  APISECRET: functions.config()?.binance?.apisecret,
  useServerTime: true,
  test:true
});

I’ve had the same problem. After some reverse engineering of the node-binance-api package, I found that it is not enough to specify “test: true” in the connection options.
On the contrary, you must also specify the testnet endpoint: because the package does not have it in its programming.

Here is a portion of my code, working

// my   ./config/binance_apikeys
module.exports = { 
  api_key: 'your-test-or-live-api-key',
  api_secret: 'your-test-or-live-api-secret',
  test: true          // true to point testnet API
}


// my Testnet or Live Connection
const apiKeys = require('./config/binance_apikeys')
const client = new Binance({
  APIKEY: apiKeys.api_key,
  APISECRET: apiKeys.api_secret,
  test: apiKeys.test,
  urls: {
    base: apiKeys.test ? 'https://testnet.binance.vision/api/' : undefined    // testnet endpoint or default
  }
})

Thanks buddy
It’s working perfectly.
you can only pass test url(not test:true) for test real fake order, it will output a placed order details,
with (test:true) you’ll only get black order object.