Back

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

Jul 17, 20246 minute read

Hey there, fellow JavaScript developer! Ready to dive into the world of HubSpot integrations? Today, we're going to tackle one of the most crucial parts of building a public integration: the authorization flow. Don't worry, it's not as daunting as it sounds. Let's break it down step by step and get you on your way to creating a secure, user-facing integration.

Prerequisites

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

  • A HubSpot developer account (if you don't have one, go grab it!)
  • A basic understanding of OAuth 2.0 (don't sweat it if you're rusty, we'll cover the essentials)
  • Node.js and Express.js set up and ready to go

Setting up the HubSpot App

First things first, let's get your app set up in HubSpot:

  1. Head over to the HubSpot developer portal and create a new app.
  2. Configure your OAuth scopes. Think about what your app needs access to and choose wisely!
  3. Set your redirect URI. This is where HubSpot will send your users after they've authorized your app.

Implementing the Authorization Flow

Now for the fun part – let's build that auth flow!

Initiating the OAuth process

We'll start by generating an authorization URL and redirecting your user to HubSpot's login page:

const authUrl = `https://app.hubspot.com/oauth/authorize?client_id=${clientId}&redirect_uri=${redirectUri}&scope=${scopes}`; res.redirect(authUrl);

Handling the callback

Once the user grants permission, HubSpot will redirect them back to your app with an authorization code. Let's grab that code and exchange it for access and refresh tokens:

app.get('/oauth-callback', async (req, res) => { const { code } = req.query; const tokenResponse = await axios.post('https://api.hubapi.com/oauth/v1/token', { grant_type: 'authorization_code', client_id: clientId, client_secret: clientSecret, redirect_uri: redirectUri, code: code }); // Store these tokens securely! const { access_token, refresh_token } = tokenResponse.data; });

Managing Token Lifecycle

Remember, access tokens don't last forever. You'll need to refresh them periodically:

async function refreshAccessToken(refreshToken) { const tokenResponse = await axios.post('https://api.hubapi.com/oauth/v1/token', { grant_type: 'refresh_token', client_id: clientId, client_secret: clientSecret, refresh_token: refreshToken }); return tokenResponse.data.access_token; }

Best Practices

Let's level up your security game:

  1. Use the state parameter to prevent CSRF attacks:
const state = crypto.randomBytes(16).toString('hex'); // Include state in your authUrl and verify it in the callback
  1. Implement PKCE (Proof Key for Code Exchange) for an extra layer of security.

Error Handling

OAuth can throw some curveballs. Be prepared to catch and handle common errors like invalid_grant or invalid_client. Your users will thank you for the smooth experience!

Testing the Integration

Before you pop the champagne, let's make sure everything's working:

  1. Use HubSpot's OAuth playground to test your flow.
  2. Verify that you can exchange tokens and access the API successfully.

Wrapping Up

And there you have it! You've just built a secure authorization flow for your HubSpot integration. Pretty cool, right? Remember, this is just the beginning. From here, you can start building out the rest of your integration, adding features, and making it your own.

Keep exploring the HubSpot API docs and don't be afraid to experiment. You've got this!

Additional Resources

Happy coding, and may your integrations be ever awesome!