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
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>
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”
}
}
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.
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
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.
}
…var request = require(‘request’);
request(‘URL Here’, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
})
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/
Top comments (2)
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.
Well Explained