Getting Started with the Instagram API
14 May 2011
Just started to play around with with jQuery and the Instagram API. Basically I wanted to search for the a user using their username, get their ID and search for their photos using that ID. The following code seemed to do the trick:
var accessToken = '[your-access-token-here]';
var username;
var getUserID;
var limit = 6;
var setSize = "small";
var size;
var instagram = function() {
return {
initiate: function() {
instagram.getUser();
},
getUser: function() {
username = "[your-username-here]"
var getUserURL = 'https://api.instagram.com/v1/users/search?q='+ username +'&access_token='+ accessToken + ''
//console.log(searchURL);
$.ajax({
type: "GET",
dataType: "jsonp",
cache: false,
url: getUserURL,
success: function(data) {
//console.log(data.data[0].id);
getUserID = data.data[0].id;
instagram.loadImages(getUserID);
}
});
},
loadImages: function(userID) {
var getImagesURL = 'https://api.instagram.com/v1/users/' + userID + '/media/recent/?access_token='+ accessToken +''
$.ajax({
type: "GET",
dataType: "jsonp",
cache: false,
url: getImagesURL,
success: function(data) {
for (var i = 0; i < limit; i++) {
if (setSize == "small"){
size = data.data[i].images.thumbnail.url;
} else if (setSize == "medium"){
size = data.data[i].images.low_resolution.url ;
} else {
size = data.data[i].images.standard_resolution.url;
}
$(".pics").append("<a target='_blank' href='" + data.data[i].link +"'><img src='" + size +"'></img></a>");
}
}
});
}
}
} ();
$(document).ready(function() {
instagram.initiate();
});


