Hey there, fellow dev! Ready to supercharge your project management with Teamwork's API? Let's dive in and build a slick integration that'll have your team collaborating like never before. We'll be using JavaScript, so buckle up and let's get coding!
Before we jump in, make sure you've got:
First things first, let's get our project off the ground:
mkdir teamwork-api-integration cd teamwork-api-integration npm init -y npm install axios dotenv
Security first! Let's keep that API key safe and sound:
.env
file in your project root:TEAMWORK_API_KEY=your_api_key_here
TEAMWORK_DOMAIN=your_teamwork_domain.com
require('dotenv').config(); const axios = require('axios'); const client = axios.create({ baseURL: `https://${process.env.TEAMWORK_DOMAIN}`, auth: { username: process.env.TEAMWORK_API_KEY, password: 'X' } });
Time to flex those API muscles! Let's start with some CRUD operations:
async function getProjects() { try { const response = await client.get('/projects.json'); return response.data.projects; } catch (error) { console.error('Error fetching projects:', error); } }
async function createTask(projectId, taskData) { try { const response = await client.post(`/projects/${projectId}/tasks.json`, { 'todo-item': taskData }); return response.data; } catch (error) { console.error('Error creating task:', error); } }
async function updateTask(taskId, updateData) { try { const response = await client.put(`/tasks/${taskId}.json`, { 'todo-item': updateData }); return response.data; } catch (error) { console.error('Error updating task:', error); } }
async function deleteTask(taskId) { try { await client.delete(`/tasks/${taskId}.json`); console.log('Task deleted successfully'); } catch (error) { console.error('Error deleting task:', error); } }
Let's add some robustness to our code:
async function makeApiCall(method, endpoint, data = null) { try { const response = await client[method](endpoint, data); return response.data; } catch (error) { if (error.response && error.response.status === 429) { // Handle rate limiting const retryAfter = error.response.headers['retry-after']; console.log(`Rate limited. Retrying after ${retryAfter} seconds`); await new Promise(resolve => setTimeout(resolve, retryAfter * 1000)); return makeApiCall(method, endpoint, data); } throw error; } }
For those data-heavy requests:
async function getAllProjects() { let page = 1; let allProjects = []; while (true) { const response = await makeApiCall('get', `/projects.json?page=${page}`); allProjects = allProjects.concat(response.projects); if (response.projects.length < 50) break; // Assuming 50 items per page page++; } return allProjects; }
Stay in the loop with real-time updates:
const express = require('express'); const app = express(); app.post('/webhook', express.json(), (req, res) => { console.log('Webhook received:', req.body); // Handle the webhook payload res.sendStatus(200); }); app.listen(3000, () => console.log('Webhook server running on port 3000'));
Don't forget to test! Here's a quick example using Jest:
const { getProjects } = require('./teamwork-api'); test('fetches projects successfully', async () => { const projects = await getProjects(); expect(Array.isArray(projects)).toBe(true); expect(projects.length).toBeGreaterThan(0); });
And there you have it! You've just built a robust Teamwork API integration. Remember, this is just the tip of the iceberg. Teamwork's API has a ton more features to explore, so don't be afraid to dive deeper.
Keep coding, keep learning, and most importantly, keep making awesome stuff! If you need more info, Teamwork's API docs are your new best friend. Happy integrating!