What is it?
Instafeed.js is a dead-simple way to add Instagram photos to your website. No jQuery required, just plain 'ol javascript. (see ↑ there?)
Examples:
- Beardstagram by Luke Beard
- Manik Rathee is a mobile photographer by Manik Rathee
- The Kozik Cocoon by Danny Palmer
Installation
Setting up Instafeed.js is pretty straight-forward. Just download the script and include it in your HTML:
<script type="text/javascript" src="path/to/instafeed.min.js"></script>
Basic Usage
Here's how easy it is to get all images tagged with #awesome:
<script type="text/javascript">
var feed = new Instafeed({
get: 'tagged',
tagName: 'awesome',
clientId: 'YOUR_CLIENT_ID'
});
feed.run();
</script>
Instafeed.js with automatically look for a <div id="instafeed"></div> and fill it with linked thumbnails. Of course, you can easily change this behavior using the standard options. Also check out the advanced options and the section on templating for additional customization.
Requirements
The only thing you'll need to get going is a valid client id from Instagram's API. You can easily register for one on Instagram's website.
If you want to get images from a specific user, you will need a valid oAuth token. Using an oAuth token has security risks. Read more here.
Standard Options
-
clientId(string) - Your API client id from Instagram. Required. -
accessToken(string) - A valid oAuth token. Required to use get: 'user'. -
target(string) - The ID of a DOM element you want to add the images to. -
template(string) - Custom HTML template to use for images. See templating. -
get(string) - Customize what Instafeed fetches. Available options are:popular(default) - Images from the popular page.tagged- Images with a specific tag. Use tagName to specify the tag.location- Images from a location. Use locationId to specify the location.user- Images with a user. Use userId to specify the user. More info here.
-
tagName(string) - Name of the tag to get. Use with get: 'tagged'. -
locationId(number) - Unique id of a location to get. Use with get: 'location'. -
userId(number) - Unique id of a user to get. Use with get: 'user'. -
sortBy(string) - Sort the images in a set order. Available options are:most-recent(default) - Newest to oldest.least-recent- Oldest to newest.most-liked- Highest # of likes to lowest.least-liked- Lowest # likes to highest.most-commented- Highest # of comments to lowest.least-commented- Lowest # of comments to highest.random- Random order.
-
links(true/false) - Wrap the images with a link to the photo on Instagram. -
limit(number) - Maximum number of Images to add. Max of 60. -
resolution(string) - Size of the images to get. Available options are:thumbnail(default) - 150x150low_resolution- 306x306standard_resolution- 612x612
Advanced Options
-
before(function) - A callback function called before fetching images from Instagram. -
after(function) - A callback function called when images have been added to the page. -
success(function) - A callback function called when Instagram returns valid data. Takes the JSON data as an object argument. -
error(function) - A callback function called when there is an error fetching images.Takes an error message as a string argument. -
mock(true/false) - Fetch data without inserting images into DOM. Use with success callback.
Templating
The easiest way to control the way Instafeed.js looks on your website is to use the template option. You can write your own HTML markup and it will be used for every image that Instafeed.js fetches.
<script type="text/javascript">
var feed = new Instafeed({
get: 'tagged',
tagName: 'awesome',
clientId: 'YOUR_CLIENT_ID',
template: '<a href="{{link}}"><img src="{{image}}" /></a>'
});
feed.run();
</script>
Notice the {{link}} and {{image}}? The templating option provides several tags for you to use to control where variables are inserted into your HTML markup. Available keywords are:
-
{{link}}- URL to view the image on Instagram's website. -
{{image}}- URL of the image source. The size is inherited from the resolution option. -
{{caption}}- Image's caption text. Defaults to empty string if there isn't one. -
{{likes}}- Number of likes the image has. -
{{comments}}- Number of comments the image has. -
{{location}}- Name of the location associated with the image. Defaults to empty string. -
{{id}}- Unique ID of the image. Useful if you want to use iPhone hooks to open the images directly in the Instagram app. -
{{model}}- Full JSON object of the image. If you want to get a property of the image that isn't listed above you access it using dot-notation. (ex:{{model.filter}}would get the filter used)
Getting images from your User Account
With Instafeed, it is possible to get images from a specific user id:
<script type="text/javascript">
var userFeed = new Instafeed({
get: 'user',
userId: YOUR_USER_ID,
accessToken: 'YOUR_ACCESS_TOKEN'
});
userFeed.run();
</script>
This setup requires an accessToken. Normally, using tokens like this in javascript would be very bad. However, since Instagram provides scoping in their API, you can limit the risk of user impersonation.
Just always make sure your token is set to basic authorization, which only allows GET requests. If you aren't sure what scope your token has, check under your account page.
Don't know your user id or token? Click here to get one.
Change Log
- (v1.2.0)
- Added the ability to sort images! Use the sortBy option.
- More template tags! You can now use {{likes}}, {{comments}}, {{id}}, {{caption}}, {{location}}, and {{model}}. Check the section on templating for more info.
- (v1.1.0)
- Added option to use a custom html template with the template option.
- Added ability to fetch several feeds at the same time (create separate instances).
- Added before, success, after, and error callback options.
- Added mock option. Use with success option for custom DOM manipulation.
- (v1.0.0)
- Initial release