C# Example

   Example generic function to handle the calls to the API:

private string Submit(string url, string method, string contentType = "application/json", string data = null) 
{ 
    string sUrl = string.Format("https://subdomain.simplekpi.com/api{0}", url); //Replace Subdomain with your account Name 
    string auth = "username:APIToken"; //Replace username with login email and APIToken with the token from your account 
    string sHtml = ""; 
    var wReq = (HttpWebRequest)WebRequest.Create(sUrl); 
    
    wReq.ContentType = contentType; 
    wReq.Accept = contentType; 
    wReq.UserAgent = "Generic Agent"; 
    wReq.Timeout = 45 * 1000; // milliseconds 
    wReq.AllowAutoRedirect = false; 
    wReq.Method = method; 
    wReq.AutomaticDecompression = DecompressionMethods.GZip; 
    byte[] authBytes = Encoding.UTF8.GetBytes(auth.ToCharArray());   
    wReq.Headers.Add("Authorization", "BASIC " + Convert.ToBase64String(authBytes)); 
    try 
    { 
        if (!string.IsNullOrWhiteSpace(data)) 
        { 
            wReq.ContentLength = data.Length; 
            var sReq = new StreamWriter(wReq.GetRequestStream()); 
            sReq.Write(data); 
            sReq.Flush(); 
            sReq.Close(); 
        } 
        var sResp = new StreamReader(wReq.GetResponse().GetResponseStream()); 
        sHtml = sResp.ReadToEnd(); 
        sResp.Close(); 
        return sHtml; 
    } 
    catch (WebException ex) 
    { 
        if (ex.Response != null) 
        { 
            var sResp = new StreamReader(ex.Response.GetResponseStream()); 
            sHtml = sResp.ReadToEnd(); 
            sResp.Close(); 
        } 
        else 
        { 
            sHtml = ex.Message; 
        } 
         
        return sHtml; 
     } 
}

 

   Usage of the function:

var html = Submit("/Groups", "GET");
Console.WriteLine(html );