Get Photos from Instagram using jQuery
This is the first post in post series about Instagram. There would be posts about working with Instagram API (using PHP and JavaScript) without difficult classes or other similar things. Just ready to use code.
And if you are looking for a tutorial about connection to Instagram API with pure JavaScript, check this out.
Now let’s come back to this post — it consists of three parts and different examples. As you could understand from its heading it is only about getting photos. Another API methods will be described in the next posts.
Instagram API changes. Access token.
How to Generate Instagram Access Token
For all the examples below a token is required. If you’re looking for a solution how to fetch Instagram feed without an access token, check this tutorial instead.
Below are just 4 steps that allows you to generate an access token:
- Log in to Instagram and open this page https://instagram.com/developer/clients/manage. Instagram may ask you some additional info – provide it.
- Click the button “Register a New Client”.
- We’re going to work with my tool, so in Valid Redirect URIs field specify url of the tool and click “Register”. Then click “Manage” button to obtain both Client ID and Client Secret of your just registered app.
- Use your Client ID and Client Secret to generate an access token with a special tool. You can find more info there as well.

A few facts about the Sandbox Mode
- All newly created apps start in Sandbox Mode
- Apps in Sandbox mode have access only to 20 latest media of an access token owner (and sandbox users invited to your app).
- To avoid these restrictions you should send your app to approval.
1. By User ID
In fact, it is the only example in this post that works properly in Sandbox Mode. For Sandbox mode change userid
to the access token owner ID or just to self
.
var token = 'your access token', // learn how to obtain it below
userid = 1362124742, // User ID - get it in source HTML of your Instagram profile or look at the next example :)
num_photos = 4; // how much photos do you want to get
$.ajax({
url: 'https://api.instagram.com/v1/users/' + userid + '/media/recent', // or /users/self/media/recent for Sandbox
dataType: 'jsonp',
type: 'GET',
data: {access_token: token, count: num_photos},
success: function(data){
console.log(data);
for( x in data.data ){
$('ul').append('<li><img src="'+data.data[x].images.low_resolution.url+'"></li>'); // data.data[x].images.low_resolution.url - URL of image, 306х306
// data.data[x].images.thumbnail.url - URL of image 150х150
// data.data[x].images.standard_resolution.url - URL of image 612х612
// data.data[x].link - Instagram post URL
}
},
error: function(data){
console.log(data); // send the error notifications to console
}
});
2. By a Username
Of course you can look for a user ID in Instagram HTML code each time.
But do not you think that it would be better to get user ID in the code automatically? Especially for your own plugin or application. It is much simpler for every single user of your app to specify a username, not a user ID.
This example isn’t supported by apps in Sandbox Mode.
var token = 'your access token',
username = 'rudrastyh', // rudrastyh - my username :)
num_photos = 4;
$.ajax({ // the first ajax request returns the ID of user rudrastyh
url: 'https://api.instagram.com/v1/users/search',
dataType: 'jsonp',
type: 'GET',
data: {access_token: token, q: username}, // actually it is just the search by username
success: function(data){
console.log(data);
$.ajax({
url: 'https://api.instagram.com/v1/users/' + data.data[0].id + '/media/recent', // specify the ID of the first found user
dataType: 'jsonp',
type: 'GET',
data: {access_token: token, count: num_photos},
success: function(data2){
console.log(data2);
for(x in data2.data){
$('ul').append('<li><img src="'+data2.data[x].images.thumbnail.url+'"></li>');
}
},
error: function(data2){
console.log(data2);
}
});
},
error: function(data){
console.log(data);
}
});
Can I get Photos of a Certain User Filtered by a Tag(s)?

3. By a Tag
This functionality is also very popular. Very easy to implement.
But if your Instagram app is in Sandbox Mode you can only get your own tagged media. And remember that your app has access only to 20 recent media. If you want to avoid these restrictions so much you could try to use my WordPress plugin.
Yes you can. Currently I do not have ready code for this, but it is in my task list.
var token = 'your access token',
hashtag='wordcamprussia2015', // hashtag without # symbol
num_photos = 4;
$.ajax({
url: 'https://api.instagram.com/v1/tags/' + hashtag + '/media/recent',
dataType: 'jsonp',
type: 'GET',
data: {access_token: token, count: num_photos},
success: function(data){
console.log(data);
for(x in data.data){
$('ul').append('<li><img src="'+data.data[x].images.standard_resolution.url+'"></li>');
}
},
error: function(data){
console.log(data);
}
});
Can I get photos by Multiple Tags?

Of course you can, but you should learn more about JavaScript arrays in that case — this requires to make an ajax request for every hashtag and then just rearrange the result data.
If you don’t want to dig into it — this functionality is also in my task list :)
If you have a question, please, leave it in comments — I will answer everyone.
Related

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
This is the simplest script I’ve found to get photos via the Instagram API, and your instructions were the clearest. Thank you!!
if i have a scenario like this : i have an instagram account, ex : @jonsnow. i want to create a web app using php, which can reply a comment with my account (@jonsnow) it is possible? my algorithm is first, i search a photo with a tag. than i want to get the photo caption, then i want to give/reply a comment with my account.
HI,
to do this, you need to submit your Instagram application for review and approval first.
I notice you say “If you won’t use WordPress, you can inspect the plugin code anyway”. I do not have a WP site but I want to mess with some of this plugin’s code in a personal product. 2 questions:
1. Can I definitely see the source code if I buy the plugin to copy and alter for my own app?
2. Will I still be restricted by the sandbox return results?
Hi Joe,
1. Yes, you can do it, but in the latest version of the plugin it is not so simple as before ( you will need good knowledge of PHP and PHP/JS beautifier ).
2. No you won’t be restricted.
Awesome post, the better one I have found that explain and help to get the Access token and without the need of other libraries.
Do you know if Is it necessary to submit the instagram app for review if you are only using it to grab the most recent photos from the instagram account to show them in the company website?
Thanks.
It is not necessary if you need to display no more than 20 recent photos.
Hi
It’s possible to send a Image and hashtag to the user timeline?
thanks
Hi,
I’m afraid this is not possible even with approved Instagram app.
Ok. Thanks!!
This is not Javascript. This is JQuery. If we wanted JQuery, we would’ve come looking for “JQuery.” The title is misleading.
JQuery is simply a Javascript library, so yes this is Javascript
That’s amazing article as I have know. Thank you so much.
This still work ?
Yes, and you can find working codepen demo on this page :)
Hi,
When I follow the instructions and attempt to get an access token I receive this error:
“code”: 400,
“error_type”: “OAuthException”,
“error_message”: “Invalid Client ID
Why is this happening?
Thanks!
Hello,
this means that you input incorrect Client ID.
Is there a way to fetch my account’s liked photos/videos?
Thanks!
Hi Marcelo,
yes, you can just add the IF statement:
Can this work for Sandbox accounts?
Its not working for me.
According to the API I can implement likes within sandbox but a limit 30/hour
im not sure how to incorporate the URL to get my likes
https://api.instagram.com/v1/media/{media-id}/likes?access_token=ACCESS-TOKEN
with your code provided.
Any help would be appreciated.
Hi Has,
this endpoint
https://api.instagram.com/v1/media/{media-id}/likes?access_token=ACCESS-TOKEN
doesn’t work with sandbox account.OMG the issue was my function was data NOT data2 as exampled!
But I still have one question.
This is only controlling the media I have personally posted on my account and liked (my own content).
I thought and would like to show any media (pictures) on Instagram I have liked to show up in my feed.
Is that possible? I have tried modifying the URL parameter to no success.
If you want to show all media you liked you need to use this endpoint
https://api.instagram.com/v1/users/self/media/liked?access_token=ACCESS-TOKEN
but it also requires public_content scope to be approved.Great tutorial. I really enjoyed it. :)
I was wondering if you had the ability for a search function to populate images via hashtag?
*For the user to input themselves that is (Sorry for not clarifying before). Thanks.
Hi Valerie,
My plugin can get images by hashtags, currently it can not filter user images by a specific tag (well, it can, but this functions doesn’t work as expected), but I’m working on it now :)
hi developer, thank for your job.
3 questions for you
1) I did understand why in Valid Redirect URIs I need to specify this url https://rudrastyh.com/tools/access-token ? I can’t put my website url http://www.exemple.com ?
2) I would like to view pictures about other public account?i s it possible ? Or just own account ?
3) at the end, is it possible found results by hashtag from all profile. I would like to create an aggregator by instangram hashtag
thank you Rud
Hi,
1) Try to put your website there and look what happens :) It is required to specify url to the tool, after receiving Access Token you can change it if you want I suppose.
2) Other accounts are possible in two cases: a) approved access token b) using my plugin.
3) Profile pictures filtered by a hashtag currenly is not supported.
thanks for your answers. I will see what to do
does it possible nowadays for me to show timeline photos based on hashtags, example #helloworld ?
because even snapwidget(instagram timeline viewer for website) remove their ability to showing all photos based on hashtags
Hi,
you can do it using this plugin.
emm, im a backend developer, i need something like instafeed.js but i think it doesnt work nowadays after 1st june, i wonder if i can grab feed based on hashtags in json data…
anyway, thanks for the help :)
Its not working.. :(
It is working, sorry!!
This is a fantastic little tool. Thanks for creating it.
Hello Misha,
Do you know how to search for tags with Ruby?
Hello Kel,
I’m not familiar with Ruby, sorry :)
is any option to move my image with some rotation with change position.
Can you please clarify what you mean?
Hey Misha, thanks a lot for this tutorial.
I have a problem tho, I get the acces token with my personal account, but when I want to retrieve a feed by hashtag, I get an empty array.
The same happens when i search for a random user, even when their accounts are public.
I have copied and pasted the code exactly as you show here, replacing only the token
Hi Leo,
since June 2016 Instagram made some changes in their API, if your application is not approved, you can get ONLY 20 latest photos of your account (the owner of the access token).
But you can consider using this plugin – it allows to get photos by tags and from any public account without an access token.
When you create an application to get a token on instagram, it is created on sandbox mode. When on sandbox mode, you can only query your own content, and you’ll only get 20 results.
You cannot submit the application for approval, I mean you can but it’s worthless, as any useful scenario won’t be approved. For instance, searching for hashtags and displaying content on your website.
So, if you create your token and query the instagram api for pictures tagged with #somehashtag
https://api.instagram.com/v1/tags/somehashtag/media/recent?access_token=[token]
it will only return images tagged that were published using your account. If you don;t have any, you’ll get an empty array.
sorry, when i get myself’s user detail, i get results, but when i want to get other’s user detail, it return error with 404.l
Hi Frank,
please read two comments above. There is an answer.
Hey!
Thank you so much for this explanations, but yet since I’m quite new to the programming world I’m not getting the result I want.
I made a instagram token, and my objective is put that on a website I made, specially in a page that I created.
I tried to copy the javascript code, but I can’t get any results when I paste it.
Any ideias to resolve this?
Much appreciated
Hi Carolina,
I think you just forget to include jQuery library.
Hey! Great post :)
I encountered a problem when trying to access the pictures posted with a tag from a specific account. I got back an OAuthPermissionException that I’m not sure how to fix. I thought it might have something to do with the security settings in the client management on instagram.com/developer but even after unchecking “Disable implicit OAuth” I still get the exception.
Any idea about what I’m missing? My URL looks like this https://api.instagram.com/v1/tags/MY_TAG/media/recent?access_token=MY_ACCESS_TOKEN
Thanks either way :)
Hi Lucia,
thank you :)
the answer to your question is in this comment.
Thank you! I went through some comments but I missed that one apparently :D
hello, how i can show location of tagged photo on map?
Hello,
I recommend you to use location information from the response —
data.data[x].location. latitude
anddata.data[x].location.longitude
.Hello
Thank you i will try
hi, I have been able to put your code into my website and it works really good.
However, can you help me showing the photo caption and the location as well (even as a text only)?
thank you.
Hi,
data.data[x].caption.text
— for caption text,data.data[x].location.name
— for location text,info about location coordinates is in my above comment.
I just tried it, it shows a broken image file, I guess it has something to do with the “img src=”. I honestly don’t know. If you will you can check on my web how it turns out: http://fazr.in and tell me what do you think.
thanks for your prompt reply
I found this piece of code on your website:
Interesting to know, why you’re trying to set location name (such as Big Ben) as an image src?
Working codepen example is above. It is easy – use it to find out what to do.
Got it lol, thank you
Incredibly helpful, thank you so much!
I am having a problem with the javascript, though. I only know CSS and HTML, and on my site, some other elements use the tag, so my instagram feed has been showing up everywhere is used. How might I change the code so that my feed only shows in the one instance of ?
Thanks in advance!
Hi Katie,
use element ID, so, add in HTML something like
<ul id="your_id">
and then in JavaScript use accordingly$('#your_id')
.That solved it, thank you so, so much! :)
I also benefitted from this comment. Thanks a lot Misha!
Hi Misha, solid plugin.
I’m looking to make a button with command to refresh my list of instagram pictures using your basic sandbox example code above. But I can’t figure it out. I’ve come across listview refresh command but I’m assuming it had something to do with refreshing the URL provided in the ajax code.
So I don’t want to page to refresh, just the content dynamically on the press of a button or even automatically after a set amount of time.
Any help would be appreciated.
P.S – I was wondering if you would release this plugin for non WordPress use so I can use it in my case a phone gap app I’m developing.
Hi Has,
currenly it doesn’t, but I plan to add it in the next version. But not sure that the plugin will be for non-WordPress :) WordPress one love.
Hi,
I did create CLIENT ID and got token access,but when I tried your code and replace it with my token access I got nothing. here it is:
what would you advice me to do please?
It is working! that was my bad, I forgot to close the script tag and to include J Query library.
Thank you for such the great tutorial
Hi Hussein,
ok, I’m glad that everything is working now :)
Hi Misha,
Can you please help me Facebook,how i get facebook URL to numeric id?
Hi,
I’m not sure about the correct way to do it, but when I need to get my profile ID, I usually right click on my cover image in profile, then Inspect, then in the source code I’m looking for
fbid
parameter :))For me it takes less than a minute.
Hi Misha,
I want to fetch only Instagram profile picture by username for my website.Is there anyway to find it means user put Instagram username and and i fetch information about user like user profile picture and name.In your code access token work only for my profile any option for other
Hi Jusis,
Yes, there is a way to get information for other users, but you have to approve your application in Instagram. This endpoint
/users/{user-id}/?access_token=ACCESS-TOKEN
.If you want to get info by username, add also this
/users/search?q={user-name}&access_token=ACCESS-TOKEN
Do you have a version this code snippet without jquery? That would be awesome :-)
Hi Richard,
no I have not, but this is a good idea for a new post :)
If you have time to wait until next week (I publish posts on Tuesdays), so, when the post will be ready, I will let you know.
Hi again,
here is the new post about pure JS Instagram API connection :)
Hi Author,
The code is working fine for me. But I want the live feed with the text,comment and post details. How to do that? Can you help me to sort out this issue?
Hi,
use
console.log( data );
function to see all the returned values in your browser console. If the received information is not enough for you, it means in most cases that your app should be submitted for approval. Proceed to the official Instagram API reference then.Am I able to get latest 20 photos for any instagram user or only just my account. Does the api allow getting any users photos or just your own.
Hi Brian,
the answer.
If I understand correctly. You can but only if instagram approves your app. And that is what happened with your wordpress plugin. Am I right ?
Do you know if it is more difficult now than it was in 2015 to get your app approved ?
Yes, you’re right.
I do not know much about approvals.
Thank you very much Misha !
Hi Misha,
your code is awesome. but is there a way that when you click the image it will redirect back to the instagram page and original photo?
thanks in advance
Hi,
of course, here it is:
Ya, this is working. Thanks.
You are a rockstar, thank you for sharing this! I had been searching for way too long trying to find a way to link the images and it was the smallest bit of code missing. Thanks again!
I’m using sandboxed mode apps,instead of userid what we need to mention?
https://api.instagram.com/v1/users/self/media/recent
orhttps://api.instagram.com/v1/users/self
depending on what you want to get.thanks, it works.
{“error_type”: “OAuthException”, “code”: 400, “error_message”: “Redirect URI does not match registered redirect URI”}
Thanks!! Its work great !!
Thanks for the code works a charm, added 2 elements
data.data[x].likes.count
data.data[x].comments.count
My question is, how can I add a load more posts button, because its only rendering 33 for max, even if I put 200, I know I have to send APP to aproval, any chance a tutorial for this would be nice.
Thanks in Advance
Rodrigo
You can try this tutorial. Hope it helps.
Thanks for the quick replay, try it and worked perfect, it would be nice to make that ajax call in a button, i’ll try to figure it out by the time and i’ll post it if I get it.
I had to duplicate 3 times more the ajax to get 132 images. ‘:)
Ok, good luck :)
Hi rudrastyh,
Thanks for the post. It is really useful and great.
But there is something on my mind, can we get another user id from username?
And then get their media from that username?
Thanks,
Cheers.
Hi,
Yes we can, and here is the example of that.
The Misha,
Thanks for the post, but what about security? Instragram says: “The token is unique to a user and should be stored securely”. But in your examples you just use it in javascript on the client side, so it’s basically public for everyone? Is this bad thing or am I wrong?
Hi,
If you use Sandbox token it is ok to use it in JS, otherwise consider this way or this.
Thanks for this post. May I know how to fetch images from instagram using the combination of user and tags. Your help is greatly appreciated.
Hi Joefrey,
Instagram sandbox limits us with it, so you can just get the latest 20 photos by user and after that filter the result by
tags
response parameter (it is an array, even if contains only one tag).Thank you for the great article!
I`m trying to pull, lets say, a quantity of comments for the latest post done.
But i get a console error “Uncaught TypeError: Cannot read property ‘count’ of undefined”
in the line where i set var postdate
I dont get why it cannot see data path properly?
Welcome!
It looks like you forgot about:
But i dont need photos)
I need likes-count, or post date\time.
Here`s a fiddle
https://jsfiddle.net/CapablancaYEAH/y86385eh/
So if u open develop console, there is a mistake
var postdate = data[‘data’][‘comments’][‘count’];
Uncaught TypeError: Cannot read property ‘count’ of undefined
If you know how to overcome that, i would be grategul!
I see.
Not sure if everything is correct there, but relating to the count error, your code should look like this:
Misha, god bless you! You`re a very talented coder, keep it up!
That works!
Thank you!
Just need to figure out, why it`s giving me info not for the last post, but seems for ALL time :33
ahhh…. figured it out too!
thats why i need count in data{} ) to set the amount of info i need)
God bless you again!
Thank you! 😊
I’m glad you’ve figured it out)
Hi Misha,
Do you know how to retrieve the image urls making the images in the feed clickable, therefore taking you to their respective instagram posts?
Thanks!
Anna
disregard, I believe I found your response to another question a few pages back…
thanks for sharing and for replying to everyone’s questions, not many do!
Hi Anna,
Ok, great, I’m glad you’ve figured it out 🙃
hi. tanks for your article.now just a little question.how can I link my photos to their instagram account??
#comment-1717
Hi Misha,
any idea how to fetch most liked pictures by date range, like show the most liked pictures in Oct 2017?
Thanks,
Dima
Hi Dima,
Depends on if you have the approved access token or not. But even if you have not access token, you can try the functions from this plugin. Custom coding is required anyway.
Hi,
Anyone help the taking images using #tag from all instagram and not only from 1 account?
Hi,
Sure, you can use:
This
mishaInstagram()
class is inside my plugin.Thanks but I need jQuery code
I think there is no way to do what you want with jQuery.
Hi Misha. Thanks for this article!
I did everything on your manual and got an error
code: 400, error_type: “APINotFoundError”, error_message: “this user does not exist” ?
Can u help me?
Hi Georg,
More info please about what you did.
I fix this problem:)
Account was tied to the wrong sandbox.
Thank u!)
Hi Misha its vinoth,
how can i get the title and the content of each instagram feeds from this.
Hi,
data.data[x].caption.text
Hi Misha,
How can I get resolution(height and width) of image ?
Hi
data.data[x].images.low_resolution.width
data.data[x].images.low_resolution.height
And you can use
standard_resolution
orthumbnail
instead oflow_resolution
.Hi Misha,
Thanks for the Reply. One general Query. Authentication token value from same credentials never change. Right ?
Yes
Can you use this to view a private instagram ?
No.
Hi, you know why can’t submit my app for get photos by tag?
Hi, no, I don’t
Hi Misha,
Can I get the feed from any user?
Hi there,
I am currently developing a tool that search hashtag, get the media under that hashtag, for each of the media, get who are liking. And currently, I’m still on search hashtag can’t continue to the second one. I already use one of your lines above from my previous project and that was successful but not implemented. I wish you can help me with this. Thank you
Hi There,
From the recent change logs from the Instagram currently, we are unable to get the feeds by using this endpoint, Is there any other process to get the feeds with new Instagram API.
Thank you in advance
It brokey!
Hi there.
Changes to inst api made all this stuff impossible to work. Do you plan to create new tutorials of of getting photos from instagram with all these changes happened?
Thank you and waiting for response
Hi,
Everything is still working in this tutorial.
hi, is this possible to get the posts captions and likes
Hi,
yes
do you know the way, or do you have an example
I think this tutorial should help you.
IT WORKED LIKE A CHARM !!! you are my hero
Hi Misha.
Great tutorial, clean, simple and basic… that’s great :)…
I have one questions:
How to get link for each image ?
Hi Mihajlo,
Thank you! 🙃
Do you mean URL of the file? If yes,
data.data[x].images.standard_resolution.url
.Great Tut Misha, how do i assign the url post to the image?
So when the user clicks on the image the wil be redirected to the post on instagram?
Thank for the effort.
Hey Levi,
data.data[x].link
.Great, thanks for the fast simply. Have a great day.
Thanks, have a great day too!
Hello can u please tell me how to print script from hashtag ?
Hello,
Currently the only working way is here.
Does it support multiple images that are on horizontal scroll in instagram?
Seems the Api picks only the first image on the horizontal scroll
Yep
I need to collect Instagram’s data (photos, comments, tags) for a research project in psychology. Apparently the sandbox mode does not let me to collect the data from crowd and I only can collect my own data (which would be totally useless for this scenario). Do you know how can we collect instagram’s data for research purposes? Thank you!
Hey Sarah,
You can check my plugin.
It’s a beautifull post, thanks a lot.
Regards, from Chile <3
Hi there
I get this error:
`This endpoint has been retired`
Looks like Instagram changed the api see this link:
https://www.instagram.com/developer/changelog/
So the `/media/recent` is deprached.
Any solution for this?
Can it work after 2020 ?
because IG will be deprecating the older Instagram API Platform in three phases with complete deprecation occurring in early 2020.
Hey Sam,
Are you talking about the API or about my Instagram plugin?
Hi I’d like to know about the method of using jquery or your Js version. Will they still work once the old IG API platform is put to rest?
This one:
-> https://www.instagram.com/developer/
If so can you do an example with this one here:
https://developers.facebook.com/docs/instagram-api/reference/user/media
Thank you so much!
Hi Franq,
I am not sure about working JS version. But I hope a PHP one will work for a long time.
Hi Misha! You are my WordPress master :D I have one question … Instagram changed its API and now we can have 200 requests per hour for a given token. How can you deal with it? Should I use cache? Or maybe WordPress Transient? I have no idea how to get around this limit, I do not know where to start … If the site is visited often, 200 per hour is very little.
Hi Maciej,
Oh, thank you! :)
Is it a WordPress website? If so, you can use transient cache for that.
Yes, this is a site based on WordPress. Do you have any tutorial on how to combine WordPress transients with Ajax? I have no idea how to go about it.
And by the way I will ask the second question :) Will your Instagram plugin work after 2020 when the API will be completely obsolete? And what exactly does Developer License mean? I mean, can I make a theme for, say, Themeforest and use it there? Will other developers who buy my theme be able to resell it or not?
Thanks! :)