Back

Reading and Writing Data Using the Fireflies.ai API

Aug 14, 20247 minute read

Hey there, fellow JavaScript devs! Ready to supercharge your app with some sweet transcription capabilities? Let's dive into integrating the Fireflies.ai API for a user-facing experience that'll knock your socks off.

Authentication: Your Golden Ticket

First things first, you'll need to grab those API credentials. Head over to the Fireflies.ai developer portal and snag your API key. If you're dealing with user-specific data, you might need to implement OAuth 2.0. Here's a quick example of how to set up your API calls:

const axios = require('axios'); const firefliesApi = axios.create({ baseURL: 'https://api.fireflies.ai/graphql', headers: { 'Authorization': 'Bearer YOUR_API_KEY_HERE', 'Content-Type': 'application/json' } });

Reading Data: Fetch Those Transcripts!

Now that we're all set up, let's grab some transcripts. Here's how you can fetch a user's transcripts:

async function getTranscripts() { const query = ` query { transcripts { id title created_at } } `; try { const response = await firefliesApi.post('', { query }); return response.data.data.transcripts; } catch (error) { console.error('Error fetching transcripts:', error); } }

Want details on a specific transcript? No sweat:

async function getTranscriptDetails(id) { const query = ` query { transcript(id: "${id}") { id title text speakers { name email } } } `; try { const response = await firefliesApi.post('', { query }); return response.data.data.transcript; } catch (error) { console.error('Error fetching transcript details:', error); } }

Writing Data: Create and Update Like a Boss

Creating a new transcript? Coming right up:

async function createTranscript(title, audioUrl) { const mutation = ` mutation { createTranscript(input: { title: "${title}", audio_url: "${audioUrl}" }) { id title } } `; try { const response = await firefliesApi.post('', { query: mutation }); return response.data.data.createTranscript; } catch (error) { console.error('Error creating transcript:', error); } }

Need to update an existing transcript? We've got you covered:

async function updateTranscript(id, newTitle) { const mutation = ` mutation { updateTranscript(id: "${id}", input: { title: "${newTitle}" }) { id title } } `; try { const response = await firefliesApi.post('', { query: mutation }); return response.data.data.updateTranscript; } catch (error) { console.error('Error updating transcript:', error); } }

Syncing Data: Stay Up-to-Date with Webhooks

To keep your data fresh, set up a webhook listener. Here's a quick Express.js example:

const express = require('express'); const app = express(); app.post('/webhook', express.json(), (req, res) => { const { event, data } = req.body; switch (event) { case 'transcript.created': console.log('New transcript created:', data.id); // Handle new transcript break; case 'transcript.updated': console.log('Transcript updated:', data.id); // Handle updated transcript break; // Add more cases as needed } res.sendStatus(200); }); app.listen(3000, () => console.log('Webhook listener running on port 3000'));

Error Handling and Rate Limiting: Play Nice with the API

Always check for errors and respect those rate limits, folks:

async function makeApiCall() { try { const response = await firefliesApi.post('', { query: 'YOUR_QUERY_HERE' }); const rateLimitRemaining = response.headers['x-ratelimit-remaining']; if (rateLimitRemaining < 10) { console.warn('Approaching rate limit, slow down!'); } return response.data; } catch (error) { if (error.response && error.response.status === 429) { console.error('Rate limit exceeded. Try again later.'); } else { console.error('API error:', error.message); } } }

Best Practices: Optimize Like a Pro

  1. Cache frequently accessed data to reduce API calls.
  2. Batch operations when possible to minimize requests.
  3. Use GraphQL to fetch only the data you need, nothing more.

Wrapping Up

And there you have it! You're now equipped to read, write, and sync data like a Fireflies.ai API ninja. Remember, with great power comes great responsibility – use these skills wisely and build something awesome!

Got questions? Feeling stuck? Don't sweat it – the Fireflies.ai docs are your best friend. Now go forth and code, you magnificent developer, you!