private static async Task ApiKeys()
{
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
dynamic sg = new SendGrid.SendGridAPIClient(apiKey, "https://api.sendgrid.com");
string queryParams = @"{
'limit': 100
}";
dynamic response = await sg.client.api_keys.get(queryParams: queryParams);
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
Console.WriteLine(response.Headers.ToString());
Console.WriteLine("\n\nPress any key to continue to POST.");
Console.ReadLine();
// POST
string requestBody = @"{
'name': 'My API Key 5',
'scopes': [
'mail.send',
'alerts.create',
'alerts.read'
]
}";
Object json = JsonConvert.DeserializeObject<Object>(requestBody);
response = await sg.client.api_keys.post(requestBody: json.ToString());
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
Console.WriteLine(response.Headers.ToString());
JavaScriptSerializer jss = new JavaScriptSerializer();
var ds_response = jss.Deserialize<Dictionary<string, dynamic>>(response.Body.ReadAsStringAsync().Result);
string api_key_id = ds_response["api_key_id"];
Console.WriteLine("\n\nPress any key to continue to GET single.");
Console.ReadLine();
// GET Single
response = await sg.client.api_keys._(api_key_id).get();
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
Console.WriteLine(response.Headers.ToString());
Console.WriteLine("\n\nPress any key to continue to PATCH.");
Console.ReadLine();
// PATCH
requestBody = @"{
'name': 'A New Hope'
}";
json = JsonConvert.DeserializeObject<Object>(requestBody);
response = await sg.client.api_keys._(api_key_id).patch(requestBody: json.ToString());
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
Console.WriteLine(response.Headers.ToString());
Console.WriteLine("\n\nPress any key to continue to PUT.");
Console.ReadLine();
// PUT
requestBody = @"{
'name': 'A New Hope',
'scopes': [
' user.profile.read',
' user.profile.update'
]
}";
json = JsonConvert.DeserializeObject<Object>(requestBody);
response = await sg.client.api_keys._(api_key_id).put(requestBody: json.ToString());
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
Console.WriteLine(response.Headers.ToString());
Console.WriteLine("\n\nPress any key to continue to DELETE.");
Console.ReadLine();
// DELETE
response = await sg.client.api_keys._(api_key_id).delete();
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Headers.ToString());
Console.WriteLine("\n\nPress any key to exit.");
Console.ReadLine();
}
}