Back

Reading and Writing Data Using the Streak API

Aug 14, 20246 minute read

Hey there, fellow JavaScript enthusiasts! Ready to dive into the world of Streak API integration? Let's roll up our sleeves and get our hands dirty with some code.

Introduction

Streak's API is a powerful tool that lets you seamlessly integrate CRM functionality into your apps. Today, we're focusing on syncing data for a user-facing integration. Trust me, it's easier than you might think!

Authentication

First things first, let's get you authenticated. Grab your API key from the Streak dashboard and let's set up those headers:

const headers = { 'Authorization': 'Basic ' + btoa('your-api-key:'), 'Content-Type': 'application/json' };

Reading Data

Now that we're in, let's fetch some data. Here's how you can grab pipelines and boxes (deals):

async function fetchData() { const pipelinesResponse = await fetch('https://api.streak.com/v1/pipelines', { headers }); const pipelines = await pipelinesResponse.json(); const boxesResponse = await fetch('https://api.streak.com/v1/boxes', { headers }); const boxes = await boxesResponse.json(); return { pipelines, boxes }; }

Writing Data

Writing data is just as straightforward. Here's how you can create a new box and update an existing one:

async function createBox(pipelineKey, name) { const response = await fetch('https://api.streak.com/v1/pipelines/' + pipelineKey + '/boxes', { method: 'POST', headers, body: JSON.stringify({ name }) }); return await response.json(); } async function updateBox(boxKey, data) { const response = await fetch('https://api.streak.com/v1/boxes/' + boxKey, { method: 'POST', headers, body: JSON.stringify(data) }); return await response.json(); }

Syncing Data

Now, let's put it all together with a sync function. We'll implement some basic error handling and retries:

async function syncData() { const maxRetries = 3; let retries = 0; while (retries < maxRetries) { try { const localData = getLocalData(); // Implement this to get your local data const { boxes } = await fetchData(); for (const localBox of localData) { const streakBox = boxes.find(b => b.name === localBox.name); if (streakBox) { await updateBox(streakBox.key, localBox); } else { await createBox(YOUR_PIPELINE_KEY, localBox.name); } } console.log('Sync completed successfully!'); break; } catch (error) { console.error('Sync failed:', error); retries++; if (retries === maxRetries) { console.error('Max retries reached. Sync failed.'); } else { console.log(`Retrying... (${retries}/${maxRetries})`); await new Promise(resolve => setTimeout(resolve, 1000 * retries)); } } } }

Webhooks

Want real-time updates? Webhooks are your friend. Here's a basic Express.js webhook handler:

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

Best Practices

Remember, efficiency is key. Batch your requests when possible, implement smart caching strategies, and always be mindful of rate limits. Your future self (and your users) will thank you!

Conclusion

And there you have it! You're now equipped to read and write data using the Streak API like a pro. Remember, practice makes perfect, so don't be afraid to experiment and push the boundaries of what you can do.

Happy coding, and may your integrations be ever smooth and your data always in sync!