Margin account response using Matlab

Hi,

I am trying to connect to my margin account using Matlab, some requests are successful some aren’t.

Below the test code I used for the first test :

%%%%%%%API
timestamp=webread(‘https://api.binance.com/api/v1/time’);

timestamp=num2str(timestamp.serverTime);
[key,secret]=key_secret('binance');
string=['&timestamp=' timestamp];
Signature = char(crypto(string, secret, 'HmacSHA256'));
url_ext = [ string '&signature=' Signature];
url=['https://api.binance.com/api/v3/account?' url_ext ];


 options = weboptions('HeaderFields',{'X-MBX-APIKEY' key},'ArrayFormat','json');
 balances=webread(url,options)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Max transferable

 timestamp=webread('https://api.binance.com/api/v1/time');
 timestamp=num2str(timestamp.serverTime);
 [key,secret]=key_secret('binance');
string=['&timestamp=' timestamp];

Signature = char(crypto(string, secret, 'HmacSHA256'));
url_ext = [ '&asset=BNB' string '&signature=' Signature];
url=['https://api.binance.com/sapi/v1/margin/maxTransferable?' url_ext ];
  options = weboptions('HeaderFields',{'X-MBX-APIKEY' key},'ArrayFormat','json');
max=webread(url,options)

There is the response I obtain :

test_api

balances =

struct with fields:

 makerCommission: 10
 takerCommission: 10
 buyerCommission: 0
sellerCommission: 0
        canTrade: 1
     canWithdraw: 1
      canDeposit: 1
      updateTime: 1.5962e+12
     accountType: 'SPOT'
        balances: [254×1 struct]
     permissions: {2×1 cell}

The server returned the status 400 with message “” in response to the request to URL
https://api.binance.com/sapi/v1/margin/maxTransferable?&asset=BNB&timestamp=xxxx&signature=xxxx

The first part is OK but I cannot get a reply for the margin account (maybe the string sent is wrong because I am not sure about the parameter ‘asset’.

In the first response the accountType=Spot, is this the issue? How to change that?

Regards.

I’m not familiar with Matlab, but have a try to update the url to:

https://api.binance.com/sapi/v1/margin/maxTransferable?asset=BNB&timestamp=xxxx&signature=xxxx

No & after ? mark.

Thanks for your help, I got the same answer :

Error using matlab.internal.webservices.HTTPConnector/copyContentToByteArray (line 396)
The server returned the status 400 with message “” in response to the request to URL
https://api.binance.com/sapi/v1/margin/maxTransferable?asset=BNB&timestamp=1596382703395&signature=xxxxxx

GET /sapi/v1/margin/maxTransferable?asset=BNB&timestamp=1596414559742&signature=XXXX

this url works fine for me, I can’t see any issue with yours.

  1. https://github.com/binance-exchange/binance-api-postman this postman collection has this endpoint working, maybe helpful to find the potential issue from API key setting.

  2. https://github.com/binance-exchange/binance-signature-examples/blob/master/python/spot.py this open source script is also helpful if you wish to see how signature works in Python.

thank you