A common task for a developer when working with or building an application is implementing and interacting with APIs.
API is an acronym for Application Programming Interface which is a software intermediary that allows two applications to talk to each other.
There are many different types of APIs. One of the more common types is a Web API, these are often called Web Services. They provide an interface for Web applications and allow applications to connect and communicate to each other via the internet. You can create your own API for others to interact with or you can interact with others API
There are thousands of public APIs that can be used. These do things like check for weather updates, get and confirm address details, make online payments and update social media status.
An example of an API in action - if you think of any website that you have visited where you are made to fill in your address details, you start by typing your street number and name. Then, a list of addresses are displayed, you select an address and the rest of the fields are populated. Chances are that the website is using a 3rd party Web API that calls off to an address service to get you a list of known addresses that match your search criteria.
While there are many different types of APIs, this post will focus on Web APIs.
The example we are going to implement is a Giphy API https://giphy.com/. Giphy is a website that stores GIF images, when a user enters provides Giphy with specific search criteria, Giphy will try and match that to an image. This API allows us to send an Http request and receive back a GIF image URL.
What we will be covering- Create a Simple ASP.NET Core 2.0 Web API project
- If you are unsure of how to create a blank .NET Core 2.0 project, then visit my YouTube channel where I show you how this is done ASP.NET Core 2.0: Building a Simple Web API
- If you are unsure of how to create a blank .NET Core 2.0 project, then visit my YouTube channel where I show you how this is done ASP.NET Core 2.0: Building a Simple Web API
- Search and read the API (Giphy) documentation
- Create our Controller
- Dependency Injection
- Project Structure
- Creating the Model
- Creating a Service Class
- Creating the GetRandomGif class
- Running the Application
In order to start implementing our Giphy API, we need to create a blank .NET Core Web API project.
I’ve created a YouTube tutorial ASP.NET Core 2.0: Building a Simple Web API that goes through how to create this. If you need help creating the Web API, then follow that tutorial before continuing on with the next step.
Read the API DocumentationBefore we start implementing anything, it’s really important to make sure we have read the APIs documentation. The API documentation contains instructions about how to effectively use and integrate with the API.
Good API documentation will tell you about usage restrictions, what kind of input the request needs, what the output of your request will look like and other implementation details.
Unfortunately, not all APIs have good documentation, you need to work with what you get and sometimes you need to do some exploratory learning.
By reading the API documentation, you can use your favorite HTTP request client to call off to the API and view the results. Currently, I am using PostMan. By ensuring that the request you make is returning you the results you expect mean that you have understood what is needed to make the request. It will also make it easier to implement into our solution.
The URL we will be using is http://api.giphy.com/v1/gifs/search?api_key={giphyKey}&q={searchCritera}&limit=1
Giphy Links
- Website: https://giphy.com/
- Developer Website: https://developers.giphy.com/
- Giphy Documentation: https://developers.giphy.com/docs/
Creating our Controller
First we add a controller that returns OK.
We then add the route [Route("v1/giphy/random/{searchCritera}")] this will allow us to hit our endpoint and pass in a value, the value will be what we want to search on.
- [Route("v1/giphy")]
- public class GiphyController: Controller {
- [HttpGet]
- [Route("random/{searchCritera}")]
- public IActionResult GetRandomGif(string searchCritera) {
- return Ok();
- }
- }
Once we have our basic concept of what the controller and method will look like we will then be following a coding style called Wishful Programming.
Wishful programming basically means that we write code with the idea that we already have classes and methods available to us, even if we haven’t created them yet. We then create these classes and methods when we need them.
Let’s try that. We know that the controller is going to call off to a GiphyServices class, so we can write the following inside our controller.
- [HttpGet]
- [Route("v1/giphy/random/{searchCritera}")]
- public async Task<IActionResult> GetRandomGif(string searchCritera)
- {
- var result = await _giphyServices.GetRandomGifBasedOnSearchCritera(searchCritera);
- return Ok(result);
- }
- }
Throughout this code, you will see that I will be using the async and await keywords. Asynchronous programming in C# requires a full blog post of its own. As such, I won’t go into too much detail about what it is doing. If in the meantime, you would like to know more about Asynchronous programming, then you can read more about it here.
Dependency Injection
We will be using ASP.NET Cores inbuilt dependency injection framework. I wrote a blog post a while back explaining a bit about dependency inversion, injection, and control, you can read it here Dependency Inversion
MSDN also has a good article Introduction to Dependency Injection in ASP.NET Core that is directly related to ASP.NET Core
A very simplified explanation of dependency injection is that in order to access and use methods inside classes we need to instantiate the class, we can do this by newing up the class i.e var x = new GiphyServices(); or making the class static.
Dependency injection in the way we will be using it inside our Giphy project will allow us to instantiate our classes at startup and then inject them into the constructor of each class that needs them.
Remember this is a very simplified explanation of why we are using dependency injection, it has many other uses as well.
In order to use and implement Dependency Injection, we need a class and an interface for our class i.e IGiphyServices and GiphyServices, we then add the following code to the startup.cs under the ConfigureServices method.
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddSingleton<IGiphyServices, GiphyServices>();
- }
- private readonly IGiphyServices _giphyServices;
- public GiphyController(IGiphyServices giphyServices)
- {
- _giphyServices = giphyServices;
- }





Hemangi ChavanPosted Jul 30, 2020, 7:53 AM
Helpful Share , Thank You @Mr.Daniel
Manav PandyaPosted Jan 23, 2018, 11:32 PM
Great share sir .........
Naresh SinghalPosted Jan 22, 2018, 11:34 PM
Nicely explained...helped.....S