Instagram authentication with Node.js and MongoDB

A few days ago I was asked by a friend to help with her business Instagram account, to develop a service which will help her and other owners of accounts to maintain their audience. Without any doubts I agreed because firstly, I love to help other people if I can, and secondly it was a good opportunity to start working with the Instagram API.

In this article I will demonstrate step by step how to develop an Instagram authentication, and at the end we will receive a response from the Instagram API as shown below, and save the data to a Mongo database.

What technologies are we going to use?

First I thought it will be built just on Angular, but we will receive an access token from the Instagram API and it is very insecure to store it in local storage, or somewhere else on the front end, because if some badass steals it — they have access to all of your account data. It’s much better to develop the backend and save the token in a database. And that’s why I decided to use Node.js as the backend and MongoDB as the database.

Instagram client registration.

First things first, lets go to Instagram Developer’s page and create our first client. To do that you need to login and go to the ‘Manage Clients’ tab and click ‘Register a New Client’ there. Complete all needed data and ‘Register’. After the client is successfully created we will need 3 parameters for now: Client ID, Client Secret and redirect URLs where Instagram will redirect you with a CODE as a query parameter after logging in to your account.

Setup the Node.js server.

Before we start, make sure you have installed Node.js and npm (node package manager) on your local machine.

node --version && npm --version

And if it’s not there just go to the Node.js web site and complete the installation instructions.

After node and npm are up and running on your computer, create a folder with the app and initialize npm there.

mkdir instagram-auth && cd instagram-auth
npm init -y
npm install --save express

If everything was installed successfully you should have a package.json file in your root project folder.


Note that I’ve added a start script property so we can start our server with a npm start command.

Now we need create our server.js file and start configuring it.


We can run npm start and see if our server is working on localhost port 3000.

How Instagram authentication works?

Lets figure out how Instagram authentication works. On your page you have a Login button which redirects you to the Instagram login page at the url:

In our Instagram client we have all those query parameters so in our case the url should look like:

After the user logs in at the provided url and accepts all permissions, it will redirect to redirect_uri with a code as a query parameter.


Use the received code to POST to the Instagram access_token API:

Get the access token from the response together with the user data and save it to the database. It seems quite simple, so let’s implement that in our backend.

Node.js implementation.

We need to install a few more node modules which are needed for the application.

  • morgan is an http request logger middleware for Node.js
  • mongoose is an object modeling tool for MongoDB
npm install --save morgan mongoose

Import them into our server and configure:


In this tutorial I won’t cover how to set up the MongoDb but you can easily do it on your own for free on the mLab website.

Lets move our constants to the config now.


Now we need a single html page with a Login button:


We need to handle 2 routes for the login. The first is when a user goes to /login and the second is /auth when a user redirects back with a CODE query parameter.


Let’s give it a try. Run the server and go to the url http://localhost:3000 and after clicking the Login button we should see the following:

If you see something similar — that’s awesome! Now we can make a POST request to Instagram API and receive a token.

We will need to request a module for posting a request to the Instagram access token API:

npm install --save request

And let’s clean up our code a little and move the GET callback function to a separate module.


So now we are making an http POST request with options and we are expecting to receive a user from Instagram. Let’s start our server and give it a try.

We now receive the token and user. The final step is to save this data to our MongoDb. And let’s start with building a plan and model for our collection.

Now save the user we received from Instagram and redirect to the home directory.

Restart the server, login and let’s connect to our MongoDb and see if the user was successfully created.

We can see that our user was created, and now you can work with this token and get access to the Instagram data.

Thank you for the time that you spent reading this article and I hope it was useful for you. If you had any difficulties — feel free to text me, always glad to help you.