Go back to the main page

Getting started with Nodejs and QuickBooks Online Part 2



Continuing from Part 1

In part 1 we just did the bare minimum. Now, in part 2 we are going to go a bit further by creating a customer route where we will pass in an id and then retrieve the associated customer from the developer sandbox and output their "DisplayName" value.

  1. Let's start by making that new route right below the /start route in app.js.
    app.get('/customer/:id', function (req, res) {
      res.render('customer.ejs', { locals: { customer: customer }})
  3. Then make a new customer view in views/customer.ejs
  4. <!DOCTYPE html>
    <html lang="en">
      <meta charset="UTF-8">
  5. Go to the browser and run the route http://localhost:3000/customer/5.
  6. You should see "5" output to the console running nodemon app.js.
  7. Next, we are going implement basic persistence with the cookie-session NPM package
  8. Minimul says —

    We need to persist the OAuth token, secret, and Intuit's realmId (now referred as the "company id") to make repeated API calls. In a real app you would use a data store like Postgres, Redis, etc. Here we will use sessions, however, the express-session only persists for a short duration and is meant for data stores. Therefore, let's use the cookie-session package to get some basic persistence.

    $ cd example/
    $ npm install cookie-session --save // --save option saves the entry to example/package.json
  9. Back in app.js make the following modifications.
  10. ....
    cookieParser = require('cookie-parser'),
    -    session    = require('express-session'),
    +    cookieSession    = require('cookie-session'),
    -app.use(session({resave: false, saveUninitialized: false, secret: 'smith'}));
    +app.use(cookieSession({name: 'session', keys: ['key1']}))
  11. Let's save the OAuth token, secret, and realmId. Go to line 78 or app.js and insert this code to persist these values across requests.
  12.     // Line 78
        req.session.qbo = {
          token: accessToken.oauth_token,
          secret: accessToken.oauth_token_secret,
          companyid: postBody.oauth.realmId
  13. We must make a way to call the Quickbooks initialization code a little easier and DRYer.
  14. Go to the end of app.js and make a global function to easily initialize the Quickbooks object from node-quickbooks.
  15. var getQbo = function (args) {
      return new QuickBooks(consumerKey,
                           true, // use the Sandbox
                           true); // turn debugging on
  16. Replace this similar code from the /callback method after the session save code in step 7.
  17.     qbo = getQbo(req.session.qbo);
  18. Now modify the /customer/:id route to look like this:
  19. app.get('/customer/:id', function (req, res) {
      var qbo = getQbo(req.session.qbo);
      qbo.getCustomer(req.params.id, function(err, customer) {
        res.render('customer.ejs', { locals: { customer: customer }})
  20. Then modify the views/customer.ejs to output the DisplayName.
  21.   <h1>Customer <%= customer.DisplayName %></h1>
  22. Go back to the browser at http://localhost:3000/start and click on the "Connect to QuickBooks" button and run through OAuth again. The will save your OAuth token, secret, and realmId in sessions.
  23. After that run the http://localhost:3000/customer/5 route.
  24. You should get:
  25. "Dukes Basketball Camp" is sandbox customer id 5.


That's a wrap for part 2, in which I created a customer route that looked up and outputted a QuickBooks Online customer "DisplayName". Also, reference the code for this 2-part tutorial.