REST API कमांड लाइन क्लाइंट (पायथन)

अंतिम अपडेट: 1 अप्रैल 2025

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

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

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

  • निजी खाता डेटा एंडपॉइंट

  • निजी ट्रेडिंग एंडपॉइंट

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

  • निजी अर्न एंडपॉइंट

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

इंस्टॉलेशन

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

  • macOS और Linux में संभवतः पहले से ही Python 3 इंस्टॉल है।

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

2. krakenapi.py फ़ाइल को अपनी पसंद के फ़ोल्डर (डायरेक्टरी) में अपने कंप्यूटर पर डाउनलोड और सेव करें।

उदाहरण के लिए: Macintosh HD > Users > Satoshi > KrakenAPI


3. एक कमांड प्रॉम्प्ट खोलें (जैसे macOS का टर्मिनल), और पिछले चरण में चुने गए फ़ोल्डर (डायरेक्टरी) पर नेविगेट करें। आप नेविगेट करने के लिए UNIX/Linux "cd" कमांड (डायरेक्टरी बदलें) का उपयोग कर सकते हैं।

उदाहरण के लिए:cd /Users/Satoshi/KrakenAPI


4. krakenapi.py फ़ाइल को निष्पादन योग्य बनाएँ

आप UNIX/Linux "chmod 755" कमांड (मोड बदलें) का उपयोग कर सकते हैं।

उदाहरण के लिए: chmod 755 krakenapi.py


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

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

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

API कुंजी बनाने/कॉन्फ़िगर करने के निर्देश उपलब्ध हैं।

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

उदाहरण कमांड

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

./krakenapi.py endpoint [parameters] [-pretty]

कमांड लाइन क्लाइंट सभी REST API एंडपॉइंट का समर्थन करता है, इसलिए निम्नलिखित कुछ उदाहरण कमांड हैं:

python

Python

# Time
./krakenapi.py Time

# Ticker
./krakenapi.py Ticker pair=xbtusd

# Trades
./krakenapi.py Trades pair=etheur since=1574067140000000000

# Balance
./krakenapi.py Balance

# Trade Balance
./krakenapi.py TradeBalance asset=xbt

# Query Orders
./krakenapi.py QueryOrders txid=O7MN22-ZCX7J-TGLQHD

# Add Order
./krakenapi.py AddOrder pair=xbtusd type=buy ordertype=limit price=6500 volume=0.002 leverage=5

# Cancel Order
./krakenapi.py CancelOrder txid=O7MN22-ZCX7J-TGLQHD

डिफ़ॉल्ट रूप से कमांड लाइन क्लाइंट मूल API प्रतिक्रिया (कोड के माध्यम से व्याख्या के लिए उपयुक्त JSON) आउटपुट करेगा, लेकिन यदि एक अतिरिक्त कमांड लाइन तर्क -pretty का उपयोग किया जाता है:

./krakenapi.py TradeBalance asset=shib -pretty

तो क्लाइंट वही API प्रतिक्रिया मानव-अनुकूल JSON (लाइन ब्रेक/इंडेंट के साथ) के रूप में आउटपुट करेगा।

पायथन कोड:

python

Python

#!/usr/bin/env python3

# Kraken Rest API
#
# Usage: ./krakenapi endpoint [parameters]
# Example: ./krakenapi Time
# Example: ./krakenapi OHLC pair=xbtusd interval=1440
# Example: ./krakenapi Balance
# Example: ./krakenapi TradeBalance asset=xdg
# Example: ./krakenapi OpenPositions
# Example: ./krakenapi AddOrder pair=xxbtzusd type=buy ordertype=market volume=0.003 leverage=5

import sys
import time
import base64
import hashlib
import hmac
import urllib.request
import json

api_public = {"Time", "Assets", "AssetPairs", "Ticker", "OHLC", "Depth", "Trades", "Spread", "SystemStatus"}
api_private = {"Balance", "BalanceEx", "TradeBalance", "OpenOrders", "ClosedOrders", "QueryOrders", "TradesHistory", "QueryTrades", "OpenPositions", "Ledgers", "QueryLedgers", "TradeVolume", "AddExport", "ExportStatus", "RetrieveExport", "RemoveExport", "GetWebSocketsToken", "CreateSubaccount", "AccountTransfer"}
api_trading = {"AddOrder", "AddOrderBatch", "EditOrder", "CancelOrder", "CancelOrderBatch", "CancelAll", "CancelAllOrdersAfter","OrderAmends","AmendOrder","ListSubaccounts"}
api_funding = {"DepositMethods", "DepositAddresses", "DepositStatus", "WithdrawMethods", "WithdrawAddresses", "WithdrawInfo", "Withdraw", "WithdrawStatus", "WithdrawCancel", "WalletTransfer"}
api_staking = {"Earn/Strategies", "Earn/Allocations", "Earn/Allocate", "Earn/Deallocate", "Earn/AllocateStatus", "Earn/DeallocateStatus", "Staking/Assets", "Staking/Balance", "Stake", "Unstake", "Staking/Pending", "Staking/Transactions"}

api_domain = "https://api.kraken.com"
api_data = ""

output_format = 0

if len(sys.argv) < 2:
	api_method = "Time"
elif len(sys.argv) == 2:
	api_method = sys.argv[1]
else:
	api_method = sys.argv[1]
	for count in range(2, len(sys.argv)):
		if sys.argv[count] == '-pretty':
			output_format = 1
			continue
		if count == 2:
			api_data = sys.argv[count]
		else:
			api_data = api_data + "&" + sys.argv[count]

if api_method in api_private or api_method in api_trading or api_method in api_funding or api_method in api_staking:
	api_path = "/0/private/"
	api_nonce = str(int(time.time()*1000))
	try:
		api_key = open("API_Public_Key").read().strip()
		api_secret = base64.b64decode(open("API_Private_Key").read().strip())
	except:
		print("API public key and API private (secret) key must be in plain text files called API_Public_Key and API_Private_Key")
		sys.exit(1)
	api_postdata = api_data + "&nonce=" + api_nonce
	api_postdata = api_postdata.encode('utf-8')
	api_sha256 = hashlib.sha256(api_nonce.encode('utf-8') + api_postdata).digest()
	api_hmacsha512 = hmac.new(api_secret, api_path.encode('utf-8') + api_method.encode('utf-8') + api_sha256, hashlib.sha512)
	api_request = urllib.request.Request(api_domain + api_path + api_method, api_postdata)
	api_request.add_header("API-Key", api_key)
	api_request.add_header("API-Sign", base64.b64encode(api_hmacsha512.digest()))
	api_request.add_header("User-Agent", "Kraken REST API")
elif api_method in api_public:
	api_path = "/0/public/"
	api_request = urllib.request.Request(api_domain + api_path + api_method + '?' + api_data)
	api_request.add_header("User-Agent", "Kraken REST API")
else:
	print("Usage: %s method [parameters]" % sys.argv[0])
	print("Example: %s OHLC pair=xbtusd interval=1440" % sys.argv[0])
	sys.exit(1)

try:
	api_reply = urllib.request.urlopen(api_request).read()
except Exception as error:
	print("API call failed (%s)" % error)
	sys.exit(1)

try:
	api_reply = api_reply.decode()
except Exception as error:
	if api_method == 'RetrieveExport':
		sys.stdout.buffer.write(api_reply)
		sys.exit(0)
	print("API response invalid (%s)" % error)
	sys.exit(1)

if '"error":[]' in api_reply:
	print(api_reply if output_format == 0 else json.dumps(json.loads(api_reply), indent = 4))
	sys.exit(0)
else:
	print(api_reply if output_format == 0 else json.dumps(json.loads(api_reply), indent = 4))
	sys.exit(1)

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