Back

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

Sep 15, 20247 minute read

Hey there, fellow JavaScript enthusiast! Ready to dive into the world of LionDesk integrations? Today, we're going to walk through building a rock-solid authorization flow for your user-facing integration. Let's get started!

Introduction

LionDesk's API is a powerful tool for real estate professionals, and building a public integration can open up a world of possibilities. The key to a successful integration? A secure and smooth authorization flow. That's what we're tackling today.

Prerequisites

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

  • A LionDesk developer account (if you don't have one, go grab it!)
  • Node.js and npm installed on your machine
  • A basic understanding of OAuth 2.0 (don't worry, we'll cover the specifics)

Setting up the project

Let's get our project off the ground:

mkdir liondesk-integration cd liondesk-integration npm init -y npm install express axios dotenv

Configuring LionDesk API credentials

Head over to your LionDesk developer dashboard and grab your Client ID and Client Secret. We'll keep these safe in a .env file:

LIONDESK_CLIENT_ID=your_client_id
LIONDESK_CLIENT_SECRET=your_client_secret
LIONDESK_REDIRECT_URI=http://localhost:3000/callback

Implementing the authorization flow

Now for the fun part! Let's set up our Express server and implement the auth flow:

require('dotenv').config(); const express = require('express'); const axios = require('axios'); const app = express(); const authorizationUrl = `https://api-v2.liondesk.com/oauth2/authorize?response_type=code&client_id=${process.env.LIONDESK_CLIENT_ID}&redirect_uri=${encodeURIComponent(process.env.LIONDESK_REDIRECT_URI)}`; app.get('/login', (req, res) => { res.redirect(authorizationUrl); }); app.get('/callback', async (req, res) => { const { code } = req.query; try { const response = await axios.post('https://api-v2.liondesk.com/oauth2/token', { grant_type: 'authorization_code', code, client_id: process.env.LIONDESK_CLIENT_ID, client_secret: process.env.LIONDESK_CLIENT_SECRET, redirect_uri: process.env.LIONDESK_REDIRECT_URI }); const { access_token, refresh_token } = response.data; // Store these tokens securely (more on this later) res.send('Authorization successful!'); } catch (error) { console.error('Error during token exchange:', error); res.status(500).send('Authorization failed'); } }); app.listen(3000, () => console.log('Server running on http://localhost:3000'));

Storing and managing tokens

Now that we've got our tokens, we need to store them securely. In a real-world scenario, you'd want to use a database for this. For now, let's keep it simple with in-memory storage:

let tokens = {}; // After successful token exchange tokens = { access_token: response.data.access_token, refresh_token: response.data.refresh_token, expires_at: Date.now() + (response.data.expires_in * 1000) };

Don't forget to implement a token refresh mechanism:

async function refreshToken() { try { const response = await axios.post('https://api-v2.liondesk.com/oauth2/token', { grant_type: 'refresh_token', refresh_token: tokens.refresh_token, client_id: process.env.LIONDESK_CLIENT_ID, client_secret: process.env.LIONDESK_CLIENT_SECRET }); tokens = { access_token: response.data.access_token, refresh_token: response.data.refresh_token, expires_at: Date.now() + (response.data.expires_in * 1000) }; } catch (error) { console.error('Error refreshing token:', error); } }

Making authenticated API requests

Now that we're authorized, let's make some API calls:

async function getLionDeskData() { if (Date.now() >= tokens.expires_at) { await refreshToken(); } try { const response = await axios.get('https://api-v2.liondesk.com/contacts', { headers: { Authorization: `Bearer ${tokens.access_token}` } }); return response.data; } catch (error) { console.error('Error fetching data:', error); } }

Error handling and edge cases

Always be prepared for the unexpected:

app.get('/revoke', async (req, res) => { try { await axios.post('https://api-v2.liondesk.com/oauth2/revoke', { token: tokens.access_token, client_id: process.env.LIONDESK_CLIENT_ID, client_secret: process.env.LIONDESK_CLIENT_SECRET }); tokens = {}; res.send('Access revoked successfully'); } catch (error) { console.error('Error revoking access:', error); res.status(500).send('Failed to revoke access'); } });

Testing the integration

Time to put our integration to the test:

  1. Start your server: node index.js
  2. Visit http://localhost:3000/login
  3. Go through the LionDesk authorization process
  4. Check if you receive the "Authorization successful!" message

Conclusion

And there you have it! You've just built a solid foundation for your LionDesk integration. Remember, this is just the beginning. From here, you can expand your integration to do all sorts of cool things with the LionDesk API.

Additional resources

Want to dive deeper? Check out:

Now go forth and build amazing things with LionDesk! Happy coding!