Hi all!
First, I’d like to thank @grt-s for their original post. Without this reference I’m not sure how long it would’ve taken me to figure out C# development and the Monday API. Successfully returning that first JSON response was very exciting and set me off on a list of projects.
Visual Studio kept bugging me that WebRequest.Create(string)
had become obsolete and should be updated to HttpClient
. After a few weeks, I finally got around to updating the helper and wanted to share it for any friends in the C# community who come across, or are still using the original, incredibly helpful resource.
Here’s the updated code, and I’ll explain some changes below. Please feel free to critique!
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace Monday
{
public class MondayAPI
{
private static readonly HttpClient client = new HttpClient();
private const string url = "https://api.monday.com/v2/";
private const string key = "";
private const string version = "2023-10";
/// <summary>
/// Get your data from monday.com.
/// </summary>
/// <returns>Response in JSON format.</returns>
public static async Task<string> Query(string query)
{
string prefix = "{\"query\":\"";
string suffix = "\"}";
string content = prefix + query + suffix;
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url))
{
request.Content = new StringContent(content, Encoding.UTF8, "application/json");
request.Headers.Add("Authorization", key);
request.Headers.Add("API-Version", version);
using (HttpResponseMessage response = await client.SendAsync(request))
{
return await response.Content.ReadAsStringAsync();
}
}
}
}
}
Version Swapping
With the upcoming release of version 2023-10
and it’s changes, easily swapping between API versions seems more important than ever. Changing the version
variable will allow you to run your queries on a specified version.
Query Simplification
Wrapping my head around the formatting of query input took me longer than I’d like to admit. The extra quotation marks and escape characters were a lot to look at. Putting those pieces into their own prefix
and suffix
variables helped simplify what you enter into the Query(string)
method call.
This also successfully tied together the API playground and the query
argument. Copying any query from the playground and flattening it: "{ workspaces { id name }}"
is all you need for the Query(string)
method call.
Use
Declaring the Query()
method as static
changed how the helper is used from the original post. Here is how you can use this version:
string query = "{ workspaces { id name }}";
string json = await MondayAPI.Query(query);
Expansion
This is just a base of what this class can do. You can easily customize and expand this class for your needs. A few examples I particularly like:
- Make the
version
variable settable to allow for API version flexibility perQuery()
- Add
complexity
oruser
queries to theprefix
variable to ensure queries consistently return useful data.
I haven’t gotten around to mutations or column values yet, but I believe this should also be a solid base for simplifying API mutations.
. . .
Hope this helps!