Binance API Setup

Hi, I’ve set up the binance API using their testnet in postman, and got the API working perfectly fine with test data. It has a pre-request script which I am having trouble replicating in bubble. I have attached the postman collection and screenshots of my setup in the bubble API. The API keys in these images are testnet data.

The pre-request script is
const ts = Date.now();

pm.environment.set(“timestamp”, ts);

let paramsObject = {};

const binance_api_secret = pm.environment.get(“binance-api-secret”);

const parameters = pm.request.url.query;

parameters.map((param) => {

if (param.key != 'signature' && 

    param.key != 'timestamp' && 

    !is_empty(param.value) &&

    !is_disabled(param.disabled)) {

        paramsObject[param.key] = param.value;

        //console.log(encodeURIComponent(param.value));

        //pm.environment.set(param.key, encodeURIComponent(param.value));

}

})

Object.assign(paramsObject, {‘timestamp’: ts});

if (binance_api_secret) {

const queryString = Object.keys(paramsObject).map((key) => {

    return `${key}=${paramsObject[key]}`;

}).join('&');

console.log(queryString);

const signature = CryptoJS.HmacSHA256(queryString, binance_api_secret).toString();

pm.environment.set("signature", signature);

}

function is_disabled(str) {

return str == true;

}

function is_empty(str) {

if (typeof str == 'undefined' ||

    !str || 

    str.length === 0 || 

    str === "" ||

    !/[^\s]/.test(str) ||

    /^\s*$/.test(str) ||

    str.replace(/\s/g,"") === "")

{

    return true;

}

else

{

    return false;

}

}

When I try run it currently it says the timestamp and signature fields are empty. I also have to put my binance secret key somewhere. Is there an easy way to run the pre-request script in bubble?

Appreciate any help
Thanks

According to your screenshot, the timestamp and signature fields are in fact, empty. If these fields are mandatory and cannot be empty as per the Binance API, you’ll have to initialize it with values :slightly_smiling_face:

yeah, those values are obtained from the pre request script I believe

Hey! Did you get this to work?

Hey @rikard ,

your best bet is run another call that doesnt require the timestamp and signature, and then manually enter the api response (which you generate in postman). This might sound confusing but I hope this screenshot helps:

(the api key is a testnet one).

when you leave the url as a parameter, then you can initialise the call, then change the url to one requiring timestamp and signature. Does this make sense?

I built a custom plugin to handle all my binance API calls which I am planning to release to the public later this week (has taken me forever to build). This handles the timestamp and signature fields so that the user doesn’t have to worry about it, also handles all the calls server-side to protect sensitive api keys being exposed in the browser.

What is your use case for the binance api?

1 Like

hey @agoo7714 hope all’s well.

I’m running into an issue which I think is identical to what you were going thru (not Binance). The API has some sophisticated signing requirements that need to be included in the header (akin to your “pre-request script”). So I’ve found myself stuck – no way to initialize the API Connector.

If we try to initialize this call, as-is and without the required data the API expects, the API Connector complains because of a 401 error message. If we use “Manually enter API response”, the call is initialized but nothing is returned when looking at the debug breakpoints. I suspect that’s because the response is a non-200 response, so Bubble sees that as an error, and omit any of the response data; but since this was a “manually entered API response” there’s no way to capture the header/error states.

In your solution, do you mean that you needed to manually come up with all of the API endpoint’s expected data so that you can initialize the endpoint in the API Connector?

Any help/insights would be very much appreciated.

TIA!
Andy

1 Like

Hey Andy,

Just for reference, which API are you trying to setup. If the API setup is difficult, it can be helpful to first get the API working in Postman. Because I don’t know the specifics of the API, these are some approaches I would use:

If the API does have difficult signature requirements (particularly a timestamp requirement as this can’t be replicated in bubble’s API connector to my knowledge), you will have to generate a response in Postman (A 200 response) and then copy this in as a “manually entered API response”. Once you have done this, when you call the API from a workflow, you will have run the pre-request script prior to calling the API. It may be easier to use the toolbox plugin and write custom javascript for the pre-request script, depending on the signature requirements (HMACSHA256 can be done in bubble).

Without knowing the exact API it’s a difficult to be specific but I hope this helps.

Thanks
Adam

Yes, very helpful thanks. Did essentially that also based on feedback from @eli – manually crafted a response json that includes both the response data and error headers/info. On totally unrelated note, but maybe you’ve encountered this in your Binance adventures: How do you add the “zero byte 0x00” to a text field? need to concatenate a string with a hex zero-byte character 0x00 as a delimiter between the data fields. So for instance, for a name it would be FIRST*LAST where * is replaced with this zero-byte character.

Thanks again!

@akalati I have not heard of the hex zero-byte before. Is the “zero byte 0x00” the same value every time, or does it take a string and then encrypt it?

nope just a plain old single byte that’s always the same. that darn zero byte which was a very easy fix after burning 2 days on it! Toolbox expression \0

1 Like