REST API कमांड लाइन क्लाइंट (NodeJS)

अंतिम अपडेट: 31 दिस॰ 2025

NodeJS में REST API कमांड लाइन क्लाइंट कमांड लाइन (जैसे macOS पर टर्मिनल) के माध्यम से Kraken REST API तक पूर्ण पहुँच प्रदान करता है, इसलिए किसी प्रोग्रामिंग ज्ञान या API अनुभव की आवश्यकता नहीं है।

सभी REST API सुविधाएँ सुलभ हैं, जिनमें शामिल हैं:

  • सार्वजनिक बाज़ार डेटा एंडपॉइंट

  • निजी खाता प्रबंधन एंडपॉइंट्स

  • ट्रेडिंग एंडपॉइंट्स

  • निजी फंडिंग एंडपॉइंट

  • निजी स्टेकिंग एंडपॉइंट्स*

कमांड लाइन क्लाइंट का उपयोग एक स्टैंडअलोन API क्लाइंट के रूप में किया जा सकता है, या इसे अन्य प्रोग्रामों (जैसे Bash स्क्रिप्ट जैसी अन्य इंटरप्रेटेड भाषाओं, या C/C++ जैसी कंपाइल की गई भाषाओं) से कॉल किया जा सकता है।

इंस्टॉलेशन

1. NodeJS इंस्टॉल करें (यदि आवश्यक हो)।

  • macOS और Linux में शायद पहले से ही NodeJS इंस्टॉल है।

  • Windows में शायद NodeJS इंस्टॉल नहीं है, लेकिन इसे https://www.nodejs.org/ से इंस्टॉल किया जा सकता है।

2. krakenapi.js फ़ाइल को अपनी पसंद के फ़ोल्डर (डायरेक्टरी) में अपने कंप्यूटर पर डाउनलोड और सहेजें। उदाहरण के लिए:

Macintosh HD > Users > Satoshi > KrakenAPI


3. एक कमांड प्रॉम्प्ट खोलें (जैसे macOS का टर्मिनल), और पिछले चरण में चुने गए फ़ोल्डर (डायरेक्टरी) पर नेविगेट करें

आप नेविगेट करने के लिए "cd" कमांड (डायरेक्टरी बदलें) का उपयोग कर सकते हैं। उदाहरण के लिए:

cd /Users/Satoshi/KrakenAPI


4. अपनी API कुंजियाँ जोड़ें उसी फ़ोल्डर में जहाँ आप krakenapi.js फ़ाइल रख रहे हैं।

अपने API सार्वजनिक कुंजी को खाता प्रबंधन से कॉपी/पेस्ट करके "API_PUBLIC_KEY" नामक एक सादे टेक्स्ट फ़ाइल में डालें।

अपने API निजी (गुप्त) कुंजी को कॉपी/पेस्ट करके "API_PRIVATE_KEY" नामक एक सादे टेक्स्ट फ़ाइल में डालें।

API कुंजी की आवश्यकता तभी होती है जब आप अपने Kraken खाते तक पहुँचने के लिए निजी API एंडपॉइंट्स का उपयोग करने की योजना बनाते हैं (जैसे बैलेंस पूछताछ, ऑर्डर देना/रद्द करना, खाता इतिहास निर्यात आदि)।

अतिरिक्त निर्देशों के लिए API कुंजी जोड़ी कैसे जनरेट करें? देखें।

उदाहरण कमांड

REST API कमांड लाइन क्लाइंट का उपयोग इस प्रकार है:

bash

Bash

nodejs krakenapi.js endpoint [parameters]

The command line client supports all of the REST API endpoints. Here are a few example commands:

nodejs krakenapi.js Time
nodejs krakenapi.js Ticker pair=xbtusd
nodejs krakenapi.js Trades pair=etheur since=1574067140000000000
nodejs krakenapi.js Balance
nodejs krakenapi.js TradeBalance asset=xbt
nodejs krakenapi.js QueryOrders txid=O7MN22-ZCX7J-TGLQHD
nodejs krakenapi.js AddOrder pair=xbtusd type=buy ordertype=limit price=6500 volume=0.002 leverage=5
nodejs krakenapi.js CancelOrder txid=O7MN22-ZCX7J-TGLQHD

NodeJS कोड

bash

Bash

const axios = require("axios");
const { clear } = require("console");
const crypto = require("crypto");
const fs = require("fs");

const main = async () => {
  let response = "";
  let apiMethod = "";
  let inputParameters = "";

  const api_private = [
    "Balance",
    "BalanceEx",
    "TradeBalance",
    "OpenOrders",
    "ClosedOrders",
    "QueryOrders",
    "TradesHistory",
    "QueryTrades",
    "OpenPositions",
    "Ledgers",
    "QueryLedgers",
    "TradeVolume",
    "AddExport",
    "ExportStatus",
    "RetrieveExport",
    "RemoveExport",
    "GetWebSocketsToken",
    "AddOrder",
    "AddOrderBatch",
    "EditOrder",
    "CancelOrder",
    "CancelAll",
    "CancelAllOrdersAfter",
    "DepositMethods",
    "DepositAddresses",
    "DepositStatus",
    "WithdrawInfo",
    "Withdraw",
    "WithdrawStatus",
    "WithdrawCancel",
    "WalletTransfer",
    "Staking/Assets",
    "Stake",
    "Unstake",
    "Staking/Pending",
    "Staking/Transactions",
  ];

  const api_public = [
    "Time",
    "Assets",
    "AssetPairs",
    "Ticker",
    "OHLC",
    "Depth",
    "Trades",
    "Spread",
    "SystemStatus",
  ];

  // Destructuring the input command
  if (process.argv.length < 3) {
    apiMethod = "Time";
  } else if (process.argv.length == 3) {
    apiMethod = process.argv[2];
  } else {
    apiMethod = process.argv[2];
    for (count = 3; count < process.argv.length; count++) {
      if (count == 3) {
        inputParameters = process.argv[count];
      } else {
        inputParameters = inputParameters + "&" + process.argv[count];
      }
    }
  }

  // Condition to check the private or public endpoints
  if (api_private.includes(apiMethod)) {
    response = await QueryPrivateEndpoint(apiMethod, inputParameters);

    if (apiMethod == "RetrieveExport") {
      try {
        fs.writeFileSync("Report.zip", response); // write the zip file response
        console.log("Report.zip file successfully received");
      } catch (err) {
        console.log(err);
      }
    } else {
      console.log(response.toString());
    }
  } else if (api_public.includes(apiMethod)) {
    response = await QueryPublicEndpoint(apiMethod, inputParameters);
    console.log(JSON.stringify(response));
  } else {
    console.log("Usage: app method [parameters]");
    console.log("Example: app OHLC pair=xbtusd interval=1440");
  }
};

// Public API Endpoint
async function QueryPublicEndpoint(endPointName, inputParameters) {
  let jsonData;
  const baseDomain = "https://api.kraken.com";
  const publicPath = "/0/public/";
  const apiEndpointFullURL = baseDomain + publicPath + endPointName + "?" + inputParameters;

  await axios
    .get(apiEndpointFullURL)
    .then((res) => {
      jsonData = res;
    })
    .catch((err) => {
      jsonData = err;
    });
  return jsonData.data;
}

// Private API Endpoint
async function QueryPrivateEndpoint(endPointName, inputParameters) {
  const baseDomain = "https://api.kraken.com";
  const privatePath = "/0/private/";

  const apiPublicKey = fs.readFileSync("API_PUBLIC_KEY").toString().trim(); // get data from API_PUBLIC_KEY text file
  const apiPrivateKey = fs.readFileSync("API_PRIVATE_KEY").toString().trim(); // get data from API_PRIVATE_KEY text file

  const apiEndpointFullURL = baseDomain + privatePath + endPointName;
  const nonce = Date.now().toString();
  const apiPostBodyData = "nonce=" + nonce + "&" + inputParameters;

  const signature = CreateAuthenticationSignature(
    apiPrivateKey,
    privatePath,
    endPointName,
    nonce,
    apiPostBodyData
  );
  const httpOptions = {
    headers: { "API-Key": apiPublicKey, "API-Sign": signature },
    responseType: "arraybuffer",
  };

  let jsonData;
  await axios
    .post(apiEndpointFullURL, apiPostBodyData, httpOptions)
    .then((res) => {
      jsonData = res;
    })
    .catch((err) => {
      jsonData = err;
    });

  return jsonData.data;
}

// Authentication algorithm
function CreateAuthenticationSignature(
  apiPrivateKey,
  apiPath,
  endPointName,
  nonce,
  apiPostBodyData
) {
  const apiPost = nonce + apiPostBodyData;
  const secret = Buffer.from(apiPrivateKey, "base64");
  const sha256 = crypto.createHash("sha256");
  const hash256 = sha256.update(apiPost).digest("binary");
  const hmac512 = crypto.createHmac("sha512", secret);
  const signatureString = hmac512
    .update(apiPath + endPointName + hash256, "binary")
    .digest("base64");
  return signatureString;
}

main();

*स्टेकिंग के लिए पात्रता मानदंड (भौगोलिक प्रतिबंधों सहित) का अवलोकन यहाँ पाया जा सकता है।

क्या आपको और मदद चाहिए?