Help getting starting with API

So I am trying to figure out how to use the coinbase api. I am very new to using requests and python to be quite frank. I have been really struggling on how to use the api. Does anyone have any links to good guides or can give detailed examples of basic functions for the api?
looking through the forum I have found how to place an order but I have been unable to reverse engineer it to use it to do other operations. I have read through the documentation and there is just seems to be quite a high level of required knowledge that I just don’t have yet.
If I can’t figure out CB api then I will just stick with using Krakens api, they had an example for every action you could do with their api in python which was just amazing for a beginner like myself. It was almost plug and play.

Hi @Pidermonkey24, first of all, thank you for taking interest in using the Coinbase APIs and in participating in the forum. Can you provide more details on the specific goal you want to complete right now in using Coinbase APIs? Can you also share which Coinbase API documentations you’ve read so far?

Usually, the https://docs.cloud.coinbase.com/ is a good starting point for beginners. You can choose the API you want to use and it will redirect you to the relevant documentation. If you have read through the documentation and you are encountering any issues on using the API, please feel free to post here in the Developer Support forum for answers from the community or from Coinbase.

Your feedback will help us assess how we can better assist you. Thank you so much!

Okay so I guess I can start with what I have already written.

here is my code for making a signature, making a basic request function then a function that is supposed to place a buy order. I have essentally based the format that kraken used because it was easy to understand.

Whenever I run it I keep getting {“message”:“invalid signature”}. I have another function that does successfully place a buy order but it isn’t as reusable to make other requests. I have based all of the signature code off of the working function but I just can’t get this one to work.

def coinbase_signature(url_path, data, secret, req_method, time_stamp):
    CbMsg = '{}{}{}{}'.format(time_stamp,req_method,url_path,data)
    secret = base64.b64decode(secret)
    data = CbMsg.encode('utf-8')
    
    # If I don't include this CbMsg section then the .encode method below doesn't work
    # data = data.encode('utf-8')
    ash = hmac.digest(secret, data, hashlib.sha256)
    sig = base64.b64encode(ash).decode('utf-8')
    return sig

if exchange == "coinbase_pro":
    api_url = 'https://api.exchange.coinbase.com'
elif exchange == "coinbase_sandbox":
    api_url = 'https://api-public.sandbox.exchange.coinbase.com'
else:
    api_url = 'https://api.exchange.coinbase.com'

def coinbase_request(url_path, data, public_key, secret_key, cb_passphrase, req_method):
    time_stamp = str(int(time.time()))
    body = json.dumps(data)
    # formatted_data = '{}{}{}{}'.format(time_stamp, req_method, url_path, body)
    sig = coinbase_signature(url_path, data, secret_key, req_method, time_stamp)

    headers = {
        "Accept": "application/json",
        'Content-Type': 'application/json',
        'CB-ACCESS-TIMESTAMP': time_stamp,
        'CB-ACCESS-KEY': public_key,
        'CB-ACCESS-PASSPHRASE': cb_passphrase,
        'CB-ACCESS-SIGN': sig
    }

    if req_method == "POST":
        req = requests.post(api_url, body, headers=headers)
    elif req_method == "GET":
        req = requests.get(api_url, body, headers=headers)
                
    return req

def action_place_order(data, settings, cb_public_key, cb_secret_key, cb_passphrase):
    try:
        resp = coinbase_request("/orders", {
            "profile_id": "sandbox",
            "type": "limit",
            "side": "buy",
            "product_id": "BTC-USD",        
            "stp": "co",
            "price": "20.0",
            "size": "20",
            "time_in_force": "GTC",
            "post_only": "True",
            "client_oid": "496ffbba-7dae-4864-f7e8-000000000001"
        }, cb_public_key, cb_secret_key, cb_passphrase, "POST")
        return resp
    except requests.exceptions.RequestException as e:
        print("crap")
        return e

# test = action_place_buy_order()

test = action_place_order(None, None, public_key, secret_key, passphrase)

print(test)
print("-=-=-")
print(test.text)```

Hi @Pidermonkey24 , thank you for taking an interest in trying out Coinbase APIs. For the details regarding your inquiry, we will check on this for you with our team to see how we can best assist. We’ll get back to you once we have more information. Keep in touch!

3 Likes

Sorry to post my issue here, I am not able to create a new post. I would like to know if I can get the USD price for coin to coin (e.g BTC to ICP) transactions on COINBASE Prime through API.

Hi @Pidermonkey24, can you confirm if the code that you’ve shared is your complete code or just a snippet? We will need your complete code to help with in-depth investigation.
Thank you. Hope to hear from you soon!

1 Like

@judy.absalon That’s just a snippet. I have another section in my code that does successfully place an order that I got from another section on the forums. I just tried to alter the working code to work like I have in my other bot that is currently working for Kraken. However I have had no success as of yet.

Hello @Pidermonkey24. Apologies for the delayed response. We would like to ask you to provide us with your full code so that we can better solve the problem you are encountering. Thank you!

1 Like

Hi @achuckoury and to others encountering the same issue, discussion regarding this concern was continued in this thread.

2 Likes