使用Google Sheets脚本编辑器调用Kraken API:AddOrder参数位置疑问
Hey there! Nice job getting the balance check working already—you’re halfway there. Let’s clear up how to send those pair, type, and ordertype parameters to the AddOrder endpoint.
First off, Kraken’s private API endpoints (like /0/private/AddOrder) require a POST request, and all your order-specific parameters (plus mandatory auth fields) go directly in the request body. You can’t tack them onto the URL like you might with some public endpoints.
Here’s a concrete example using Python (super common for API testing):
import requests import time import urllib.parse import hashlib import hmac import base64 # Replace these with your actual Kraken API credentials API_KEY = "your_kraken_api_key" API_SECRET = "your_kraken_api_secret" # Your order details—this is where you define your core parameters order_details = { "pair": "XBTUSD", # The trading pair you’re targeting "type": "buy", # Use "buy" or "sell" depending on your goal "ordertype": "limit", # Options include "limit", "market", "stop-loss", etc. "price": "32000", *# Required for limit orders—set your desired execution price* "volume": "0.001", # How much of the asset you want to trade "nonce": str(int(time.time() * 1000000)) # Unique, incrementing number (critical for security) } # Generate the required API signature (Kraken’s way of verifying your request is legitimate) url_path = "/0/private/AddOrder" encoded_params = urllib.parse.urlencode(order_details).encode() message = url_path.encode() + hashlib.sha256(str(order_details["nonce"]).encode() + encoded_params).digest() signature = hmac.new(base64.b64decode(API_SECRET), message, hashlib.sha512) api_sign = base64.b64encode(signature.digest()).decode() # Set up headers with your API key and generated signature headers = { "API-Key": API_KEY, "API-Sign": api_sign } # Send the POST request to Kraken response = requests.post("https://api.kraken.com/0/private/AddOrder", data=order_details, headers=headers) print(response.json())
Key things to keep in mind:
- All order parameters live in the request body: Whether you’re using code, Postman, or another tool, input
pair,type,ordertype, and related fields (likepricefor limits) as form data or JSON in the POST body. - Don’t skip the auth requirements: The
nonce(unique, incrementing number) andAPI-Sign(generated signature) are mandatory for all private endpoints. Kraken won’t process your order without these. - Double-check parameter rules: For
limitorders, you must includeprice; formarketorders, you can omit it. Also, make sure your API key has "Trade" permissions enabled in your Kraken account settings—otherwise you’ll hit an authorization error.
If you’re using a tool like Postman instead of code, just set the request method to POST, add your parameters as "form-data" in the Body tab, then add the API-Key and API-Sign headers (you’ll need to generate the signature manually or use Postman’s pre-request script to automate it).
内容的提问来源于stack exchange,提问作者Rob Cranton

