Skip to content

Instantly share code, notes, and snippets.

@david-pm
Created May 7, 2018 03:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save david-pm/b878d342e6bbdf987061f2b772743818 to your computer and use it in GitHub Desktop.
Save david-pm/b878d342e6bbdf987061f2b772743818 to your computer and use it in GitHub Desktop.
Binance REST API - signed request in Ruby
require 'net/http'
require 'openssl'
# https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#general-api-information
# example curl request:
# `curl -H "X-MBX-APIKEY: #{api_key}" -X GET "https://api.binance.com/api/v3/account?recvWindow=100000&timestamp=#{current_time}&signature=#{signature}"`
api_key = '<replace this with your api_key generated on binance>'
secret_key = '<replace this with your secret_key generated on binance>'
request_uri = 'https://api.binance.com/api/v3/account'
current_time = Time.now.to_i * 1000
query_string = "recvWindow=5000&timestamp=#{current_time}"
signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), secret_key, query_string)
params = { recvWindow: 5000, timestamp: current_time, signature: signature }
uri = URI.parse request_uri
uri.query = URI.encode_www_form params
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new uri
reqquest.add_field('X-MBX-APIKEY', api_key)
response = http.request(request)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment