Back

Step by Step Guide to Building a Frame.io API Integration in JS

Aug 16, 20246 minute read

Introduction

Hey there, fellow dev! Ready to dive into the world of Frame.io API integration? Buckle up, because we're about to embark on a journey that'll supercharge your video collaboration workflow. Frame.io's API is a powerhouse, and we're going to harness it with some slick JavaScript. Let's get this show on the road!

Prerequisites

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

  • Node.js and npm (you're probably nodding already)
  • A Frame.io account with API credentials (if not, go grab 'em!)

Setting up the project

Alright, let's lay the groundwork:

mkdir frameio-integration && cd frameio-integration npm init -y npm install axios dotenv

Create a .env file for your API credentials:

FRAMEIO_TOKEN=your_token_here

Authentication

First things first, let's get that access token:

require('dotenv').config(); const axios = require('axios'); const api = axios.create({ baseURL: 'https://api.frame.io/v2', headers: { 'Authorization': `Bearer ${process.env.FRAMEIO_TOKEN}` } });

Pro tip: Implement token refresh to keep your integration running smoothly!

Basic API Requests

Now, let's flex those API muscles:

// GET request async function getProjects() { try { const response = await api.get('/projects'); return response.data; } catch (error) { console.error('Error fetching projects:', error); } } // POST request async function createAsset(projectId, assetData) { try { const response = await api.post(`/projects/${projectId}/assets`, assetData); return response.data; } catch (error) { console.error('Error creating asset:', error); } }

Core Functionalities

Let's tackle some key features:

Listing projects and assets

async function listProjectAssets(projectId) { const assets = await api.get(`/projects/${projectId}/assets`); console.log(assets.data); }

Uploading files

const fs = require('fs'); async function uploadFile(projectId, filePath) { const fileStream = fs.createReadStream(filePath); const response = await api.post(`/projects/${projectId}/uploads`, { name: 'awesome_video.mp4', type: 'video/mp4', filesize: fs.statSync(filePath).size }); // Use the upload URL from the response to upload the file await axios.put(response.data.upload_url, fileStream); }

Managing comments and reviews

async function addComment(assetId, text) { await api.post(`/assets/${assetId}/comments`, { text }); }

Webhooks Integration

Stay in the loop with webhooks:

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

Error Handling and Rate Limiting

Don't let errors catch you off guard:

api.interceptors.response.use( response => response, error => { if (error.response && error.response.status === 429) { // Handle rate limiting console.log('Rate limited. Retrying after cooldown...'); // Implement retry logic here } return Promise.reject(error); } );

Testing and Debugging

Keep your code in check:

const { expect } = require('chai'); describe('Frame.io API Integration', () => { it('should fetch projects successfully', async () => { const projects = await getProjects(); expect(projects).to.be.an('array'); }); });

Best Practices and Optimization

  • Cache frequently accessed data to reduce API calls
  • Use pagination for large datasets
  • Implement proper error handling and logging

Conclusion

And there you have it! You've just built a rock-solid Frame.io API integration. Remember, this is just the beginning – there's a whole world of possibilities waiting for you in the Frame.io API docs. Keep exploring, keep coding, and most importantly, keep creating awesome stuff!

Happy coding, and may your videos always render on time! 🎬✨