Signature for this request is not valid. In /fapi/v1/batchOrders

I am creating batch Orders in the following way : and I am getting Signature for this request is not valid , any Idea what is going wrong? Thank you


const axios = require('axios')
const crypto = require('crypto')

const apiKey = ''
const apiSecret = ''

const baseUrl = 'https://testnet.binancefuture.com'
const endpoint = '/fapi/v1/batchOrders'

const orders = [
    {
        symbol: 'BTCUSDT',
        side: 'BUY',
        type: 'MARKET',
        quantity: '0.01'
    },
    {
        symbol: 'ETHUSDT',
        side: 'SELL',
        type: 'STOP_MARKET',
        quantity: '0.1',
        stopPrice: '4000'
    }
]

const timestamp = Date.now()
const recvWindow = 5000

const query = `batchOrders=${encodeURIComponent(JSON.stringify(orders))}&timestamp=${timestamp}&recvWindow=${recvWindow}`
const signature = crypto.createHmac('sha256', apiSecret).update(query).digest('hex')

console.log(signature);

const headers = {
    'X-MBX-APIKEY': apiKey,
    'Content-Type': 'application/json',
    "Accept-Encoding":"*"
}


console.log("URL")
console.log(baseUrl + endpoint + '?' + query + '&signature=' + signature);


axios.post(baseUrl + endpoint + '?' + query + '&signature=' + signature, {}, { headers: headers })
    .then(res => {
        console.log(res.data)
    })
    .catch(err => {
        console.error(err.response.data)
    })


Hi,

Please try the following code instead, it should fix your issue:

const axios = require('axios');
const crypto = require('crypto');

// Binance API credentials
const apiKey = 'apikey';
const apiSecret = 'secret';

// Binance API endpoint for placing batch orders
const apiUrl = 'https://testnet.binancefuture.com/fapi/v1/batchOrders';

// Batch order information
const batchOrders = [
  {
    symbol: 'BTCUSDT',
    side: 'BUY',
    type: 'MARKET',
    quantity: '0.01',
  },
  {
    symbol: 'ETHUSDT',
    side: 'SELL',
    type: 'STOP_MARKET',
    quantity: '0.1',
    stopPrice: '4000',
  },
];

// Function to create a Binance API signature
function createSignature(params) {
  return crypto
    .createHmac('sha256', apiSecret)
    .update(params)
    .digest('hex');
}

// Function to place batch orders
async function placeBatchOrders() {
  const timestamp = Date.now();

  // Create the batch order request data without timestamp
  const batchOrderRequest = batchOrders.map((order) => ({
    symbol: order.symbol,
    side: order.side,
    type: order.type,
    quantity: order.quantity,
    // Include stop price if it's a STOP_MARKET order
    ...(order.type === 'STOP_MARKET' && { stopPrice: order.stopPrice }),
  }));

  // Create the query string with timestamp as a separate parameter
  const queryString = new URLSearchParams({ batchOrders: JSON.stringify(batchOrderRequest) }).toString();
  const queryStringWithTimestamp = `${queryString}&timestamp=${timestamp}`;

  // Create the signature using the updated queryStringWithTimestamp
  const signature = createSignature(queryStringWithTimestamp);

  // Include the signature in the request headers
  const headers = {
    'X-MBX-APIKEY': apiKey,
  };

  // Make the API request to place the batch orders with timestamp included in the URL
  try {
    const response = await axios.post(`${apiUrl}?${queryStringWithTimestamp}&signature=${signature}`, null, {
      headers: headers,
    });

    console.log(`Placed batch orders: ${JSON.stringify(response.data)}`);
  } catch (error) {
    console.error(`Error placing batch orders: ${error.response.data}`);
  }
}

placeBatchOrders();
1 Like

Thank you very much!!

Since you’re using node.js, you can also save yourself the pain of getting the signature process working for every type of request that binance supports by using one of the open source SDKs found on github.

Here’s an example of using the batch orders endpoint with the USDM futures REST client: