Simple, easy and very complete implementation of the Instagram private web API.
- Support for all the main functions of Instagram Web
- Well tested, CI
- All test runs daily
npm install instagram-web-apiIntance Instagram and call login method; this stores the credentials in memory.
const Instagram = require('instagram-web-api')
const { username, password } = process.env
const client = new Instagram({ username, password })
client
.login()
.then(() => {
client
.getProfile()
.then(console.log)
})Using async/await in Node >= 8
const Instagram = require('instagram-web-api')
const { username, password } = process.env
const client = new Instagram({ username, password })
;(async () => {
await client.login()
const profile = await client.getProfile()
console.log(profile)
})()Save cookies to disk by using a though-cookie store.
// Packages
const Instagram = require('instagram-web-api')
const FileCookieStore = require('tough-cookie-filestore2')
const { username, password } = process.env // Only required when no cookies are stored yet
const cookieStore = new FileCookieStore('./cookies.json')
const client = new Instagram({ username, password, cookieStore })
;(async () => {
// URL or path of photo
const photo =
'https://scontent-scl1-1.cdninstagram.com/t51.2885-15/e35/22430378_307692683052790_5667315385519570944_n.jpg'
await client.login()
// Upload Photo to feed or story, just configure 'post' to 'feed' or 'story'
const { media } = await client.uploadPhoto({ photo: photo, caption: 'testing', post: 'feed' })
console.log(`https://www.instagram.com/p/${media.code}/`)
})()- Instagram
- new Instagram({ username, password, cookieStore }, { language, proxy, requestOptions })
- .login({ username, password })
- .logout()
- .getHome()
- .getUserByUsername({ username })
- .getFollowers({ userId, first, after })
- .getFollowings({ userId, first, after })
- .getActivity()
- .getProfile()
- .updateProfile({ name, email, username, phoneNumber, gender, biography, website, similarAccountSuggestions })
- .changeProfilePhoto({ photo })
- .deleteMedia({ mediaId })
- .uploadPhoto({ photo, caption, post })
- .getMediaFeedByLocation({ locationId })
- .getMediaFeedByHashtag({ hashtag })
- .locationSearch({ query, latitude, longitude })
- .getMediaByShortcode({ shortcode })
- .addComment({ mediaId, text })
- .deleteComment({ mediaId, commentId })
- .getChallenge({ challengeUrl })
- .updateChallenge({ challengeUrl, choice, securityCode })
- .resetChallenge({ challengeUrl })
- .replayChallenge({ challengeUrl })
- .approve({ userId })
- .ignore({ userId })
- .follow({ userId })
- .unfollow({ userId })
- .block({ userId })
- .unblock({ userId })
- .like({ mediaId })
- .unlike({ mediaId })
- .save({ mediaId })
- .unsave({ mediaId })
- .search({ query, context })
- .getPhotosByHashtag({hashtag, first, after})
- .getPhotosByUsername({username, first, after})
- .getPrivateProfilesFollowRequests(cursor)
- .getChainsData({ userId })
- .getMediaLikes({ shortcode, first, after })
- .getMediaComments({ shortcode, first, after })
const client = new Instagram({ username: '', password: '' }, { language: 'es-CL' })Initializes the client.
credentialsusername: The username of accountpassword: The password of accountcookieStore: An optionalthough-cookiecookie storage, which allows for persistent cookies. Default isundefined
optslanguage: The language of response from API. Default isen-USproxy:Stringof a proxy to tunnel all requests. Default isundefined
const { username, password, cookies } = await client.login({ username: '', password: '' })
const { authenticated, user } = await client.login({ username: '', password: '' })Login in the account, this method returns
user(truewhen username is valid) andauthenticated(truewhen login was successful)
credentialsusername: The username of accountpassword: The password of account
await client.logout()Logout in the account.
const feed = await client.getHome('KGEAxpEdUwUrxxoJvxRoQeXFGooSlADHZ8UaDdSWbnOIxxoUUhyciJ7EGlxNlZjaYcUaXTgUM00qyBrgBhUsLezIGqVTlxqausga5W-fVax9xRryaBdN1EnIGvdQFgzxoMgaFoLO7v7xWQA=')Get home feed timeline, media shared by the people you follow.
paramsend_cursor(String) for pagination
const instagram = await client.getUserByUsername({ username: 'instagram' })
const me = await client.getUserByUsername({ username: client.credentials.username })Get user by username, this method not require authentication for public profiles.
paramsusername: The username of the profile
const followers = await client.getFollowers({ userId: '1284161654' })Get followers for given userId. Be aware that the response gets slightly altered for easier usage.
paramsuserId: The user idfirst: Amount of followers to request. Default is20after: Optionalend_cursor(String) for pagination.
const followings = await client.getFollowings({ userId: '1284161654' })Get followings for given userId. Be aware that the response gets slightly altered for easier usage.
paramsuserId: The user idfirst: Amount of followings to request. Default is20after: Optionalend_cursor(String) for pagination.
const activity = await client.getActivity()Get activity of account, news following, liked, etc.
const profile = await client.getProfile()Get profile the account
first_name,last_name,username,phone_number,gender,birthday,biography,external_urlandchaining_enabled.
await client.updateProfile({ biography: '❤️', website: 'https://jlobos.com/', gender: 1 })Update profile the account.
paramsname: The full name. Default isemail: The email of account. Default isusername: The username of account. Default isclient.credentials.usernamephoneNumber: The Phone Number. Default isgender:Number1male,2female and3not specifiedbiography: The Bio. Default iswebsite: The Website. Default issimilarAccountSuggestions:BooleanInclude your account when recommending similar accounts people might want to follow. Default istrue
const fs = require('fs')
const photo = fs.join(__dirname, 'photo.jpg')
await client.changeProfilePhoto({ photo })Change the profile photo.
paramsphoto: AStringof path file or URL
await client.deleteMedia({ mediaId: '1442533050805297981' })Delete a media, photo, video, etc. by the id.
paramsmediaId: The media id
const photo = 'https://scontent-scl1-1.cdninstagram.com/t51.2885-15/e35/16465198_658888867648924_4042368904838774784_n.jpg'
await client.uploadPhoto({ photo, caption: '❤️', post: 'feed' })Upload a photo to Instagram. Only jpeg images allowed.
paramsphoto: AStringof path file or URLcaption: The caption of photo. Default ispost: The local post,feedorstory
const location = await client.getMediaFeedByLocation({ locationId: '26914683' })Get latitude, longitude, top posts, last media, country, city, and more related to the location.
paramslocationId: The location id
const tag = client.getMediaFeedByHashtag({ hashtag: 'unicorn' })Explore last media and top posts feed related to a hashtag.
paramshashtag: A hashtag, not including the "#"
const venues = client.locationSearch({ query: 'chile', latitude: -33.45, longitude: -70.6667 })Search venues by latitude and longitude.
paramslatitude: Latitudelongitude: Longitudequery: A optional location name. Default is
const media = await client.getMediaByShortcode({ shortcode: 'BQE6Cq2AqM9' })Get data of a media by the Instagram shortcode
paramsshortcode: A shortcode
await client.addComment({ mediaId: 1442533050805297981, text: 'awesome' })Add comment to a media item.
paramsmediaId: The media idtext: Comment textreplyToCommentId: Optional comment id to which to reply
await client.deleteComment({ mediaId: '1442533050805297981', commentId: '17848908229146688' })Delete a comment.
paramsmediaId: The media idcommentId: The comment id
await client.getChallenge({ challengeUrl: '/challenge/1284161654/a1B2c3d4E6/' })Get information about a challenge.
paramschallengeUrl: AStringwith a challenge path
const challengeUrl = '/challenge/1284161654/a1B2c3d4E6/'
await client.updateChallenge({ challengeUrl, choice: 0 })
await client.updateChallenge({ challengeUrl, securityCode: 123456 })Request or submit a verification code for the given challenge.
paramschallengeUrl: AStringwith a challenge pathchoice:Number0for phone and1for email. Default is ``securityCode:Numberthe received verification code for the challenge. Default is ``
await client.resetChallenge({ challengeUrl: '/challenge/1284161654/a1B2c3d4E6/' })Reset a challenge to start over again.
paramschallengeUrl: AStringwith a challenge path
await client.replayChallenge({ challengeUrl: '/challenge/1284161654/a1B2c3d4E6/' })Request a new verification message.
paramschallengeUrl: AStringwith a challenge path
await client.approve({ userId: '1284161654' })Approve a friendship request.
paramsuserId: The user id
await client.ignore({ userId: '1284161654' })Reject a friendship request.
paramsuserId: The user id
await client.follow({ userId: '1284161654' })Follow a user.
paramsuserId: The user id
await client.unfollow({ userId: '1284161654' })Unfollow a user.
paramsuserId: The user id
await client.block({ userId: '1284161654' })Block a user.
paramsuserId: The user id
await client.unblock({ userId: '1284161654' })Unblock a user.
paramsuserId: The user id
await client.like({ mediaId: '1442533050805297981' })Like a media item.
paramsmediaId: The media id
await client.unlike({ mediaId: '1442533050805297981' })Unlike a media item.
paramsmediaId: The media id
await client.save({ mediaId: '1442533050805297981' })Save a media item.
paramsmediaId: The media id
await client.unsave({ mediaId: '1442533050805297981' })Unsave a media item.
paramsmediaId: The media id
await client.search({ query: 'unicorn' })Search users, places, or hashtags.
paramsquery: Querycontext: The context of search,hashtag,place,userorblended. Default isblended
await client.getPhotosByHashtag({ hashtag: 'unicorn' })Get photos for hashtag.
paramshashtag: AStringwith a hashtagfirst: Anumberof records to returnafter: The query cursorStringfor pagination
await client.getPhotosByUsername({ username: 'unicorn' })Gets user photos.
paramsusername: AStringwith a hashtagfirst: Anumberof records to returnafter: The query cursorStringfor pagination
await client.getPrivateProfilesFollowRequests(cursor)await client.getChainsData({ userId })This will return the similar accounts, that you see, when you click on the ARROW in a profile.
paramsuserId: The user id
await client.getMediaLikes({ shortcode: 'B-0000000', first: '49', after: '' })This will return the media likes.
paramsshortcode: The shortcode media like this: https://www.instagram.com/p/B-00000000/, only put shortcode like this : B-000000000first: Anumberof records to return max is49after: The query cursorStringfor pagination
await client.getMediaComments({ shortcode: 'B-0000000', first: '12', after: '' }).catch((error) => {
console.log(error);
})
.then((response) => {
console.log(response);
});
//The query cursor 'after' maybe return an array, if array you need to convert like this:
let pointer = response.page_info.end_cursor;
// this will try to convert array to json stringify
try{
pointer = JSON.parse(pointer);
pointer = JSON.stringify(pointer);
}catch(e){
console.log('Pointer is not array!, don't need to be converted!');
}This will return the media comments.
paramsshortcode: The shortcode media like this: https://www.instagram.com/p/B-00000000/, only put shortcode like this : B-000000000first: Anumberof records to return max is49after: The query cursorStringfor pagination
MIT © Jesús Lobos