Back

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

Jul 31, 20247 minute read

Hey there, fellow JavaScript enthusiast! Ready to dive into the world of Typeform integrations? Today, we're going to tackle one of the most crucial parts of building a public integration: the authorization flow. Buckle up, because we're about to make your integration secure and user-friendly in no time!

Prerequisites

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

  • A Typeform Developer account (if you don't have one, go grab it!)
  • A solid grasp on OAuth 2.0 (don't worry, we'll refresh your memory)
  • Node.js and Express.js set up and ready to roll

Setting up the Typeform App

First things first, let's get your app set up in the Typeform Developer Portal:

  1. Head over to the Typeform Developer Portal and create a new app.
  2. Configure your redirect URIs (we'll use http://localhost:3000/callback for this guide).
  3. Choose the scopes you need (we'll use forms:read for this example).

Great! Now you've got your client ID and secret. Keep these safe – they're your keys to the Typeform kingdom!

Implementing the Authorization Flow

Initiating the OAuth process

Let's kick things off by sending users to Typeform's authorization page:

const authUrl = `https://api.typeform.com/oauth/authorize?client_id=${clientId}&redirect_uri=${redirectUri}&scope=forms:read&state=${generateRandomState()}`; res.redirect(authUrl);

Pro tip: That state parameter? It's your CSRF attack shield. Don't skip it!

Handling the callback

When the user comes back, they'll bring a shiny authorization code. Let's exchange it for an access token:

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

Storing and managing tokens

Now that you've got the tokens, treat them like gold. Store them securely (please, not in plain text!) and implement a refresh mechanism:

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

Making Authenticated Requests

Time to put that access token to work! Let's fetch the user's forms:

async function getForms(accessToken) { const response = await axios.get('https://api.typeform.com/forms', { headers: { Authorization: `Bearer ${accessToken}` } }); return response.data; }

Error Handling and Edge Cases

Always be prepared! Handle expired tokens gracefully:

try { const forms = await getForms(accessToken); } catch (error) { if (error.response && error.response.status === 401) { const newAccessToken = await refreshAccessToken(refreshToken); // Update stored token and retry the request } }

Security Considerations

Remember, with great power comes great responsibility:

  • Never expose your client secret on the client-side.
  • Always validate the state parameter in your callback to prevent CSRF attacks.
  • Use HTTPS in production. Always.

Testing the Integration

Before you pop the champagne, give your integration a thorough test:

  1. Try the happy path (everything works perfectly).
  2. Attempt to use an expired token.
  3. Simulate a user denying access.

Consider setting up some automated tests to catch any future hiccups!

Conclusion

And there you have it! You've just built a rock-solid authorization flow for your Typeform integration. You're now ready to create amazing experiences for your users with the power of Typeform at your fingertips.

Remember, this is just the beginning. There's a whole world of Typeform API endpoints waiting for you to explore. So go forth and integrate!

Additional Resources

Want to dive deeper? Check out:

Happy coding, and may your forms be ever responsive!