Create order advanced trade api, Error 502 bad gateway

When i make the call with my code:

import requests
import time
import hashlib
import hmac
import json
import time
import urllib.request as urllib2
from urllib.parse import urlencode, unquote, urljoin
import http.client

conn = http.client.HTTPSConnection("api.coinbase.com")
payload = "{\"product_id\":\"BTC-USD\",\"side\":\"BUY\",\"client_order_id\":\"default\"}"
timestamp = str(int(time.time())) 
secretKey = "mysecret"
message = timestamp + "POST" + "/api/v3/brokerage/orders" + str(payload)
signature = hmac.new(secretKey.encode('utf-8'), message.encode('utf-8'), digestmod=hashlib.sha256).digest()
headers = {
    'accept': 'application/json',
    'CB-ACCESS-KEY': 'mykey',
    'CB-ACCESS-SIGN': signature.hex(),
    'CB-ACCESS-TIMESTAMP': timestamp
}
conn.request("POST", "/api/v3/brokerage/orders", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

It results in an error bad gateway:

<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
</body>
</html>

I hope to get an error, not enough funds (which is true) but i think that either my body or another part of the code is wrong.

Hi @Shinyzack123! Welcome to the forum!

We’re happy to help and we understand that you are encountering a 502 Bad Gateway error response when trying to push a buy order using the Create Order endpoint.

Upon checking the code snippet you have shared with us, what we can suggest to you is to utilize the order_configuration parameter and this is shown in the documentation which have several possible options such as: market_market_ioc, limit_limit_gtc, limit_limit_gtd, stop_limit_stop_limit_gtc, and stop_limit_stop_limit_gtd. Each order_configuration object corresponds to the order_type and time_in_force parameters that are in use for Coinbase Pro. For example, market_market_ioc corresponds to market order with Immediate or Cancel (ioc) policy for the lifetime of the order. Meaning, should your order fall under this category of market_market_ioc, your request body must only contain the parameters under this order_configuration: quote_size (required for BUY orders) and base_size (required for SELL orders).

Additionally, here is a forum post that is related to your concern. You might want to check it out. Aside from the forum post, you may visit this link to know more about order_configurations.

We hope this helps. If the issue still persists after utilizing the order_configuration objects, please don’t hesitate to reach back to us.

Thank you and have a great day!

1 Like

I changed my code to this:

import requests
import sys
import platform
import time
import base64
import hashlib
import hmac
import json
import time
import urllib.request as urllib2
import os
import urllib.parse
import jwt
import uuid
from urllib.parse import urlencode, unquote, urljoin
import http.client

conn = http.client.HTTPSConnection("api.coinbase.com")
    payload = "{\"side\":\"BUY\",\"client_order_id\": \"default\",\"product_id\":\"BTC-USD\",\"order_configuration\": {\"market_market_ioc\": {\"quote_size\": \"1000\"}}}"
    timestamp = str(int(time.time())) 
    secretKey = "mysecret"
    message = timestamp + "POST" + "/api/v3/brokerage/orders" + payload
    signature = hmac.new(secretKey.encode('utf-8'), message.encode('utf-8'), digestmod=hashlib.sha256).digest()
    headers = {
        'accept': 'application/json',
        'CB-ACCESS-KEY': 'mykey',
        'CB-ACCESS-SIGN': signature.hex(),
        'CB-ACCESS-TIMESTAMP': timestamp,
        "content-type": "application/json"
    }
    conn.request("POST", "/api/v3/brokerage/orders", payload, headers)
    res = conn.getresponse()
    data = res.read()
    print(data.decode("utf-8"))

EDIT:

Added the side but now i get the same error:

<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>  
</body>
</html>
1 Like

Hi @Shinyzack123! We understand that you are still having issues placing a BUY order. We are working with our team to check on this but we would like to ask if this is your first time placing an order using the Create Order endpoint? If not, were you able to successfully place an order before?

Once you send us the information requested above, we’ll work to quickly address your concern. We appreciate your patience and understanding. Have a nice day!

2 Likes

This is my first time trying to place an order with the api endpoint.

1 Like

Hi @Shinyzack123! Thank you for your patience as we look into this. Based on the inputs we got from the relevant teams, we suggest that you try running your code again, but this time, please set a value to the parameter client_order_id (unique uuid for this order) instead of default. In addition, please note that we also found a forum post that could be helpful with your case scenario. We highly suggest that you try looking into this for reference, since the user provided a random client_order_id generator which you could also embed in your code.

Please do not hesitate to reply back to this thread if you still have queries/concerns. Thank you and have a great day!

1 Like

Thank you so much for all the help, it works now, last question, for some reason this line

    order_id = np.random.randint(2**63)

gives me an error when i run it in visual studio code, when i run it in google colab, it works fine. despite this my vsc is 64 bit so i shouldn’t be seeing an error.
Then i tried

    order_id = np.random.randint(2**31)

and it ran telling me that i had insufficient funds which is true :slight_smile: but i don’t know if that would give me the same result from the order api as the first sample of code (64 bit), can you confirm that it is okay if i use the second sample of code.

Thanks

TL;DR: That should be fine.

The client_order_id can be pretty much anything you want. Could use a random word generator if you wanted to. The important part is that the id is truly random. If you use an id that you’ve already used in the past, Coinbase will return the order that you placed in the past, and won’t create a new order.

With a 32 bit integer, you have 4,294,967,295 possible random numbers. With 64 bit you have 9,223,372,036,854,775,807. Both of these are very large, but technically using 64 bit would have a lower chance of generating the same number twice. If you want to be extra safe you could generate multiple integers and concatenate them together as strings. You could also look into UUIDs. UUIDv4 is generally pretty easy to implement and will give you a 128 bit id, which is practically impossible to accidentally duplicate.

At the end of the day, there’s no security risk here or anything. The client id is mostly for your own organizational purposes. Using 32 bit ints is totally okay. You will get the same results as with 64 bit ints.