Back

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

Aug 15, 20246 minute read

Hey there, fellow JavaScript enthusiast! Ready to dive into the world of Hotmart integrations? Let's roll up our sleeves and build a rock-solid authorization flow that'll make your users feel safe and your integration shine. We'll keep things concise and focused, just the way we like it.

Introduction

Hotmart's a powerhouse in the digital product world, and integrating with it can open up some exciting possibilities. But before we can play with all the cool features, we need to nail the authorization flow. It's like the bouncer at an exclusive club – gotta get past it to enjoy the party inside.

Prerequisites

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

  • A Hotmart Developer account (if you don't have one, go grab it!)
  • Node.js and Express.js set up and ready to go
  • A basic understanding of OAuth 2.0 (don't worry, we'll refresh your memory as we go)

Setting up the project

First things first:

  1. Head over to the Hotmart Developer Portal and create a new application.
  2. Snag your client ID and client secret – treat these like your secret sauce.
  3. Set up your redirect URI. This is where Hotmart will send your users after they've granted permission.

Implementing the Authorization Flow

Initiating the OAuth request

Let's kick things off by constructing that authorization URL:

const authUrl = `https://api-sec-vlc.hotmart.com/security/oauth/authorize?client_id=${clientId}&redirect_uri=${redirectUri}&response_type=code&scope=all`; // Redirect the user to Hotmart's authorization page res.redirect(authUrl);

Handling the callback

Now, set up an endpoint to catch that callback:

app.get('/callback', async (req, res) => { const { code } = req.query; // We'll use this code in the next step });

Exchanging the code for access token

Time to trade that code for the real prize – the access token:

const tokenResponse = await axios.post('https://api-sec-vlc.hotmart.com/security/oauth/token', { grant_type: 'authorization_code', client_id: clientId, client_secret: clientSecret, code, redirect_uri: redirectUri }); const { access_token, refresh_token } = tokenResponse.data; // Store these tokens securely

Refreshing the access token

Don't forget to keep that token fresh:

const refreshTokenResponse = await axios.post('https://api-sec-vlc.hotmart.com/security/oauth/token', { grant_type: 'refresh_token', client_id: clientId, client_secret: clientSecret, refresh_token: storedRefreshToken }); // Update your stored tokens

Securing the Integration

Security's no joke, so:

  • Never, ever store tokens in plain text. Use encryption or a secure key management system.
  • Implement PKCE (Proof Key for Code Exchange) for an extra layer of security.

Testing the Authorization Flow

Take your shiny new auth flow for a spin:

  1. Start at your app's login page.
  2. Click through to Hotmart's authorization page.
  3. Grant permissions and watch the magic happen as you're redirected back.
  4. Verify that you've got your hands on those precious tokens.

Error Handling and Edge Cases

Life's not always smooth sailing, so prepare for:

  • Expired tokens (hint: that's where your refresh flow comes in handy)
  • Users changing their minds and denying access
  • Network hiccups and other gremlins that might pop up

Conclusion

And there you have it! You've just built a robust authorization flow for your Hotmart integration. With this foundation, you're all set to start making those API requests and building something awesome.

Additional Resources

Want to dive deeper? Check out:

Now go forth and integrate! Remember, every great app starts with a solid foundation, and you've just laid yours. Happy coding!