getting started with YNAB API in C# using RestSharp - how do I authenticate?

Anyone here have experience using RestSharp to talk to the YNAB API?  I think I see how the general operations will work, but am stuck on the authentication.

I successfully generated my API key and ran the curl command line example from the YNAB API website.

Just trying to translate that over to RestSharp so I can use the same API key there to authenticate.  If anyone has a brief code snippet for this I'd appreciate it.

1reply Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
  • Got it working!  Thought I'd post my code in case it helps others.  This mirrors the curl command line example given on the YNAB API page but in C# using RestSharp.  Of course for a real app, you may want to take a look at the RestSharp recommended usage page, they suggest making a proxy class and making data classes to map the JSON results.  But this quick and dirty example at least shows how to deal with the authentication (when using the API key method) for YNAB from C#.

     

            const string apiKey = "--- put your API key here ---";
    
            public void Run()
            {
                try
                {
                    var client = new RestClient("https://api.youneedabudget.com/v1");
    
                    var req = new RestRequest("/budgets", DataFormat.Json);
                    req.AddHeader("Authorization", $"bearer {apiKey}");
    
                    var resp = client.Execute(req);
    
                    Debug.WriteLine("got resp => " + resp.Content);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("oops! " + e);
                    throw;
                }
            }
    
    Reply Like 1
Like Follow
  • 1 mth agoLast active
  • 1Replies
  • 18Views
  • 1 Following