Back

How to build a public QuickBooks Time integration: Building the Auth Flow

Aug 8, 20247 minute read

Hey there, fellow JavaScript enthusiasts! Ready to dive into the world of QuickBooks Time integration? Let's roll up our sleeves and build an auth flow that'll make your users' lives easier and your app more powerful.

Introduction

QuickBooks Time API is a powerhouse for time tracking and workforce management. But before we can tap into its potential, we need to nail the authorization process. It's like getting the keys to a shiny new car – once you've got 'em, you're ready to hit the road!

Prerequisites

Before we jump in, make sure you've got:

  • A QuickBooks Developer account (if you don't have one, go grab it – it's free!)
  • A registered application (think of it as your app's ID badge)
  • Node.js installed (you're a JS dev, so I'm betting you've got this covered)
  • Some essential packages (express and axios – we'll be using these bad boys)

Setting up the project

Let's start with a basic Express.js server. Nothing fancy, just the bare bones:

const express = require('express'); const app = express(); // Your routes will go here app.listen(3000, () => console.log('Server running on port 3000'));

Now, let's keep our sensitive info safe. Create a .env file and add your client ID and secret:

QB_CLIENT_ID=your_client_id_here
QB_CLIENT_SECRET=your_client_secret_here

Don't forget to add .env to your .gitignore!

Implementing OAuth 2.0 flow

Initiating the authorization request

First, let's create a route that'll kick off the auth process:

app.get('/auth', (req, res) => { const authUrl = `https://appcenter.intuit.com/connect/oauth2?client_id=${process.env.QB_CLIENT_ID}&redirect_uri=${encodeURIComponent('http://localhost:3000/callback')}&response_type=code&scope=com.intuit.quickbooks.timetracking`; res.redirect(authUrl); });

This route constructs the authorization URL and sends your user on a quick trip to QuickBooks land.

Handling the callback

When the user comes back (hopefully with good news), we need to be ready:

app.get('/callback', async (req, res) => { const { code } = req.query; try { const { data } = await axios.post('https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer', { grant_type: 'authorization_code', code, redirect_uri: 'http://localhost:3000/callback' }, { auth: { username: process.env.QB_CLIENT_ID, password: process.env.QB_CLIENT_SECRET } }); // Store these tokens securely! const { access_token, refresh_token } = data; res.send('Authorization successful!'); } catch (error) { console.error('Error exchanging code for tokens:', error); res.status(500).send('Authorization failed'); } });

This code exchanges the authorization code for access and refresh tokens. Remember, treat these tokens like gold – they're your ticket to the QuickBooks Time API!

Making authenticated API requests

Now that we've got our access token, let's use it:

app.get('/api-request', async (req, res) => { try { const { data } = await axios.get('https://quickbooks.api.intuit.com/v3/company/<realmId>/timeactivity', { headers: { 'Authorization': `Bearer ${access_token}`, 'Accept': 'application/json' } }); res.json(data); } catch (error) { console.error('API request failed:', error); res.status(500).send('API request failed'); } });

Replace <realmId> with the actual realm ID for the QuickBooks company you're working with.

Error handling and edge cases

Always be prepared for the unexpected:

  • Check for token expiration before making requests
  • Implement token refresh logic
  • Handle cases where the user denies authorization

Security considerations

Security isn't just important, it's crucial. Here are some tips:

  • Always use HTTPS in production
  • Implement the state parameter to prevent CSRF attacks
  • Store tokens securely (consider encryption at rest)

Testing the integration

Before you pop the champagne, make sure to thoroughly test your integration:

  1. Try the happy path (everything works)
  2. Test error scenarios (invalid tokens, network issues)
  3. Verify token refresh works correctly

Consider setting up automated tests to catch any future issues.

Conclusion

And there you have it! You've just built a solid foundation for your QuickBooks Time integration. The auth flow might seem like a lot of work, but trust me, it's worth it. With this in place, you're ready to unlock all sorts of cool features for your users.

Remember, this is just the beginning. There's a whole world of QuickBooks Time API endpoints waiting for you to explore. So go forth and build something awesome!

Additional resources

Want to dive deeper? Check out these resources:

Happy coding, and may your integrations always be smooth and your tokens ever-refreshing!