I'm trying to use Binance's API. The following works:

URLExecute[ <|"Scheme" -> "http", "Domain" -> "api.binance.com", 
                   "Method" -> "GET", "Path" -> "/api/v1/time"|>]
(* {"serverTime" -> 1519522573487} *)

but I would like to use the socket instead. I tried

socket = SocketConnect[{"api.binance.com", 443}]
WriteString[socket, "GET /api/v1/time"]
message = SocketReadMessage[socket]

but the last command never stops (even without the WriteString). I also tried replacing the first line with socket = SocketConnect["stream.binance.com:9443"], see binance doc, with the same result.

What is wrong and how can I connect to the socket to return, e.g., the server time or last orders?

Related: 26875 but does not use Mathematica's latest features and 165297.


Please try:

socket = SocketConnect[{"api.binance.com", 443}]
WriteString[socket, "GET /api/v1/time \n"]
message = SocketReadMessage[socket]

note the "\n" message returns a bytearray. Without the "\n" it hangs. I am not familiar with the binance API but just generally sometimes one or more carriage returns are necessary in HTTP headers.


Returns a 400 error page. I am assuming you are looking for a timestamp.

  • $\begingroup$ At least I get an error now... I also get a 400 Bad Request error with WriteString[socket, "!ticker@arr\n"] (even without \n) but the string comes from the doc. And yes, I am looking for a timestamp or anything that shows that the connection with the socket is working, such as any live data from the website. $\endgroup$ – anderstood Feb 27 '18 at 18:17
  • $\begingroup$ I would say an error shows that the connection or parts of it is working, The remainder maybe how to send proper header and payload to get back the proper response. I was only trying to get you past the hanging. The link you provide does not help much as I see no info on the time part of the api. $\endgroup$ – Hans Feb 27 '18 at 18:36
  • $\begingroup$ I see. There is another doc which is not dedicated to sockets, that mentions time in section "check server time". $\endgroup$ – anderstood Feb 27 '18 at 18:38
  • $\begingroup$ I am not sure that SocketConnect is setup to do secure communications or the handshake that is required for https. $\endgroup$ – Hans Feb 28 '18 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.