Can anyone provide a working example of any API call in python?

Hi,
I know this question has been asked here a zillion times and I’m sorry, but I couldn’t mine the answer from here, documentation, pip import, youtube, google, chat GPT and from two years of coding.
So yes, I’m an imbecile. No need to point that out. Please help me.

My ultimate goal is to call API with options. My example is listFills() with an option listFills(product_id => UNI-EUR)
But it can be any call with any option - just that the option has to make a difference.

I started out and am using this: GitHub - coinbase/coinbase-advanced-py: The Advanced API Python SDK is a Python package that makes it easy to interact with the Coinbase Advanced API. The SDK handles authentication, HTTP connections, and provides helpful methods for interacting with the API.
But I couldn’t manage to use options there.

So I tried building the requests manually. Using documentation.
I have to calculate within a 30s timeframe, so Postman goes out of the window.
What I created didn’t work.
What I found somewhere either didn’t work or went 401.

So please. Please. I’m begging you all. Please one person step forward and give me those stupid couple of lines of code.
I have my key and my secret and I need everything else.
Please!

All right, I figured it out, hope it helps someone sometime!

So even if filtering using “product_id” works (my screenshot). And also “limit” works.
“start_date” and “end_date” doesn’t seem to work.

Can anyone confim, they can sucessfully run one of those two (with listing fills) and hence the issue is on my side?

List Fills endpoint does not have start_date:
https://docs.cloud.coinbase.com/advanced-trade-api/reference/retailbrokerageapi_getfills

Thank you. I looked into the pip package and it is implemented like this:
image
So that’s also a no-go :frowning:

By any change you wouldn’t have a piece of python code where you’d send this call manually, provided two information:

  • api key
  • api secret

Would you?
Thank you

Use their SDK?

They are using correct parameters:

I tried.
I admit it was only a couple of hours before I figured that there is no way I making it work that way. I also tried the python, but non SDK way. I tried doing it manually and I tried two pip libraries. I explored everything there is to be explored. I would not ask, if there was a non zero chance I could do it without asking.

I would like to appeal on your empathy.
I don’t mean it in a bad way - at all!
But where do I put my api key and api secret into the code you sent?
If I copy it, there’s nothing calling it, so it won’t do anything.
Hence, it’s of no use to me.

I will understand completely, if you don’t want to help me, but if you do, I will repeat my question:

By any change you wouldn’t have a piece of python code where you’d send this call manually, provided two information:

  • api key
  • api secret

Would you?

Thank you very much.

Read documentation:

They have examples and codes to get you started. Simply use their SDK. I doubt it will get simpler than that.

If you want copy & paste code I guess you will have to pay for that.

Here is Python that I used to get open orders (into file orders.json) while using the coinbase SDK. I pipe the output to a script and run that (using curl). More as a way to translate to Perl than anything else.

from coinbase import jwt_generator
api_key = “organizations/SOMETING/apiKeys/SOMETHING”
api_secret = “-----BEGIN EC PRIVATE KEY-----\nSOMETHING\n-----END EC PRIVATE KEY-----\n”

request_method = “GET”
request_path = “/api/v3/brokerage/orders/historical/batch”

def main():
jwt_uri = jwt_generator.format_jwt_uri(request_method, request_path)
jwt_token = jwt_generator.build_rest_jwt(jwt_uri, api_key, api_secret)
print(f"set JWT={jwt_token}")
print(f’curl -s -H “Authorization: Bearer %JWT%” -H “Content-Type: application/json” https://api.coinbase.com/api/v3/brokerage/orders/historical/batch -o orders.json’)

if name == “main”:
main()

However, I have not been able to get any of the POST versions to work. I always get [unauthorized] as the response. Coinbase helpdesk is little help.

Since hard coding the keys into the code is weak security, you can use (for example)

import sys
symbol = sys.argv[1]
entryQ = sys.argv[2]
clientOID = sys.argv[3]

to pass the vars to the python script at runtime or, if you have set them up, environmental vars

import os
api_key= os.environ[‘CB_API_KEY’]
api_secret = os.environ[‘CB_API_SECRET’]

Hope this helps. The new API is more complex than the Pro version and seems to be very buggy. I am stuck on getting a simple market order to execute.

@muktupavels
already did, didn’t help.

They have examples and codes to get you started. Simply use their SDK. I doubt it will get simpler than that.

Well, that piece of code I keep asking for would be simpler than that.

If you want copy & paste code I guess you will have to pay for that.

Now you’re talking. How much?
I have a bit of code where there’s imports, key and secret, that signature crap, headers, call and printing a response, including blank lines and comments it’s 37 lines.
I’m pretty sure that by responding here that I should find a solution you already spent at least half of the 15 minutes necessary to write that piece of code and hence - how much?

@bubu0000
Thank you. I mean THANK YOU!
However…
Splitting your message into two parts where the second part starts with “However, I have not…”
There is no way, that that code is going to do something. Maybe it will run, but there is no way in hell it’s going to do anything remotely similar to getting data from coinbase.
Why? I have no idea and so many questions that asking them all would be super stupid and starting to ask with one would be equally stupid.
But I can say that I have no idea what the second part of your message has to do with first part. Not that I don’t understand, I do. I just have no idea how to connect the second part with the first part.

And so I will show you my version of your code and how it won’t work. Which is not surprising, but please assume I’m that stupid and try to help…if you’d be kind enough?
The edit’s I did to your code:

  • subtituted copied apostrophes and quotes to the ones ok for python
  • indentation
  • initialization of the main variable, which I have no idea what is for
    Here goes:

Why don’t you post code that you tried? It is easier to help with specific problems…

from coinbase.rest import RESTClient
from json import dumps

api_key = ""
api_secret = ""

client = RESTClient(api_key=api_key, api_secret=api_secret)
fills = client.get_fills(product_id="UNI-EUR")
print(dumps(fills, indent=2))

If there are many fills you need to use cursor to request next page…

How much is it worth to you? Feel free to send to muktupavels.cb.id and/or ask for email if you want to send trough Coinbase using email.

In case you are serious about that you will need to first define what exactly you want to do. I can provide working example to access specific endpoint using their SDK or without it. I won’t write app for you!

The code I provided runs and returns the correct results for open orders and positions. It is a GET method with no payload. The “SOMETHING” has to be your own API keys, of course.

Part 2: However, when I modify script to run a POST method to create an order (/api/v3/brokerage/orders) and provide the payload (coin, quantity and client order ID), it returns “unauthorized”. Same JWT generation method. And since I capture the output to a file before running that script, I can compare the curl statement I generate to the Coinbase Advanced API documentation curl example. I can’t see a difference and curl doesn’t complain. Since there are no curl errors, the JWT generation is the same, and the error is “unauthorized”, it appears that there is some undocumented or obscure parameter or syntax that is missing. I was hoping that someone had worked that out at a more primitive level than the coinbase SDK.

Quotes, double quotes, open/close quotes and other delimiters are different on different platforms. I am using Windows 10 Pro 64-bit to run curl and Python. If you cut and paste and try to run it on Linux or IOS, it will not work without adjusting the delimiters.