C# call REST client with x-api-key within grasshopper

This is a simple example calling https://yesno.wtf/api from a c# component in GH:

var request =  System.Net.WebRequest.Create(@"https://yesno.wtf/api");
request.Timeout = 1000;
request.Method = "GET";
try
{  
  using (var response = request.GetResponse())
   {
      using (var stream = response.GetResponseStream())
      {
        var reader = new StreamReader(stream, System.Text.Encoding.UTF8);
        Print(reader.ReadToEnd());
      }
   }
 }
 catch (System.Net.WebException e)
 {
    Print(e.Message);
 }

RestApiExample.gh (5.9 KB)

This example doesn’t need an api key. In your case, I suppose that would go in the headers:

request.Headers.Add("x-api-key", "your api key");
You might also want to add the content type:
request.ContentType = "application/json";

Finally, to parse the response, I’d recommend using Json.net and creating a class that matches the properties of the response. Something like:

public class OpenSensorMessage
  {
    public int date {get; set;}
    public int rssi {get; set;}
    public bool lowBattery {get; set;}
    public bool heartbeat {get; set;}
    public int lsnr {get; set;}
    public string id {get; set;}
    public string type {get; set;}
    public string deviceId {get; set;}
    public List<string> tags {get; set;}
  }