Hey there, fellow code wranglers! Ready to dive into the world of CompanyCam API integration? Buckle up, because we're about to embark on a journey that'll supercharge your project management capabilities. The CompanyCam API is a powerful tool that'll let you tap into a wealth of project data, photo management, and more. Let's get cracking!
Before we jump in, make sure you've got:
First things first, let's get our project off the ground:
mkdir companycam-integration cd companycam-integration npm init -y npm install axios dotenv
Create a .env
file for your API key:
COMPANYCAM_API_KEY=your_api_key_here
CompanyCam uses OAuth 2.0, but don't sweat it – we'll keep it simple with API key authentication for now. Here's how to set up your API calls:
const axios = require('axios'); require('dotenv').config(); const api = axios.create({ baseURL: 'https://api.companycam.com/v2', headers: { 'Authorization': `Bearer ${process.env.COMPANYCAM_API_KEY}`, 'Content-Type': 'application/json' } });
Now that we're all set up, let's make our first request:
async function getProjects() { try { const response = await api.get('/projects'); return response.data; } catch (error) { console.error('Error fetching projects:', error.response.data); } }
Let's implement some key features:
async function getProject(projectId) { try { const response = await api.get(`/projects/${projectId}`); return response.data; } catch (error) { console.error('Error fetching project:', error.response.data); } }
async function uploadPhoto(projectId, photoData) { try { const response = await api.post(`/projects/${projectId}/photos`, photoData); return response.data; } catch (error) { console.error('Error uploading photo:', error.response.data); } }
async function addTag(photoId, tagName) { try { const response = await api.post(`/photos/${photoId}/tags`, { name: tagName }); return response.data; } catch (error) { console.error('Error adding tag:', error.response.data); } }
CompanyCam supports webhooks for real-time updates. Here's a quick Express server to handle them:
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'));
Always be prepared for the unexpected:
async function makeApiCall(endpoint, method = 'get', data = null) { try { const response = await api[method](endpoint, data); return response.data; } catch (error) { if (error.response && error.response.status === 429) { // Rate limited, wait and retry await new Promise(resolve => setTimeout(resolve, 1000)); return makeApiCall(endpoint, method, data); } throw error; } }
Don't forget to test your integration! Here's a simple test using Jest:
test('fetches projects successfully', async () => { const projects = await getProjects(); expect(projects).toBeDefined(); expect(Array.isArray(projects.items)).toBe(true); });
When deploying your integration:
And there you have it, folks! You're now armed with the knowledge to build a robust CompanyCam API integration. Remember, the key to a great integration is understanding the API docs, handling errors gracefully, and optimizing for performance. Now go forth and code something amazing!
For more in-depth info, check out the CompanyCam API docs. Happy coding!