tiagosiebler / bybit-api

Node.js SDK for the Bybit APIs and WebSockets, with TypeScript & browser support.
https://www.npmjs.com/package/bybit-api
MIT License
244 stars 80 forks source link

Add support for account asset v3 #219

Closed manzgoeggel closed 1 year ago

manzgoeggel commented 1 year ago

when trying to create api creds for a sub account uid, the API returns this very undescriptive error "body not json". I tried a bunch of other v3 endpoints, which work seamlessly. (link to the endpoint)

before anyone is asking; masterClient just happens to be an instance of the extended BaseRestClient.

const { uid } = subAccounts.result.subMembers[0];

// create a new api keypair for an existing subaccount
const result = await masterClient.postPrivate("/user/v3/private/create-sub-api", {
    subuid: uid,
    readOnly: 0,
    permissions: {
        ContractTrade: ["Order", "Position"],
        Spot: ["SpotTrade"],
        Wallet: ["AccountTransfer", "SubMemberTransfer"],
    },
});
console.log(`apikey for subUID ${uid}: `, result);

which then returns this: { retCode: 10001, retMsg: 'body not json', result: {}, retExtInfo: {}, time: 1673284599016 }

manzgoeggel commented 1 year ago

ok, found the issue myself. ended up going with a a vanilla HTTP request:

import axios from "axios";
import crypto from "crypto";
import * as dotenv from "dotenv";
dotenv.config();
const url = "https://api-testnet.bybit.com";

const apiKey = process.env.BYBIT_API_KEY_TESTNET;
const secret = process.env.BYBIT_API_SECRET_TESTNET;
const recvWindow = 5000;
const timestamp = Date.now().toString();

function getSignature(parameters, secret) {
    return crypto
        .createHmac("sha256", secret)
        .update(timestamp + apiKey + recvWindow + parameters)
        .digest("hex");
}

async function http_request(endpoint, method, data, Info) {
    const sign = getSignature(data, secret);
    let fullendpoint = "";
    if (method == "POST") {
        fullendpoint = url + endpoint;
    } else {
        fullendpoint = url + endpoint + "?" + data;
        data = "";
    }
    //endpoint=url+endpoint
    const config = {
        method: method,
        url: fullendpoint,
        headers: {
            "X-BAPI-SIGN-TYPE": "2",
            "X-BAPI-SIGN": sign,
            "X-BAPI-API-KEY": apiKey,
            "X-BAPI-TIMESTAMP": timestamp,
            "X-BAPI-RECV-WINDOW": "5000",
            "Content-Type": "application/json; charset=utf-8",
        },
        data: data,
    };

    const result = await axios(config);
    return result.data;
}

(async () => {
    try {
        const body = {
            subuid: "<YOUR_SUBACCOUNT_UID>",
            readOnly: 0,

            permissions: {
                ContractTrade: ["Order", "Position"],
                // Spot: ["SpotTrade"],
                Wallet: ["AccountTransfer"],
            },
        };
        const createAPIkey = await http_request("/user/v3/private/create-sub-api", "POST", JSON.stringify(body));
        console.log("created key", createAPIkey);
    } catch (err) {
        console.log(err);
    }
})();
tiagosiebler commented 1 year ago

Leaving this open, since I do want to add support for the new account asset v3 endpoints

tiagosiebler commented 1 year ago

@manzgoeggel the AccountAssetV3 REST API client is now included with this module as of version v3.4.0, published to npm a few minutes ago.