Why does Kucoin spot order URL seem to be causing KeyError?

229 Views Asked by At

I tried to create a simple trading execution code for one-time spot market buy order in Python (using Pycharm).

The code is provided below. API key, API passphrase and API secret key are correct, as well as a signature encoded in base64. The problem seems to be with the order URL. I use this one (provided by Kucoin API documentation): api/v1/orders

The error I'm getting is:

Traceback (most recent call last):
  File "C:\Users\xxxx\PycharmProjects\pythonProject11\main 2.py", line 57, in <module>
    response = client.request('POST', url, body=body_str, headers=headers)
  File "C:\Users\xxxx\PycharmProjects\pythonProject11\venv\lib\site-packages\ccxt\base\exchange.py", line 2801, in request
    return self.fetch2(path, api, method, params, headers, body, config, context)
  File "C:\Users\xxxx\PycharmProjects\pythonProject11\venv\lib\site-packages\ccxt\base\exchange.py", line 2797, in fetch2
    request = self.sign(path, api, method, params, headers, body)
  File "C:\Users\xxxx\PycharmProjects\pythonProject11\venv\lib\site-packages\ccxt\kucoin.py", line 3259, in sign
    url = self.urls['api'][api]
KeyError: '/api/v1/orders'

Process finished with exit code 1

Code:

import ccxt
import base64
import hashlib
import hmac
import json
from datetime import datetime

# Replace these with your own API key, secret key, and passphrase
api_key = ''
api_secret = ''
api_passphrase = ''

# Set up the Kucoin client
client = ccxt.kucoin()
client.apiKey = api_key
client.secret = api_secret
client.version = 'v1'

# Set the request parameters -- I know there should be a specific token in place of XXXX
symbol = 'XXXX/USDT'
side = 'buy'
type = 'market'
amount = 1.0

# Generate the timestamp in ISO 8601 format
timestamp = datetime.utcnow().isoformat() + 'Z'

# Construct the request body
body = {
  'clientOid': 'YOUR_UNIQUE_ID',
  'size': amount,
  'side': side,
  'symbol': symbol,
  'type': type
}

# Encode the request body as a JSON string
body_str = json.dumps(body)

# Set the request URL
url = '/api/v1/orders'

# Construct the signature
message = timestamp + 'POST' + url + body_str
signature = base64.b64encode(hmac.new(bytes(api_secret, 'latin-1'), bytes(message, 'latin-1'), digestmod=hashlib.sha256).digest())

# Set the request headers
headers = {
  'KC-API-KEY': api_key,
  'KC-API-SIGN': signature,
  'KC-API-TIMESTAMP': timestamp,
  'KC-API-PASSPHRASE': api_passphrase,
  'Content-Type': 'application/json'
}

# Send the POST request to the "POST /api/v1/orders" URL
response = client.request('POST', url, body=body_str, headers=headers)

# Print the response
print(response)

I tried various URLs but it doesn't seem to work. What is causing the problem?

0

There are 0 best solutions below