Implementing Coinbase Connect (OAuth2) in Node

This post includes the use of Node.js, Express, PassportJS, Coinbase, and the passport-coinbase-oauth2 Strategy

Last week we discussed utilizing React and PapaParse to import a CSV of crypto trading data into a Rails backend. This week, we’ll go in a different direction and learn how to connect to Coinbase using Coinbase Connect and a Node.js backend. This method (rather than the API) should be used when you’re building an application for others to login. I ended up making this all work within React, but have decided to move forward with a Node.js backend for this Crypto app. I have used MongoDB to store user data, but we don’t go over that in this tutorial. This tutorial assumes basic knowledge of Javascript, Express, NodeJS but I will walk you through them at a basic level.

Creating our App

Within this index.js file, we want to require our express library, create our first express application, and have our application listen on a certain port. Once we have these very basics complete, we can begin setting our routes and getting familiar with Coinbase Connect.

Coinbase Connect

Let’s install the necessary libraries by running “npm install --save passport passport-coinbase-oauth2 coinbase”. This will install the three necessary libraries and strategies. Let’s now require these libraries in our application, and then use our new strategy…

Before continuing further, make sure to create a Coinbase OAuth2 application here and get a ClientID and Client Secret. We will be creating a redirect route for: http://localhost:3000/auth/coinbase/callback

Now that we have our ClientID and Client Secret, we will create a separate keys.js file that we can add to .git.ignore if we are uploading our project. We don’t want our private keys out in the wild!

Coinbase Strategy

The arguments quickly filled up! We want to pass our ClientID and ClientSecret so Coinbase’s servers know that we are correctly requesting data, we will set our callback URL in the application in the next step, but notice how it matches what we set in Coinbase OAuth2. The next three are standard values, a list of scopes can be found here, and I am still trying to figure out how to call all accounts and not just a single crypto account. I will post an update when I figure this out.

**UPDATE** You don’t have to put the scope and account here — it should be put in the get route as you can see in the image below (explained in the next section):

In the next argument, we can just console log the outputs to ensure we are correctly connecting with the server.

Handling Routes

Testing OAuth2

If you’re looking to save users or user data, you’re going to want to add some code in the Coinbase strategy where we put the console log.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store