DEV Community

Cover image for Week 8: Working with APIs
Code_Regina
Code_Regina

Posted on • Edited on

Week 8: Working with APIs

This week was Working with API’s from Colt Steele The Web Developer Bootcamp.

 -Intro to API’s 
 -JSON and XML 
 -Making API Request with Node
 -JSON Placeholder API Example 
Enter fullscreen mode Exit fullscreen mode

Intro to API’s

API is known as Application Programming Interface.

API’s are used to get preexisting data to use within multiple projects. API's are just basic interfaces for code/computers to talk to one another.
In web development, web API’s work by communicating with the HTTP protocol.

JSON and XML

API’s don’t send information back as HTML, because HTML contains information about the structure of a page. API’s only send pure data back as a response, it does not send the structure of that data therefore, data formats are used to make the data easier to read. Data formats are used to make HTML data easier to read. There are two types of formats that are used which are known as JSON (JavaScript Object Notation) and XML (Extensible Markup Language).

XML is syntactically similar to HTML, but it does not describe presentation like HTML does. All it does is provide key-value pairs

<person> 
<age>21</age>
<name>Travis</name>
<city>Los Angeles</city> 
</person> 
Enter fullscreen mode Exit fullscreen mode

JSON is JavaScript Object Notation it is a cleaner more organized way to format HMTL data than XML. JSON also utilizes key-value pairs.

{
“person”: {
“age”: 21, 
“name”: “travis”, 
“city”: “Los Angeles”
}
}
Enter fullscreen mode Exit fullscreen mode

JSON is more common than XML because it can be utilized in normal JavaScript programming.

Making API Request with Node

Utilizing a Request HTTP client
It is possible to make a API request from a JavaScript file.
First, a request must be made to the URL of that API.
During the time that this course was created, a now deprecated code base was used.

GitHub logo request / request

🏊🏾 Simplified HTTP request client.

Deprecated!

As of Feb 11th 2020, request is fully deprecated. No new changes are expected to land. In fact, none have landed for some time.

For more information about why request is deprecated and possible alternatives refer to this issue.

Request - Simplified HTTP client

npm package

Build status Coverage Coverage Dependency Status Known Vulnerabilities Gitter

Super simple to use

Request is designed to be the simplest way possible to make http calls. It supports HTTPS and follows redirects by default.

const request = require('request');
request('http://www.google.com', function (error, response, body) {
  console.error('error:', error); // Print the error if one occurred
  console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
  console.log('body:', body); // Print the HTML for the Google homepage.
}
Enter fullscreen mode Exit fullscreen mode

var request = require(request); 
request(URL Here, function (error, response, body) {  
if (!error && response.statusCode == 200) {
console.log(body)
}
})

Enter fullscreen mode Exit fullscreen mode

Although this course may be a slightly outdated, I believe that the concepts remain relevant.

JSON Placeholder API Example

JSON Placeholder is dummy data that is made up to use in any application.
Free JSON data to use in any projects.
https://jsonplaceholder.typicode.com/

Overall, I have learned how valuable API's are to web development. It is important to consider preexisting data to work with that way a solid foundation can be built upon with whatever the current project is at the moment. So much data is widely available and made easy to use with a simple API.

Top comments (2)

Collapse
 
leastbad profile image
leastbad

I suggest that you check out the Twilio SMS and voice APIs. For me, Twilio was really mind-expanding in terms of stuff I realized I could now do easily. Just sending text messages from your web page is super cool, but realizing that you can set up a phone number for $1/month and have people dial in and press 1 to call a URL on your Express (or Rails!) back-end is totally wild.

Collapse
 
gunishmatta profile image
gunishmatta

Well Explained