https-based API?

Yes, another question. (LOL)
Is it possible to use https-based APIs in Xojo using the HTMLViewer? An example I’m wanting to use is the “Coinbase API

Thanks,
Shane.

Yes Look at the httpget/http post classes.

http://documentation.xojo.com/index.php/HTTPSocket.Get

http://documentation.xojo.com/index.php/HTTPSocket.Post

I got the Get method down, but how would I get the price of say one whole Bitcoin?

My Code (so far):

Dim BuyPrice As HTTPSocket
BuyPrice.Get("https://coinbase.com/api/v1/prices/buy")

How would I send the parameter: "qty": 1
API Example

Thanks for the help!

For HTTPS use the HTTPSecureSocket instead of the HTTPSocket.

The API appears to use JSON to encode requests and responses, so I would suggest looking into the JSONItem class.

I’ll take a look into it.

Thanks!

And you’ll typically use POST instead of GET.

Even if the API specifies GET?

No - you would use GET :slight_smile:

Try the following example (with lots of variables declared so you can see the values in the debugger - you can write tighter code when you get a feel for what is going on later)

Create a simple window with a pushbutton and a textarea (TextArea1) and paste the code below into the pushbutton action handler.

  dim s as new HttpSecureSocket
  dim fullUrl as string
  dim stringContent as string
  dim jsonContent as new JSONItem()
  dim stringResponse as string
  dim response as new JSONItem()
  dim timeoutSeconds as integer = 10
  dim responseCode as integer
  
  fullUrl = "https://coinbase.com/api/v1/"
  fullUrl = fullUrl + "prices/buy"
  
  jsonContent.Value("qty") = 2.0
  stringContent = jsonContent.ToString()
  
  s.SetRequestContent(stringContent, "application/json")
  
  stringResponse = s.Get(fullUrl, timeoutSeconds)
  
  response = new JSONItem(stringResponse)
  responseCode = s.HttpStatusCode // checking and doing something with this can be important later
  
  textArea1.Text = response.ToString()

You should see:

{"subtotal":{"amount":"251.44","currency":"USD"},"fees":[{"coinbase":{"amount":"2.51","currency":"USD"}},{"bank":{"amount":"0.15","currency":"USD"}}],"total":{"amount":"254.10","currency":"USD"},"amount":"254.10","currency":"USD"}

in the textarea. Change it to read jsonContent.Value(“qty”) = 1.0 and your USD amount should be 125.76 at the time of writing this (I am assuming that the USD value can change over time.

Oh and have fun with JSONItem, it does work but can be difficult to follow until you understand how it works.

HTH

[quote=33844:@Carl Clarke]No - you would use GET :slight_smile:

Try the following example (with lots of variables declared so you can see the values in the debugger - you can write tighter code when you get a feel for what is going on later)

Create a simple window with a pushbutton and a textarea (TextArea1) and paste the code below into the pushbutton action handler.

  dim s as new HttpSecureSocket
  dim fullUrl as string
  dim stringContent as string
  dim jsonContent as new JSONItem()
  dim stringResponse as string
  dim response as new JSONItem()
  dim timeoutSeconds as integer = 10
  dim responseCode as integer
  
  fullUrl = "https://coinbase.com/api/v1/"
  fullUrl = fullUrl + "prices/buy"
  
  jsonContent.Value("qty") = 2.0
  stringContent = jsonContent.ToString()
  
  s.SetRequestContent(stringContent, "application/json")
  
  stringResponse = s.Get(fullUrl, timeoutSeconds)
  
  response = new JSONItem(stringResponse)
  responseCode = s.HttpStatusCode // checking and doing something with this can be important later
  
  textArea1.Text = response.ToString()

You should see:

{"subtotal":{"amount":"251.44","currency":"USD"},"fees":[{"coinbase":{"amount":"2.51","currency":"USD"}},{"bank":{"amount":"0.15","currency":"USD"}}],"total":{"amount":"254.10","currency":"USD"},"amount":"254.10","currency":"USD"}

in the textarea. Change it to read jsonContent.Value(“qty”) = 1.0 and your USD amount should be 125.76 at the time of writing this (I am assuming that the USD value can change over time.

Oh and have fun with JSONItem, it does work but can be difficult to follow until you understand how it works.

HTH[/quote]

Thanks for the help & code. I am having fun with JSONItem.

Label1.Text = response.Value("amount").StringValue <-- That provides just the amount it costs into a Label.

If you’re not using api keys… which it appears you aren’t why not just make a pushbutton that refreshes a web viewer set to that URL.
https://coinbase.com/api/v1/prices/buy” and then parse it accordingly… might be the dirty way, but seems much simpler.

[quote=114564:@chris ridgeway]If you’re not using api keys… which it appears you aren’t why not just make a pushbutton that refreshes a web viewer set to that URL.
https://coinbase.com/api/v1/prices/buy” and then parse it accordingly… might be the dirty way, but seems much simpler.[/quote]

Because once he gets this far, then decides to implement the other 98% of the API he would be stuck with the hell of manually parsing everything or he could just start right and then continue through to the remaining parts and it stays clean.