Binance has APIs for both fetching public data and for executing trades.
Data API
https://data-api.binance.vision/api
Testnet / Sandbox
Binance has a testnet/sandbox thing for writing your API code without touching your real account.
It is located at https://testnet.binance.vision/.
Authentication
You need an API key and a secret key. HMAC-SHA256 is used for signing the HTTP requests.
Here is a dotnet code snippet I’ve used for this before.
private static long Millis()
{
return DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
}
private static string SignUrl(string url)
{
var qry = new UriBuilder(url).Query;
if (!string.IsNullOrEmpty(qry))
qry += "&";
qry += $"timestamp={Millis()}";
var key = GlobalHost.Config["Binance:HmacSha256SecretKey"];
using var hmacCalculator = new HMACSHA256(Encoding.UTF8.GetBytes(key));
var hmacBytes = hmacCalculator.ComputeHash(Encoding.UTF8.GetBytes(qry));
var hmacHex = BitConverter.ToString(hmacBytes).Replace("-", string.Empty).ToLower();
qry += $"&signature={hmacHex}";
return new UriBuilder(url) { Query = qry }.ToString();
}
internal static async Task<HttpResponseMessage> GetAsync(string url)
{
var signedUrl = SignUrl(url);
var apiKey = GlobalHost.Config["Binance:HmacSha256ApiKey"];
using var req = new HttpRequestMessage(HttpMethod.Get, signedUrl);
req.Headers.Add("X-MBX-APIKEY", apiKey);
var resp = await HttpClient.Instance.SendAsync(req).ConfigureAwait(false);
return resp;
}
Resources
Some API docs
- https://github.com/binance/binance-spot-api-docs/blob/master/rest-api.md
- https://binance-docs.github.io/apidocs/spot/en/