• Stars
    star
    191
  • Rank 202,877 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Instagram api client for node that support promises.

npm version npm Build Status Coverage Status Dependency Status Known Vulnerabilities

node-instagram

⚠️ The legacy Instagram API is deprecated and will be disabled on June 29, 2020.

More information https://www.instagram.com/developer.

You should use the new Instagram Graph API.

Instagram api client for node that supports promises and typescript.

You can find examples in the examples directory.

Install

npm install --save node-instagram

yarn add node-instagram

Usage

import Instagram from "node-instagram";
// or
const Instagram = require("node-instagram").default;

// Create a new instance.
const instagram = new Instagram({
  clientId: "your-client-id",
  clientSecret: "your-client-secret",
  accessToken: "user-access-token"
});

// You can use callbacks or promises
instagram.get("users/self", (err, data) => {
  if (err) {
    // an error occured
    console.log(err);
  } else {
    console.log(data);
  }
});

// Get information about the owner of the access_token.
const data = await instagram.get("users/self");
console.log(data);

// Handle errors
instagram
  .get("tags/paris")
  .then(data => {
    console.log(data);
  })
  .catch(err => {
    // An error occured
    console.log(err);
  });

Streaming

This lib have a stream method. It is used to receive new post as events. Streaming can only be used on all endpoints taking MIN_TAG_ID as parameter. Inside it is running setInterval.

const stream = instagram.stream("tags/:tag-name/media/recent");

stream.on("messages", messages => {
  console.log(messages);
});

// handle stream error
stream.on("error", err => {
  // An error occur
  console.log(err);
});

Server side authentication

Two steps are needed in order to receive an access_token for a user.

  • Get an authentication url from instagram and redirect the user to it
  • Exchange the code for an access_token

You can find a working example with express here.

To see more info about server side authentication take a look at the instagram documentation.

// Example with express

// Your redirect url where you will handle the code param
const redirectUri = "http://localhost:3000/auth/instagram/callback";

// First redirect user to instagram oauth
app.get("/auth/instagram", (req, res) => {
  res.redirect(
    instagram.getAuthorizationUrl(
      redirectUri,
      {
        // an array of scopes
        scope: ["basic", "likes"]
      },
      // an optional state
      (state: "your state")
    )
  );
});

// Handle auth code and get access_token for user
app.get("/auth/instagram/callback", async (req, res) => {
  try {
    // The code from the request, here req.query.code for express
    const code = req.query.code;
    const data = await instagram.authorizeUser(code, redirectUri);
    // data.access_token contain the user access_token
    res.json(data);
  } catch (err) {
    res.json(err);
  }
});

Endpoints

To see all endpoint available take a look at instagram developer documentation.

// Get information about current user
instagram.get("users/self", (err, data) => {
  console.log(data);
});

// Get information about a user.
instagram.get("users/:user-id").then(data => {
  console.log(data);
});

// Get the most recent media published by the owner of the access_token.
instagram.get("users/self/media/recent").then(data => {
  console.log(data);
});

// Get the most recent media published by a user.
instagram.get("users/:user-id/media/recent").then(data => {
  console.log(data);
});

// Get the list of recent media liked by the owner of the access_token.
instagram.get("users/self/media/liked").then(data => {
  console.log(data);
});

// Get a list of users matching the query.
instagram.get("users/search", { q: "paris" }).then(data => {
  console.log(data);
});

// Get information about this media.
instagram.get("media/:media-id").then(data => {
  console.log(data);
});

// Get a list of users who have liked this media.
instagram.get("media/:media-id/likes").then(data => {
  console.log(data);
});

// Set a like on this media by the currently authenticated user.
instagram.post("media/:media-id/likes").then(data => {
  console.log(data);
});

// Remove a like on this media by the currently authenticated user.
instagram.delete("media/:media-id/likes").then(data => {
  console.log(data);
});

// Get information about a tag object.
instagram.get("tags/:tag-name").then(data => {
  console.log(data);
});

// Get a list of recently tagged media.
instagram.get("tags/:tag-name/media/recent").then(data => {
  console.log(data);
});

// Search for tags by name.
instagram.get("tags/search", { q: "paris" }).then(data => {
  console.log(data);
});

It is also possible to send the access_token along as a parameter when you call an endpoint. For example:

// Get information about current user
instagram.get("users/self", { access_token: accessToken }, (err, data) => {
  console.log(data);
});

// Search for tags by name.
instagram
  .get("tags/search", { access_token: accessToken, q: "paris" })
  .then(data => {
    console.log(data);
  });

Api

const instagram = new Instagram(config)

Create a new Instagram instance

Arguments

  • clientId string
  • accessToken string

instagram.get(endpoint, [params, callback])

Make a GET request on endpoint

Arguments

  • endpoint string
  • params object
  • callback function

instagram.post(endpoint, [params, callback])

Make a POST request on endpoint

Arguments

  • endpoint string
  • params object
  • callback function

instagram.delete(endpoint, [params, callback])

Make a DELETE request on endpoint

Arguments

  • endpoint string
  • params object
  • callback function

instagram.stream(endpoint, params)

Start a fake stream to a endpoint and return new messages found

Arguments

  • endpoint string
  • params object
  • params.interval number interval to run inside default 10000
  • params.runOnCreation boolean run the request when creating object
  • params.minTagId boolean instagram min_tag_id to start request

instagram.getAuthorizationUrl(redirectUri, options)

Get a valid auth url for instagram

Arguments

  • redirectUri string the url to redirect the user with the code
  • options object
  • options.scope array|string the scope to request
  • options.state string optional state
  • callback function

instagram.authorizeUser(code, redirectUri, [callback])

Handle the code returned by instagram an get a user access_token

Arguments

  • redirectUri string code returned by instagram
  • redirectUri string
  • callback function

License

MIT Β© LΓ©o Pradel

More Repositories

1

react-responsive-modal

Simple responsive react modal
TypeScript
605
star
2

create-react-app-esbuild

Use esbuild in your create-react-app for faster compilation, development and tests
JavaScript
560
star
3

create-react-app-swc

Use swc in your create-react-app for faster compilation, development and tests
JavaScript
129
star
4

esbuild-node-externals

Easily exclude node modules in Esbuild
TypeScript
71
star
5

gitlab-ci-validate

Checks if your .gitlab-ci.yml file is valid.
JavaScript
20
star
6

react-google-photo

React lightbox component using the google photo style
TypeScript
18
star
7

twoblocks

Free and open source 2fa manager built with Stacks
TypeScript
17
star
8

mongodb-bluebird

A simple package mongodb promise
JavaScript
16
star
9

react-minimalist-portal

A minimalist portal for react
JavaScript
15
star
10

stacks-graphql-api

A GraphQL API to query the Stacks blockchain.
12
star
11

stackspulse

Real-Time Gateway to Stacks DeFi.
TypeScript
12
star
12

stacks-next-auth-example

An example of Sign-In with Stacks and NextAuth.js
TypeScript
10
star
13

blockstack-wallet

🚧 The project is still in beta, use it at your own risk 🚧
TypeScript
10
star
14

octon

Notifies you when a new release has been made on repositories you starred on Github or Docker.
JavaScript
9
star
15

craco-blockstack

A craco plugin to use Blockstack with create-react-app
JavaScript
6
star
16

node-social-api

Social api client for node that support promises
JavaScript
6
star
17

find-facebook-id

Find a facebook id given a facebook name
JavaScript
5
star
18

graphql-rules

Utility allowing you to write rules for your graphql resolvers.
JavaScript
5
star
19

docusaurus-plugin-fathom

Fathom analytics plugin for Docusaurus
JavaScript
4
star
20

blockspot

Find the best spots to spend your crypto ⚠️ WIP ⚠️
TypeScript
4
star
21

accounts-js-server-tutorial

JavaScript
3
star
22

ionic-push

A node client for pushing android and ios notifications to your ionic app.
JavaScript
3
star
23

gitlab-ci-validate-rust

Checks if your .gitlab-ci.yml file is valid.
Rust
3
star
24

stacks-nft-guide

JavaScript
2
star
25

stacks-blockchain-api-deploy-fly-guide

Guide to deploy the stacks blockchain node API on Fly.io
Dockerfile
1
star
26

feed-sub

Tracks rss/atom feeds and get notified when they are updated.
JavaScript
1
star
27

ceramic-multi-users

Allow multiple wallets to write on the same ceramic stream via Lit Actions and Unlock protocol
TypeScript
1
star
28

meteor-ionic-push

Ionic push for meteor
JavaScript
1
star
29

defi-arkadiko-compound

Compound an arkadiko pool diko rewards into more LP tokens automatically
TypeScript
1
star
30

nextjs-starter

Next.js starter using the latest tech I use to create projects
TypeScript
1
star
31

blockstack-devtool

A simple devtool interface to help you manage different wallets and contracts on testnet.
TypeScript
1
star