Getting Started with Instagram API in 20 minutes or Less

I love Instagram. I enjoy curating photography (taken mostly from my iPhone) from my travels and more mundane day-to-day activities. Instagram provides another venue to express my creative side and to stay connected with friends and family. So I thought I would combine this love with my interest in technology. This article includes a tutorial to quickly get started using the Instagram API in 20 minutes are less.  Let’s get started.

How to use the Instagram API in 20 minutes or less

To start using the Instagram REST API refer to the developer page.  Visit this page for a list of the API endpoints.  You can also use Apigee API Console for Instagram to explore the api services.  To dive in we need to start with one api endpoint. Once you are familiar with one, the others should be straight forward. For purposes of this tutorial I will go over the api: ‘Get the most recent media published by a user
https://api.instagram.com/v1/users/{user-id}/media/recent/?access_token=ACCESS-TOKEN.

Instagram API Authorization

All Instagram API endpoints now require a valid access_token. Instagram uses the OAuth 2.0 protocol to authenticate requests, meaning all api requests require user authentication, even for public user data. To get an access_token requires one to get user authorization. (See this page for more information on Instagram API Authentication).  If you are providing an Instagram user service, you will want to get an access_token specific to that user.  If you are only interested in public data, such as a user’s recent media feeds, you can retrieve an access_token for a specific developer account and use it for access to any user’s public data. This is the method I will go over in this tutorial.
As a side note, there is an undocumented endpoint that gets a user’s recent media feeds up to 20 without any authentication(https://www.instagram.com/{username}/media/). This greatly simplifies the implementation, but because it’s undocumented, it’s probably not supported so be forewarned and use at your own risk.

Steps for getting a user’s recent media using Instagram API

The api endpoint is:
https://api.instagram.com/v1/users/{USER-ID}/media/recent/?access_token={ACCESS-TOKEN}
The required parameters are access_token and user_id.  As mentioned, every Instagram api service now requires an access_token. You can get an access token for an account by following these quick and simple steps.

1. Register your app

Enter this url: https://www.instagram.com/developer/in your browser and enter required information.  Once complete you will be given a CLIENT-ID and CLIENT-SECRET.   You will use this information in later steps.

instagram-clientid

2. Register your client

Select the ‘Register a New Client‘ button, filling in all personal info.  For the ‘redirect URI’, enter http://localhost.  Using localhost will make it very easy to receive the CODE, needed to get the access_token, in the next step.

instagram-register

3. Get the access token

Enter the following url in your browser, replacing {CLIENT-ID} with your values from step 1.
https://api.instagram.com/oauth/authorize/?client_id={CLIENT-ID}&redirect_uri=http://localhost&response_type=code . Your browser will return a response similar to the following where the returned CODE is used to get the access_token, in the next step.
http://localhost/?code=CODE

4. Get access_token

From your command-line, execute the following curl command replacing your values for CLIENT-ID, CLIENT-SECRET, CODE.

curl -F ‘client_id=CLIENT-ID‘ \
-F ‘client_secret=CLIENT-SECRET” \
-F ‘grant_type=authorization_code’ \
-F ‘redirect_uri=http://localhost’ \
-F ‘code=CODE‘ \
https://api.instagram.com/oauth/access_token

You will receive a response similar to the following, where ACCESS-TOKEN is the token that will be used in all Instagram api services.  Secure your access_token; this is your authorization key.

{“access_token”: ACCESS-TOKEN, “user”: {“username”: “bellasjardin”, “bio”: “Engineer, Succulent and Plant Enthusiast, Traveler and Nature Lover. \ud83c\udf3fhttps://www.etsy.com/shop/BellasJardin\ud83c\udf3f”, “website”: “http://www.bellasjardin.com”, “profile_picture”: “https://scontent.cdninstagram.com/t51.2885-19/s150x150/12950269_835314869906758_1077337788_a.jpg”, “full_name”: “Dolce Vita”, “id”: “2929570424”}}

5. Now you are ready to explore the available Instagram api endpoints

Recall, to get a user’s recent media, we need the user-id. There is an api endpoint ‘Get information about the owner of a token‘ .  Enter the following url in your browser, replacing ACCESS-TOKEN with your access_token.
https://api.instagram.com/v1/users/self/?access_token=ACCESS-TOKEN

You will receive a response similar to the following, where USER-ID will be your user id

{“meta”: {“code”: 200}, “data”: {“username”: “bellasjardin”, “bio”: “Engineer, Succulent and Plant Enthusiast, Traveler and Nature Lover. \ud83c\udf3fhttps://www.etsy.com/shop/BellasJardin\ud83c\udf3f”, “website”: “http://www.bellasjardin.com”, “profile_picture”: “https://scontent.cdninstagram.com/t51.2885-19/s150x150/12950269_835314869906758_1077337788_a.jpg”, “full_name”: “Dolce Vita”, “counts”: {“media”: 181, “followed_by”: 61, “follows”: 14}, “id”: USER-ID}}

5. Get A User’s Recent Media.

From your browser, enter the following url, replacing {USER-ID} and {ACCESS-TOKEN} with your values. https://api.instagram.com/v1/users/{USER-ID}/media/recent/?access_token={ACCESS-TOKEN}

Json data is returned.  You can limit the amount of data returned by specifying parameters for count, min_id, max_id.
instagram-json

Conclusion

Up to this point, we learned how to get an access_token for an Instagram account.  We also executed a couple of Instagram services to retrieve a user’s recent media. We did all of this without any coding. Now you are ready to code your own Instagram app using the language and technologies of your choice.   I have created an Instagram test application on gitHub, that uses javascript with ajax/jquery.  This sample demonstrates 2 approaches.
1. Implement Instagram requests  all from the client (eg: browser).  While simple, this is not recommended as your access_token is unprotected, allowing a malicious developer to easily get your access_token using tools like firebug or other browser developer tools.  You will also notice that this example uses JSONP to overcome the same-domain-policy restriction.  You can read more about Instagram’s support for JSONP here.

2.  Implement Instagram requests from a server component.   This approach is more complex as it requires an added server layer.  However this approach is more secure since the implementation details of the request is done at the server, whose resources are protected.  This example uses PHP for the server component.

Instagram is a fun and popular social media platform for sharing photos and videos.  With the Instagram API, you can integrate Instagram into custom apps and websites.  I hope you enjoyed this tutorial and that it was informative.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.