What’s an API?

Before attending Flatiron’s online coding bootcamp, I worked as an Operations Analyst for a clean tech company. Although writing SQL queries were the closest I got to actual coding, I was constantly surrounded by tech jargon. I remember hearing lots about API’s. They seemed fragile, complex, hard to define. However, after adding Javascript onto an existing rails project, and enabling internal API use, I have a clearer understanding of what an API is and its use.
What’s an API?
An API is an end point to request data. The data is typically in JSON (Javascript Object Notation) format, reading like a dictionary with its key value pairs, making it very intuitive to read. Data is processed from the endpoint through the browser and can be viewed in the console before it is added to the desired destination, or page. Viewing the data in the console is very helpful because you can play around with the data with Javascript in the browser’s console— testing code to access the points of data that are important to your application.
Internal vs External API’s:
I always thought as API’s as exclusively external. My goto example of an external API is Google Maps. For example, my local pizza shop has Google Maps embedded into the user interface of their website. Luigi’s connects to Google Map’s API so pizza lovers can see the restaurant location when they go onto the site.
However, in the context of my project, I wasn’t in need of external API’s, I wanted to render data that was available within my application. The theme of the project was to render data via AJAX. For example, after a user click ‘View Class Plan’ the class plan will be rendered from the API endpoint, and there will not be a page refresh.

The API endpoint looked like this:

I then wrote the following code in order to render the data from: http://localhost:3000/yoga_classes/6/yoga_class_data and append it to the yoga_classes index page: http://localhost:3000/yoga_classes
“$(function() {
$(“.js-more").on(‘click’, function(){
let id = $(this).data("id");
$.getJSON("/yoga_classes/" + id + "/yoga_class_data", function(data) {
$("#body-" + id).append("

" + data["class_plan"] + "</p><p> Created
by: " + data["user"]["email"] + "</p>")
});
});
}); “

Breaking down the code:
Line 2: is saying for class “js-more”, which was assigned to the “View Class Plan” button, when the user clicks the button the following should happen…
Line 3: assigns a variable id to the yoga_class id so that in line 4 the yoga_class_data URL can be dynamic.
Line 4: is where the magic happens. The JQuery method $.getJSON takes two arguments the, the dynamic /yoga_class_data url and a function…
Line 5: that function takes in data (which represents JSON data of a yoga_class) as an argument and appends (adds) the class plan and user to the class ID body-[id], this case body-6, which is located underneath the yoga class title on the page http://localhost:3000/yoga_classes.
Note: I also used Active Model Serializers gem which grants the backend ability to serialize data with the necessary attributes and relationships. .
When Learning and Applying Something New, Pace Yourself:
At the end of each module for Flatiron’s Online Software Engineering Immersion course we are required to build a project which culminates everything we learned the 3 works prior. We cover so much material in 3 weeks that when project time comes around I don’t feel comfortable with the material initially. Each project week I look over the scope of the project and immediately the requirements seem daunting. The Javascript, APIs, and AJAX section was no different. Instead of remaining in a state of resistance I decided to breakdown the project. We had 5 requirements, so my goal was to complete a requirement per day, leaving two extra days for pesty bug fixes or more time in case a certain spec took longer to tackle. This proved to be a great method. I managed to finish my project in 4 days. If I was able to meet my requirement for the day, I continued working. I find that learning happens best when spread out over multiple sessions, so working on a requirement (each requirement tackles a slightly different concept) over 2 separate days solidified my learnings.
Learning a second language was tough, but I learned why it’s necessary. AJAX requests make it quicker for a user to interact with my app. It gives the user the ability to request more data without having to load a new page, which is the modern experience of the web. I’m looking forward to how the concepts of AJAX apply to Redux and React, the last module for the coding bootcamp!

Link: https://dev.to/mccarronmollye/what-s-an-api-d6