Bitfinex API for Google Apps Script

Gists

This sample script is converted this sample script (javascript) to Google Apps Script. The point for converting is signature as shown in the following sample script.

In order to achieve above, I made the method of bytesToHex().

function bytesToHex(data) {
    return data.map(function(e) {
        var v = (e < 0 ? e + 256 : e).toString(16);
        return v.length == 1 ? "0" + v : v;
    }).join("");
}

function main() {
    const apiKey = '<Your API key here>';
    const apiSecret = '<Your API secret here>';

    const apiPath = "v2/auth/r/alerts";
    const nonce = Date.now().toString();
    const body = { "type": "price" };
    const rawBody = JSON.stringify(body);
    var signature = "/api/" + apiPath + nonce + rawBody;
    signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_384, signature, apiSecret);
    signature = bytesToHex(signature);
    const url = "https://api.bitfinex.com/" + apiPath;
    const options = {
        method: 'POST',
        contentType: "application/json",
        headers: {
            'bfx-nonce': nonce,
            'bfx-apikey': apiKey,
            'bfx-signature': signature
        },
        payload: rawBody
    };
    var res = UrlFetchApp.fetch(url, options);
    Logger.log(res.getContentText())
}

 Share!