Search code examples
c#rest-clientcoinbase-api

Access the Coinbase Candles url


I have successfully managed to call some of Coinbase API's end points, but struggling on calling the Candles Url:

        using RestSharp;
        using System.Globalization;
        using System.Net.Http;
        using System.Security.Cryptography;
        using System.Text;

        var symbol = "BTC-GBP";
        var _privateApiKey = "xxxx";
        var _secret = "xxxxxxxxxx";
        var _timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
        var _requestPath = "/api/v3/brokerage/products/" + symbol + "/candles";
        var _method = "GET";

        var client = new RestClient(@"https://api.coinbase.com" + _requestPath + "?granularity=ONE_DAY&start=" + DateTime.Now.AddMilliseconds(-3000).ToUniversalTime().Ticks + "&end=" + DateTime.Now.ToUniversalTime().Ticks + "");

        var request = new RestRequest();
        request.Method = Method.Get;
        request.AddHeader("Content-Type", "application/json");
        request.AddHeader("CB-ACCESS-KEY", _privateApiKey);
        request.AddHeader("CB-ACCESS-TIMESTAMP", _timestamp);
        request.AddHeader("CB-ACCESS-SIGN", GetAccessSign(_timestamp, _method, _requestPath, "", _secret));
        request.AddHeader("CB-VERSION", "2023-01-18");



        try
        {

            var response = client.Execute(request);
            Console.WriteLine(response.Content);


        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        Console.ReadLine();
        string GetAccessSign(string timestamp, string method, string path, string body, string apiSecret)
        {
            var convertedString = Convert.FromBase64String(apiSecret);
            var hmaccsha = new HMACSHA256(convertedString);

            string timestampStr = timestamp;
            string httpMethodStr = method;
            var prehash = timestampStr + httpMethodStr + path + body;

            // Convert the input string and key to byte arrays
            byte[] prehashBytes = Encoding.UTF8.GetBytes(prehash);
            byte[] keyBytes = Encoding.UTF8.GetBytes(apiSecret);

            // Compute the SHA-256 hash of the input data using the key
            HMACSHA256 hmac = new HMACSHA256(keyBytes);
            byte[] hash2 = hmac.ComputeHash(prehashBytes);

            // Convert the hash to a hexadecimal string
            string signedSignature = BitConverter.ToString(hash2).Replace("-", "").ToLower();

            return signedSignature;
        }

But i keep getting this error:

{"error":"INVALID_ARGUMENT","error_details":"start and end argument is invalid - number of candles requested should be less than 300 ","message":"start and end argument is invalid - number of candles requested should be less than 300 "}

Solution

  • You are missing the required arguments: https://docs.cloud.coinbase.com/advanced-trade-api/reference/retailbrokerageapi_getcandles

    Path Params

    product_id string required The trading pair.

    Query Params

    start string required Timestamp for starting range of aggregations, in UNIX time.

    end string required Timestamp for ending range of aggregations, in UNIX time.

    granularity string required The time slice value for each candle.

    The API probably just has a bad error message and treats undefined start and end range query params as out of range values.