How to make your first GET API call in C# .NET Core

Call your first GET request in .NET C# using parameters, header values, and map-to models in your application:

Daniel Wilkinson
BGL Tech

--

APIs are the unsung heroes of our connected world. Simply put an Application Programming Interface (API) is the messenger which accepts a request and soon after the same API returns a response back to you.

They have their own use cases. One example may be that you want to explore new recipes. You could, in theory, build up your own lists and save them to your very own database… or you could use an API which in turn can aggregate millions of recipes from different sources into one single source of truth. In turn that would allow you to sift through a huge pile to find the ones you like. Ones which you may not have known existed if you saved your own. 😬

In this article, we’re going to be talking about a special kind of request known as the GET request. Typically, we use these to request data. Wait, that doesn’t sound cool enough? Ok… Get requests can be used to request OAuth tokens that authenticate a user. They can be used to request data in different forms such as form data, XML, and JSON. There are all sorts of sweet things we can learn from a simple GET request.

— So let’s jump into it!

Alright, so firstly let’s set up. I’m going to head on over to Visual Studio and create a little Console App (.NET Core). I’ll name it… Fooch. A clear winner. A nice blend of food and search. 😆

Now that we’re set up, let's grab our API. We’re going to be using Spoonacular API as they brag about having over 5,000 recipes. I think that should be enough, right?

So what I’m going to do now is just read some documentation and sip on my coffee whilst I do so. My goal is to find the API endpoint which we’d want to use in our app. Ah yes, here it is. The endpoint is called ComplexSearch. It has a whopper of a parameter list which makes it a good shout for what we’re looking for.

Spoonacular also, quite surprisingly, only uses an API Key as their form of authentication. We’d typically find that we’d have to request OAuth tokens and in that case, I’d recommend going here and finding out how to implement those, but… oh well! Let's create an account and grab our token.

Finally, before we leave this site for a while let's look at the parameter list, I want five names of recipes to explore instead of the default 10. Looks like they called that ‘number’ so let's keep that in mind as we move on.

Pro tip: Before implementing the API endpoint in code let’s open up postman, a free API caller, and ensure we can call this endpoint correctly. That way we know exactly how to structure our request and models in the code.

Interestingly, Spoonacular has a button on their site which we can use to grab all the endpoints and put them in our Postman. Let's absolutely do this.👍

Where to find the Postman button on Spoonacular

After finding the ‘Search Recipes’ endpoint in the ‘Recipes’ folder I removed all of the parameters I don't need and added them in my API key. I then checked to make sure this was a GET request and clicked send. The API responded with a single recipe as we entered in the number parameter as 1.

The parameters of the url

We have our first response! What on earth are pizza bites with pumpkin?! I thought pineapple on pizza was controversial enough!? This is a whole other ball game! 😛

Response of the request for the API

Right, let's write some code! I’m going to create a static class called Consts.cs where I am going to put my token so I can access this anywhere in my application.

Class of Consts.cs

Then I am going to create my model based on the response above. That means I am going to need a ‘Recipe’ object. I’m working bottom level in the JSON up at this point creating the property ‘Title’ as this is all I need.

The layout of the Recipe class

Next, I’m going to create an object to represent the JSON array. I’ll call this ‘RecipeList’ with an IEnumerable of type recipe (The previous object). The response above calls this ‘results’ but I want to map this to something that makes sense in our context so I’ve named this RecipeList.

The layout of the RecipeList class

Finally, let's write the code that does all of the juicy stuff! I’m going to create a class called SpoonacularService and implement the interface. It’s going to be asynchronous as we are unsure as to how long the API call will actually take.

At the moment we want five names of recipes but, in the future, we might want to extend this to return 100 recipes, all of which have their own information.

Layout of the SpoonacularService class

OK, so let’s break this down. I am firstly creating my base URL and adding my parameters into a separate variable. This would be handy if we were building up a lot of parameters in another method for example. Then, we’re creating our HttpClient and ensuring that it knows what is’s base URI is. We’re also telling the client that we’re expecting our result as a JSON string. We’re calling our API with the GetAsync() method and placing that into a nice little variable.

Pro tip: You can easily check if an API call has successfully executed and returned with a 200 OK by adding some conditional logic and checking the IsSuccessStatusCode boolean flag.

Upon checking if the response sends us a successful status code we’re then reading the body of the response as a string which will then be deserialized using Newtonsoft.NET in order to convert this into our local models.

Calling GET5Recipes

Then we simply add our model to a list and return that to the frontend where we can see below that our pizza recipes are now on show!

Recipe names!

We did it! We managed to call our first GET request in .NET C# and it only took 10 minutes! We looked at how to use parameters, header values, and map to models in our application. We saw how to use postman to speed up development and looked at what an API actually is.

To clone the repository you can head here:

Further Reading:

--

--

Daniel Wilkinson
BGL Tech

Daniel Wilkinson is my name and Software Engineering is my game. I'm also a coffee Enthusiast but I couldn't find a way to make that rhyme. 😂