Back

Step by Step Guide to Building a Bonjoro API Integration in JS

Aug 15, 20246 minute read

Introduction

Hey there, fellow code wrangler! Ready to supercharge your app with some personalized video magic? Let's dive into integrating the Bonjoro API into your JavaScript project. Bonjoro's API lets you programmatically create and send video messages, manage campaigns, and pull analytics data. Buckle up, because we're about to make your app a whole lot more engaging!

Prerequisites

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

  • Node.js and npm (you're probably nodding already)
  • A Bonjoro API key (if you don't have one, hop over to their site and grab it)
  • Your HTTP request library of choice (we'll use Axios in our examples, but feel free to use fetch if that's your jam)

Setting Up the Project

Let's get this show on the road:

mkdir bonjoro-integration cd bonjoro-integration npm init -y npm install axios dotenv

Create a .env file in your project root and add your API key:

BONJORO_API_KEY=your_api_key_here

Authentication

Time to get cozy with the Bonjoro API. Create an api.js file:

require('dotenv').config(); const axios = require('axios'); const api = axios.create({ baseURL: 'https://api.bonjoro.com/v2', headers: { 'Authorization': `Bearer ${process.env.BONJORO_API_KEY}`, 'Content-Type': 'application/json' } }); module.exports = api;

Basic API Requests

Now, let's flex those API muscles:

const api = require('./api'); // GET request async function getCampaigns() { try { const response = await api.get('/campaigns'); return response.data; } catch (error) { console.error('Error fetching campaigns:', error.response.data); } } // POST request async function createMessage(campaignId, recipientEmail, messageText) { try { const response = await api.post('/messages', { campaign_id: campaignId, recipient_email: recipientEmail, message: messageText }); return response.data; } catch (error) { console.error('Error creating message:', error.response.data); } }

Handling Responses

Bonjoro's API speaks JSON, so let's make sure we're listening:

async function handleApiCall(apiFunction) { try { const data = await apiFunction(); console.log('Success:', data); return data; } catch (error) { if (error.response) { console.error('API Error:', error.response.status, error.response.data); } else { console.error('Network Error:', error.message); } } }

Implementing Key Bonjoro Features

Let's put it all together and create a video message:

async function sendVideoMessage(campaignId, recipientEmail, videoUrl) { try { const message = await createMessage(campaignId, recipientEmail, 'Check out this video!'); const response = await api.post(`/messages/${message.id}/video`, { video_url: videoUrl }); console.log('Video message sent:', response.data); } catch (error) { console.error('Error sending video message:', error.response.data); } }

Webhooks (if applicable)

If you're using webhooks, here's a quick Express server to handle them:

const express = require('express'); const app = express(); app.use(express.json()); app.post('/webhook', (req, res) => { const event = req.body; console.log('Received webhook:', event); // Process the event res.sendStatus(200); }); app.listen(3000, () => console.log('Webhook server running on port 3000'));

Best Practices

  • Respect rate limits: Bonjoro's not a fan of spam, so keep an eye on those API calls.
  • Cache when you can: Save those API calls for when you really need them.
  • Keep it secret, keep it safe: Never expose your API key in client-side code.

Testing

Let's make sure everything's ship-shape:

const api = require('./api'); const nock = require('nock'); describe('Bonjoro API', () => { it('should fetch campaigns', async () => { nock('https://api.bonjoro.com/v2') .get('/campaigns') .reply(200, { campaigns: [] }); const campaigns = await getCampaigns(); expect(campaigns).toHaveProperty('campaigns'); }); });

Conclusion

And there you have it, folks! You've just turbocharged your app with Bonjoro's video messaging prowess. Remember, this is just scratching the surface - there's a whole world of personalized video goodness waiting for you in the Bonjoro API docs.

Now go forth and make those user interactions unforgettable! Happy coding! 🚀📹