Bitcoin

How to POST using RobinHood Crypto API in Python?

I’m using the new RobinHood crypto API documented here: https://docs.robinhood.com/crypto/trading/#tag/Trading/Operation/api_v1_post_crypto_trading_order

All GET endpoints are successful, but I can’t seem to place an order using the POST endpoint. It fails with a verification error. The test results usually mean there is a problem with the message. As far as I can tell, I followed the documentation exactly.

Here is my code. Am I missing something?

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

body = 
    "client_order_id" : "131de903-5a9c-4260-abc1-28d562a5dcf0",
    "side" : "buy",
    "symbol" : "BTC-USD",
    "type" : "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"body"
)

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

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

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

headers = 
    'x-api-key': API_KEY_ROBIN_HOOD,
    'x-timestamp': str(current_unix_timestamp),
    'x-signature': base64_signature,
    'Content-Type': 'application/json; charset=utf-8',


response = requests.post(url, headers=headers, json=body)

The entire code is almost identical to my (working) GET function, except that it is a POST request and for the first time the request (and message) contains a body element.

Do you see anything I overlooked?

Related Articles

Back to top button