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!
Before we jump in, make sure you've got:
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
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!
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); } }
Let's tackle some key features:
async function listProjectAssets(projectId) { const assets = await api.get(`/projects/${projectId}/assets`); console.log(assets.data); }
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); }
async function addComment(assetId, text) { await api.post(`/assets/${assetId}/comments`, { text }); }
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'));
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); } );
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'); }); });
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! 🎬✨