I know i keep saying it, but figuring out how to implement the API was easily the most difficult thing I’ve done so far. I had to combine information from a few sources, and still hack things together to get everything to work. One of the first things I have to say if is you are ever in my position use the JavaScript fetch API. One of the first “best answers” I came across on StackOverFlow had the correct code for a query, but requested information from an API using an XML request. XML stands for Extended Markup Language, and from what I recall extends HTML. Its purpose is largely to help tell applications where to find elements of a layout and keep track of those elements’ properties. This solution flew in the face of a few good programming principles.

First, you should try to change data types as little as possible. One reason is that not every data type is the same size, and that can mean that accuracy is lost in translation. Switching between programming languages can also be as jarring as someone switching between spoken languages. However, the cost is slower performance instead of difficulties communicating. The final issue that I can think of is the fact that it took a “key”:”value” relationship and turned it into a sentence. The textbook definition of existentialism is a bit different than the definition you’d get from a drunk guy, and honestly the end result of using XML here was about as effective too. Luckily, JavaScript does have some functions for handling data from APIs.

JSON is a JavaScript based language meant for organizing and transferring devices across machines. Where XML keeps tracks of elements and their properties, JSON keep tracks of “key”:”value” pair relationships. Luckily, JavaScript will let you fetch your information straight from the API. Therefore, as long as you send the information as a JSON object you can easily retrieve it with no extra work on the client side. The code below shows a much simpler solution than trying to use XML, and one that works very well.

JavaScript Fetch() for API calls

Leave a comment

Your email address will not be published. Required fields are marked *