Back

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

Aug 16, 20246 minute read

Hey there, fellow JavaScript enthusiast! Ready to dive into the world of WhatConverts integration? Let's focus on the most crucial part: building a rock-solid authorization flow. Buckle up, because we're about to make your integration secure and user-friendly in no time.

Introduction

WhatConverts is a powerful tool for tracking and analyzing your marketing efforts. But before we can tap into its potential, we need to set up a bulletproof authorization system. Trust me, it's not as daunting as it sounds!

Prerequisites

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

  • WhatConverts API credentials (if you don't have them, go grab 'em!)
  • A Node.js environment with Express.js set up (I know you've got this covered)

Setting up the OAuth 2.0 flow

First things first, let's get you registered with WhatConverts:

  1. Head over to the WhatConverts developer portal
  2. Register your application
  3. Snag your client ID and client secret

Keep these safe – they're your golden tickets!

Implementing the authorization request

Time to construct that authorization URL:

const authUrl = `https://app.whatconverts.com/oauth/authorize? client_id=${clientId}& redirect_uri=${encodeURIComponent(redirectUri)}& response_type=code& scope=read_leads`; res.redirect(authUrl);

This will send your users to WhatConverts for authentication. Easy peasy!

Handling the callback

Set up your redirect URI endpoint:

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

Now, let's trade that code for an access token:

const tokenResponse = await axios.post('https://app.whatconverts.com/oauth/token', { client_id: clientId, client_secret: clientSecret, code, grant_type: 'authorization_code', redirect_uri: redirectUri }); const { access_token, refresh_token } = tokenResponse.data;

Boom! You've got your access token. Feel the power!

Refreshing the access token

Don't forget to keep that token fresh:

async function refreshToken(refresh_token) { const response = await axios.post('https://app.whatconverts.com/oauth/token', { client_id: clientId, client_secret: clientSecret, refresh_token, grant_type: 'refresh_token' }); return response.data.access_token; }

Securing the token storage

Security first! Store those tokens safely:

// Use environment variables const accessToken = process.env.ACCESS_TOKEN; const refreshToken = process.env.REFRESH_TOKEN; // Or use a secure key management system // const { accessToken, refreshToken } = await secretsManager.getSecrets();

Making authenticated API requests

Now you're ready to make some API calls:

const response = await axios.get('https://app.whatconverts.com/api/v1/leads', { headers: { Authorization: `Bearer ${accessToken}` } });

Remember to handle rate limits and errors gracefully!

Error handling and edge cases

Always be prepared:

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

Testing the integration

Set up a test environment and put your auth flow through its paces. Trust me, your future self will thank you!

Conclusion

And there you have it! You've just built a robust authorization flow for your WhatConverts integration. Pat yourself on the back – you've taken a big step towards creating a powerful, secure integration.

Next up: start building out those amazing features that'll make your integration shine. The sky's the limit now that you've got the auth flow nailed down.

Happy coding, and may your API calls always return 200 OK! 🚀