DEV Community

Kyle Knapp
Kyle Knapp

Posted on

Exploring API Protocols: From Conceptualization to Implementation

For the month of April I decided to do something different from my regular tech articles and embark on a project that I could work on over the course of a few days. After some consideration, I chose to create my own web API, a project in which is long over due for myself. Working with third party APIs daily at work its about time I create my own. In this article, I'll explain my journey of creating my first web API, the challenges I faced, and the lessons I learned along the way.

Understanding REST APIs:

Before we dive into my project, let's take a moment to understand what web REST APIs are and why they are crucial in modern web development. REST APIs, or Representational State Transfer APIs, act as intermediaries between different software systems, enabling them to communicate and exchange data. They are the backbone of countless web applications, facilitating seamless data retrieval, updates, and interaction between clients and servers. They have become a cornerstone in modern web development, enabling seamless communication between systems. They serve a multitude of use cases, such as fetching data, creating new records, and updating existing resources. Developers leverage the power of HTTP methods like GET, POST, and PUT to perform these operations efficiently. The beauty of REST APIs lies in their simplicity and scalability, making them an ideal choice for tech enthusiasts of all skill levels.

The Project

To kick this project, I recognized the need to address several key aspects before diving into coding. First and foremost, I determined the purpose of the API, which I concluded would be a movie API. This API would provide a list of movie titles along with their respective genres and indicate whether or not I had watched them. Additionally, I developed a basic HTML/JavaScript website to showcase the API calls in an interactive and meaningful manner (I will present the website later in this article).

Once the purpose was clearly defined, my next step was to select a programming language and framework to assist in building the API. After careful consideration, I opted for C# as my language of choice, with ASP.NET serving as my supportive framework. With the development environment in place, I commenced coding the logic for the API endpoints, enabling access to the underlying movie database.

During the endpoint creation process, I crafted several types of endpoints to fulfill different functionalities. I created POST endpoints to allow users to add movies to the list, PUT endpoints to update movie data, DELETE endpoints to remove entries, and a variety of GET endpoints to facilitate finding movie entries using different criteria such as title, ID, and watched status. Here's a brief code snippet to provide an idea of the concepts:

[HttpPut("/movies/{id}")]
public async Task<IActionResult> UpdateMovie(int id, Movie movie)
{
    var existingMovie = await _dbContext.Movies.FindAsync(id);
    if (existingMovie == null)
        return NotFound();

    existingMovie.Title = movie.Title;
    existingMovie.Watched = movie.Watched;

    await _dbContext.SaveChangesAsync();

    return NoContent();
}

[HttpGet("/movies/complete")]
public async Task<ActionResult<List<Movie>>> GetCompleteMovies()
{
    var movies = await _dbContext.Movies.Where(m => m.Watched).ToListAsync();
    return movies;
}

[HttpGet("/movies")]
public async Task<ActionResult<List<Movie>>> GetMovies()
{
    var movies = await _dbContext.Movies.ToListAsync();
    return movies;
}`
Enter fullscreen mode Exit fullscreen mode

After completing the code implementation, I proceeded to test the various endpoints using Postman. Through extensive testing and fine-tuning, I successfully ensured that all the API calls were functioning as intended.

Using Postman, I simulated different scenarios and input combinations to verify the correctness of the API responses. I tested the POST endpoints to add movies to the list, PUT endpoints to update movie data, DELETE endpoints to remove entries, and the GET endpoints to retrieve movie information based on different criteria.

Throughout the testing process, I made necessary adjustments and refinements to the code, addressing any issues or bugs that arose. By conducting thorough testing and refining the endpoints, I achieved a robust and reliable API that delivered the expected outcomes.

The Fun Part

With the endpoints tested and validated, it was now time to create a website to let the user manipulate the API to their liking. So I created a webpage using HTML/CSS and some JavaScript to make this website a bit lively. On page load the code calls the GET movies API to create a tile for each instance of a movie found, it then dynamically create a genre button for each different type of movie found that will later be used for sorting on the page. I thought about coding a search bar but I figured that I already put enough time in to this thing. The functionality is very basic in its current state but in the backend I made it super simple to build upon this page and add more endpoints and UI features to further increase its functionality! Below are some screenshots how it turned out!

Image description

When "Horror" is clicked only that genre of movies is shown
Image description

Closing Thoughts

Surprisingly, creating a web API turned out to be a smoother journey than I had anticipated. With the assistance of ASP.NET as my framework and a helpful YouTube tutorial, I was able to navigate through the process quite independently once I grasped the syntax. It became apparent that certain challenges, like scalability, backwards compatibility, and security, were certain concerns I luckily didn't have to worry about for this project.

However, this experience has opened my eyes to the potential complexities that can arise when dealing with larger-scale API implementations. I now appreciate the importance of thorough planning and consideration for these aspects in future endeavors. It's evident that the world of API development holds countless opportunities for growth and further exploration.

In conclusion, creating my own web API has been enlightening and empowering. It has not only expanded my knowledge and skills but also instilled in me a sense of excitement for future API-related projects. I encourage fellow developers to embark on their own API creation journeys, as the rewards in terms of personal growth and the ability to bring innovative ideas to life are truly worth it.

HERE IS THE CODE FOR ANYONE INTERESTED

Top comments (0)