Back

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

Sep 14, 20247 minute read

Hey there, fellow JavaScript enthusiasts! Ready to dive into the world of Thryv integrations? Today, we're going to walk through building a rock-solid authorization flow for your user-facing Thryv integration. Buckle up, because we're about to make your integration dreams a reality!

Introduction

Thryv is a powerhouse when it comes to business management software, and integrating with it can open up a world of possibilities for your app. The key to a smooth integration? A bulletproof auth flow. That's what we're tackling today, so let's get to it!

Prerequisites

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

  • A Thryv Developer account (if you don't have one, go grab it!)
  • A solid grasp on OAuth 2.0 (we'll be using it extensively)
  • Node.js and Express.js set up and ready to roll

Got all that? Great! Let's build something awesome.

Setting up the project

First things first, let's get our ducks in a row:

  1. Head over to the Thryv Developer Portal and create a new application.
  2. Snag your client ID and client secret - you'll need these bad boys.
  3. Set up your environment variables. Keep those secrets safe!
require('dotenv').config(); const CLIENT_ID = process.env.THRYV_CLIENT_ID; const CLIENT_SECRET = process.env.THRYV_CLIENT_SECRET;

Implementing the Authorization Flow

Initiating the OAuth process

Time to kick things off! We'll create an authorization URL and send our users on a little trip to Thryv's authorization page.

const authorizationUrl = `https://login.thryv.com/oauth2/authorize?client_id=${CLIENT_ID}&response_type=code&redirect_uri=${REDIRECT_URI}`; app.get('/auth', (req, res) => { res.redirect(authorizationUrl); });

Handling the callback

When our users come back from their Thryv adventure, we need to be ready. Let's set up that redirect URI endpoint and grab that sweet, sweet authorization code.

app.get('/callback', async (req, res) => { const { code } = req.query; // Now, let's exchange this code for an access token! });

Exchanging the code for access token

Alright, time for the main event! We'll trade in our authorization code for an access token.

const tokenResponse = await axios.post('https://login.thryv.com/oauth2/token', { grant_type: 'authorization_code', client_id: CLIENT_ID, client_secret: CLIENT_SECRET, code, redirect_uri: REDIRECT_URI }); const { access_token, refresh_token } = tokenResponse.data; // Store these tokens securely - they're your golden tickets!

Refreshing the access token

Access tokens don't last forever, so let's implement a refresh mechanism to keep the party going.

async function refreshAccessToken(refresh_token) { const response = await axios.post('https://login.thryv.com/oauth2/token', { grant_type: 'refresh_token', client_id: CLIENT_ID, client_secret: CLIENT_SECRET, refresh_token }); return response.data.access_token; }

Error Handling and Security Considerations

Security is no joke, folks. Let's add some PKCE (Proof Key for Code Exchange) to our flow and handle those pesky errors like pros.

const crypto = require('crypto'); function generateCodeVerifier() { return crypto.randomBytes(32).toString('hex'); } function generateCodeChallenge(verifier) { return crypto.createHash('sha256').update(verifier).digest('base64') .replace(/\+/g, '-') .replace(/\//g, '_') .replace(/=/g, ''); }

Don't forget to add error handling to your token requests. Nobody likes a crashy app!

Testing the Authorization Flow

Time to put on your user hat and take this baby for a spin. Walk through the flow, make sure you're getting those tokens, and verify that refresh is working smoothly.

Next Steps

Congrats, you've got your auth flow up and running! Now the real fun begins. Use that access token to make some API calls and start building out your user-specific functionality. The sky's the limit!

Conclusion

And there you have it, folks! You've just built a rock-solid authorization flow for your Thryv integration. Remember, a secure and efficient auth process is the backbone of any great integration. Keep iterating, keep improving, and most importantly, keep building awesome stuff!

Now go forth and integrate with confidence. You've got this! 🚀