Skip to content

Instantly share code, notes, and snippets.

@bryancodesjs
Created January 21, 2022 17:58
Show Gist options
  • Save bryancodesjs/cc1f3ce63ad2c140c5a40a30b3e5e8a6 to your computer and use it in GitHub Desktop.
Save bryancodesjs/cc1f3ce63ad2c140c5a40a30b3e5e8a6 to your computer and use it in GitHub Desktop.
how to use Fetch to retrieve data from an API
//For this gist, I'm using a free, public API that shows information about a user's location based on their IP address
//here's where I store the API key I was provided with
const myKey = 'c00508dc4104c70518fcf49e17216676';
//here's a random IP from the internet
const myIp = '191.101.63.2';
//here's the base url
const myUrl = 'http://api.ipstack.com/';
function myRequest() {
//send the get request
fetch(myUrl + myIp +'?access_key='+ myKey +'&format=1')
//convert the json response to js object
.then(response => response.json())
//put the response in a variable called 'data' and do XYZ with it
.then(data => {
console.log(data);
})
}
myRequest();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment