Back

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

Aug 3, 20246 minute read

Hey there, fellow JavaScript aficionado! Ready to dive into the world of QuickBooks integration? Let's focus on the most crucial part: building a rock-solid authorization flow. We'll keep things concise and to the point, so you can get your integration up and running in no time.

Introduction

QuickBooks API is a powerhouse for financial data, and we're going to tap into it using OAuth 2.0. This industry-standard protocol will ensure our integration is secure and user-friendly.

Prerequisites

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

  • A QuickBooks Developer account (if you don't have one, go grab it!)
  • An app registered in the QuickBooks Developer portal
  • Node.js and Express.js set up and ready to roll

Got all that? Great! Let's get our hands dirty.

Setting up the OAuth 2.0 flow

First things first, let's configure those OAuth 2.0 credentials. Head to your app settings in the QuickBooks Developer portal and snag your client ID and secret. Now, let's create an authorization URL:

const authUri = 'https://appcenter.intuit.com/connect/oauth2'; const redirectUri = 'http://localhost:3000/callback'; const authUrl = `${authUri}?client_id=${clientId}&redirect_uri=${redirectUri}&scope=com.intuit.quickbooks.accounting&response_type=code&state=${generateRandomState()}`;

Implementing the authorization request

Time to send your users on a little trip to QuickBooks:

app.get('/connect', (req, res) => { res.redirect(authUrl); });

When they come back, be ready to catch that callback:

app.get('/callback', async (req, res) => { const { code, state } = req.query; // Verify state and handle the authorization code });

Exchanging the authorization code for tokens

Now for the good stuff - let's swap that code for some shiny tokens:

const tokenResponse = await axios.post('https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer', { grant_type: 'authorization_code', code, redirect_uri: redirectUri, }, { auth: { username: clientId, password: clientSecret, }, }); const { access_token, refresh_token } = tokenResponse.data; // Store these tokens securely!

Refreshing the access token

Access tokens don't last forever, so let's keep them fresh:

async function refreshAccessToken(refreshToken) { const tokenResponse = await axios.post('https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer', { grant_type: 'refresh_token', refresh_token: refreshToken, }, { auth: { username: clientId, password: clientSecret, }, }); return tokenResponse.data.access_token; }

Making authenticated API calls

Time to put those tokens to work:

async function getCompanyInfo(accessToken) { const response = await axios.get('https://quickbooks.api.intuit.com/v3/company/{realmId}/companyinfo/{companyId}', { headers: { 'Authorization': `Bearer ${accessToken}`, 'Accept': 'application/json', }, }); return response.data; }

Error handling and edge cases

Always be prepared for the unexpected:

try { // Your API call here } catch (error) { if (error.response && error.response.status === 401) { // Time to refresh that token! } else { // Handle other errors } }

Best practices and security considerations

Remember, with great power comes great responsibility:

  • Never store tokens in plain text. Use encryption or a secure key management service.
  • Implement PKCE (Proof Key for Code Exchange) for an extra layer of security.
  • Regularly audit your token usage and implement proper logout functionality.

Conclusion

And there you have it! You've just built a solid authorization flow for your QuickBooks integration. From here, the sky's the limit. Start exploring the QuickBooks API, build out your features, and create something awesome.

Remember, the key to a great integration is attention to detail and a focus on user experience. Keep iterating, keep learning, and most importantly, keep coding!

Happy integrating, and may your tokens always be fresh and your API calls swift!