Sunday, December 22, 2024

Find out how to POST utilizing RobinHood Crypto API in Python?

I am utilizing the brand new RobinHood crypto API documented right here: https://docs.robinhood.com/crypto/buying and selling/#tag/Buying and selling/operation/api_v1_post_crypto_trading_order

I’m profitable with all GET endpoints, however can not seem to place an order utilizing the POST endpoints. It fails with a verification error, which testing exhibits often imply one thing in regards to the message is incorrect. So far as I can inform although, I’ve adopted the documentation precisely.

Right here is my code. Is there one thing that I am lacking?

url = "https://buying and selling.robinhood.com/api/v1/crypto/buying and selling/orders/"
api_path = "/api/v1/crypto/buying and selling/orders/"
http_method_type = "POST"

physique = {
    "client_order_id" : "131de903-5a9c-4260-abc1-28d562a5dcf0",
    "aspect" : "purchase",
    "image" : "BTC-USD",
    "sort" : "market",
    "market_order_config" : {
        "asset_quantity" : "0.1"
    }
}    

current_unix_timestamp = int(time.time())

message = (
    f"{API_KEY_ROBIN_HOOD}"
    f"{current_unix_timestamp}"
    f"{api_path}"
    f"{http_method_type}"
    f"{physique}"
)

signature = PRIV_KEY.signal(message.encode("utf-8"))

base64_signature = base64.b64encode(signature).decode("utf-8")

PUB_KEY.confirm(signature, message.encode("utf-8"))

headers = {
    'x-api-key': API_KEY_ROBIN_HOOD,
    'x-timestamp': str(current_unix_timestamp),
    'x-signature': base64_signature,
    'Content material-Sort': 'software/json; charset=utf-8',
}

response = requests.put up(url, headers=headers, json=physique)

Your entire code is nearly similar to my (working) GET features, besides that this can be a POST request and that for the primary time, the request (and the message) features a physique ingredient.

Do you see one thing I’ve missed?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles