Working with APIs
Prof V
Working with APIs
Example: A RESTful API — NYTimes
4
5
Give the URL to reach the API. Should include the key needed to access it, your query, and any necessary parameters.
6
fetch() returns a promise. A promise may or may not be resolved. If a promise is resolved, then whatever is in then() will be executed-- this will also be a promise because it may or may not be executed based on whether the original promised gets resolved or not.
fetch() returns a promise. A promise may or may not be resolved. If a promise is resolved, then whatever is in then() will be executed-- this will also be a promise because it may or may not be executed based on whether the original promised gets resolved or not.
7
We need to return the JSON version of our results so we can pass it to our display function and access the fields more easily
We need to return the JSON version of our results so we can pass it to our display function and access the fields more easily
We need to return the JSON version of our results so we can pass it to our display function and access the fields more easily
8
This example wants exactly 10 articles returned
(remember: we want up to 15)
This example wants exactly 10 articles returned, otherwise it won’t display anything. If no results are found, it tells the user through the HTML
9
Once we have the results, we create the appropriate HTML elements
10
We then populate our newly created HTML elements with the info from the response JSON
Once we have the results, we create the appropriate HTML elements
11
And lastly, we append our newly created and recently populated HTML elements to our webpage
Unit Exercise
Let’s make a dynamic image gallery by using an image API!
Unit Exercise Prep Steps
13
Unit Exercise Helpful Hints
14
API Tutorial Videos