Coinbase Pro Api Reference, get all orders for portfolio

Hi,
I use the coinbase pro api since long to place limit orders.
Now I wanted to fetch all open orders for a given portfolio and can not succeed.

First, the API Reference here uses as url base: https://api.exchange.coinbase.com/, while I use https://api.pro.coinbase.com/.

Question one: do I have the wrong api reference? Can anyone provide me a link to the coinbase pro api? (all resources I can find nowadays point to the one given above).

Question two: the api reference given above does nowhere state something about auth. So far, I use:
authPortfolio1=CoinbaseExchangeAuth(API_KEY_PF1, API_SECRET_PF1, API_PASS_PF1)
order = {
‘type’: ‘limit’,
‘price’: price,
‘size’: size,
‘side’: ‘buy’,
‘product_id’: portfolio_dict_in[portfolio_in][‘product_id’],
}
r = requests.post(api_url + ‘orders’, json=order, auth=authPortfolio1)

so how to do this with the api given above?

Question three
Is there an example on how to request all open orders for a portfolio, authenticated?

Thanks you!
Alex

Hello @alexp! Thank you for taking an interest in trying out Coinbase APIs. For the details regarding your concern, we will check on this for you with our team. We will get back to you once we have more information. Keep in touch!

1 Like

Hi @alexp, Welcome to the Developer’s Forum!We’re happy to help, but first we’ll need you to provide us with the following information so that we can further assist you with the issue:

  • Are you using a third party library for your code? If so, please share what library you are trying to use. If not, please share a snippet of the code detailing the CoinbaseExchangeAuth function.

Once you provide the information above, we’ll work to quickly address the concern. In the meantime, you can refer to this documentation for the Exchange/Pro API guide about how to authenticate: Authorization and Authentication

On the other hand, regarding your queries about the link to Coinbase Pro, please note that Coinbase Pro and Coinbase Exchange API are interchangeable; thus the same documentation. The main difference between the two is its intended users. Coinbase Pro is for individuals while the Coinbase Exchange is for institutional accounts. You may use endpoints from both the APIs. To access the endpoints of Coinbase pro API, you can just change the word “exchange” on the URLs of your requests to “pro.” For example, https://api.exchange.coinbase.com/accounts is similar to https://api.pro.coinbase.com/accounts.

To list all open orders, orders for a single portfolio can be queried using /orders endpoint since API keys for Coinbase Pro is portfolio-specific, meaning it is scoped to a single portfolio by default.

Looking forward to your response and hoping we answer your concern regarding Coinbase Pro documentation and open orders.

Thank you!

3 Likes

Hi Jonathan,
first of all, sorry for the late response, I was traveling with quite limited access to the internet / this forum.

I do not use a third party library as far as I see.
Here comes my minimal example making use of the portfolios (which I find quite handy, pleas keep support for it):

# Requires python-requests. Install with pip:
import json, hmac, hashlib, time, requests, base64
from requests.auth import AuthBase
import sys #for devel exit

# Create custom authentication for Exchange
class CoinbaseExchangeAuth(AuthBase):
    def __init__(self, api_key, secret_key, passphrase):
        self.api_key = api_key
        self.secret_key = secret_key
        self.passphrase = passphrase

    def __call__(self, request):
        timestamp = str(time.time())
        message = timestamp + request.method + request.path_url + (request.body or '')
        hmac_key = base64.b64decode(self.secret_key)
        signature = hmac.new(hmac_key, message, hashlib.sha256)
        signature_b64 = signature.digest().encode('base64').rstrip('\n')

        request.headers.update({
            'CB-ACCESS-SIGN': signature_b64,
            'CB-ACCESS-TIMESTAMP': timestamp,
            'CB-ACCESS-KEY': self.api_key,
            'CB-ACCESS-PASSPHRASE': self.passphrase,
            'Content-Type': 'application/json'
        })
        return request

def get_eurBalance(portfolio_in,portfolio_dict_in):
    #r = requests.get(api_url + 'accounts/'+EUR_ID, auth=portfolio_dict_in[portfolio_in]['auth'])
    r = requests.get(api_url + 'accounts/' + portfolio_dict_in[portfolio_in]['EUR_ID'], auth=portfolio_dict_in[portfolio_in]['auth'])
    a=r.json()
    bal=float(a['balance'])
    hold=float(a['hold'])
    free=bal-hold
    #print r.json()

    print("EUR Balance / Hold / Free: "+str(bal) +" / " +str(hold)+" / "  +str(free))

    return bal,hold,free

# price is in EUR, size in coin
def place_limitedPortfolioOrder(portfolio_in,portfolio_dict_in,price, size):
    # Place an order
    tSucc=False

    order = {
        'type': 'limit',
        'price': price,
        'size': size,
        'side': 'buy',
        'product_id': portfolio_dict_in[portfolio_in]['product_id'],
    }

    r = requests.post(api_url + 'orders', json=order, auth=portfolio_dict_in[portfolio_in]['auth'])
    #print r.json()
    if r.status_code==200:
        print("success")
        tSucc=True
    else:
        print("!Error! in trade")
        txt = raw_input("Continue? [y/N]: ")
        if txt!='y':
            raise Exception("Stopped execution of orders")



    return tSucc

#============================================================================
#=== set-up keys
#============================================================================
api_url = 'https://api.pro.coinbase.com/'

#=== default
API_KEY='XXX'
API_SECRET='XXX'
API_PASS='XXX'
BTC_ID='XXX'
EUR_ID='XXX'
#auth = CoinbaseExchangeAuth(API_KEY, API_SECRET, API_PASS)

#==22accBTC
API_KEY_22accBTC='XXX'
API_SECRET_22accBTC='XXX'
API_PASS_22accBTC='XXX'
#auth_22accBTC = CoinbaseExchangeAuth(API_KEY_22accBTC, API_SECRET_22accBTC, API_PASS_22accBTC)
EUR_ID_22accBTC='XXX'

#==22accATM
API_KEY_22accATM='XXX'
API_SECRET_22accATM='XXX'
API_PASS_22accATM='XXX'
#auth_22accATM = CoinbaseExchangeAuth(API_KEY_22accATM, API_SECRET_22accATM, API_PASS_22accATM)
EUR_ID_22accATM='XXX'

portfolio_dict = {
                'default': {'auth': CoinbaseExchangeAuth(API_KEY, API_SECRET, API_PASS),
                            'EUR_ID': EUR_ID
                            },
                '22accBTC': {'auth': CoinbaseExchangeAuth(API_KEY_22accBTC, API_SECRET_22accBTC, API_PASS_22accBTC),
                             'product_id': 'BTC-EUR',
                             'EUR_ID': EUR_ID_22accBTC,
                             'minOrderEur': 0.1,
                             'amountDigits': 5,
                             'totalVolumeInclFee': 9614,#807,#6108,#800,  #stays
                             'high': 17500,#32500,#<may22 #39000,  #<======= SET!!!
                             'low': 3000   #stays
                             },
                '22accATM': {'auth': CoinbaseExchangeAuth(API_KEY_22accATM, API_SECRET_22accATM, API_PASS_22accATM),
                            'product_id': 'ATOM-EUR',
                            'EUR_ID': EUR_ID_22accATM,
                            'minOrderEur': 0.1,
                            'amountDigits': 3,
                            'totalVolumeInclFee': 800, #stays
                            'high': 15.00, #<may22 #24.70,#26.00, #<======= SET!!!
                            'low': 3.50#20.00  #stays

                             }
                }

#============================================================================
#=== do something
#============================================================================
portfolio='22accBTC'

bal,hold,freeEu=get_eurBalance(portfolio,portfolio_dict)

#determine price and size_ and place the limit order
#place_limitedPortfolioOrder(portfolio, portfolio_dict, price, size_)
print('Done')

The portfolio-dict keeps the keys and settings for the portfolios, such that one can simply call e.g. the provided get_eurBalance for a portfolie, calculate the desired limit and volume and place a limit order.

An example in this style how to fetch all open orders for a given portfolio (and how to cancel an order) would be nice!

Thanks a lot!
Alex

regarding yout hint: yes, I know all this documentation. The examples however do not apply any auth, nor portfolio specification. This is what irritates me…

Hello @alexp! Thank you for providing us with more information as well as the code snippet as requested.

We can see that you are actually able to integrate Coinbase Pro APIs with your program and we’re happy to assist you further on this. However, please note that we are only able to provide guidance and troubleshooting for issues with the raw Coinbase APIs. Thus, we cannot provide specific guidance on how to develop your application such as writing code in specific programming languages and frameworks.

Please note that all we can do for you now is to refer you back to this API reference link and guide you on how to utilize such in calling for the specific order requests as you mentioned. You can actually utilize this /orders endpoint to view all open orders for a specific portfolio by filling in the profile_id parameter when you call for the GET request. When you apply this profile_id parameter, the GET request will actually have a response that is filtered for that specified profile_id or portfolio. However, please note that in order to successfully run this request, a “view” or “trade” permission should first be initiated.

Moving forward, please note that the same is true for cancelling an order for a given profile_id. When calling for the DELETE request, you have to specify the order_id and profile_id in the parameter and should have a “trade” scope permission. For more details, you can refer to this link.

We hope that we are still able to help and provide you with clarity on utilizing Coinbase Pro APIs. Please feel free to reach back to us if you have other questions or concerns.

2 Likes

Hi @bazinga ,

i finally got it. I had the permissions, but was thinking the auth part was wrong, which is not.
It was a misunderstanding on my side (I confused product id (like ‘BTC-EUR’ , actually required by the order request) with the id of the currency you trade (e.g. the euro-id you generate per portfolio).

Now it works all fine. Thank you!

Alexander

Hello @alexp,

Thanks for letting us know. Happy to know that the issue has been resolved! :smile:

1 Like