Back

Reading and Writing Data Using the BambooHR API

Aug 14, 20246 minute read

Hey there, fellow JavaScript devs! Ready to dive into the world of BambooHR API integration? Let's get our hands dirty with some code and learn how to sync data for a user-facing integration. Buckle up!

Introduction

BambooHR's API is a powerful tool that lets us tap into their HR management system. We'll be focusing on how to read and write data, perfect for building that slick integration you've been dreaming about.

Authentication

First things first, let's get that authentication sorted. BambooHR uses API key authentication, which is pretty straightforward. Here's how you set up those headers:

const headers = new Headers({ 'Authorization': 'Basic ' + btoa(apiKey + ':x'), 'Accept': 'application/json' });

Easy peasy, right? Just remember to keep that API key safe!

Reading Data

Now, let's fetch some employee data. BambooHR uses pagination, so we'll need to handle that. Check this out:

async function getEmployees(page = 1) { const response = await fetch(`https://api.bamboohr.com/api/gateway.php/your-subdomain/v1/employees/directory?page=${page}`, { headers }); const data = await response.json(); if (data.employees.length === 0) return []; return [...data.employees, ...await getEmployees(page + 1)]; }

This recursive function will fetch all your employees. Neat, huh?

Writing Data

Updating employee info is just as easy. Here's a quick example:

async function updateEmployee(id, data) { const response = await fetch(`https://api.bamboohr.com/api/gateway.php/your-subdomain/v1/employees/${id}`, { method: 'POST', headers, body: JSON.stringify(data) }); return response.json(); }

Syncing Data

Now, let's put it all together with a sync function:

async function syncEmployees() { try { const employees = await getEmployees(); for (const employee of employees) { // Your sync logic here await updateEmployee(employee.id, { /* updated data */ }); } console.log('Sync completed successfully!'); } catch (error) { console.error('Sync failed:', error); } }

Remember to be mindful of rate limits. You might want to add some delays between requests if you're dealing with a lot of data.

Webhooks

Webhooks are your best friend for real-time updates. Here's a basic Express endpoint to handle them:

app.post('/bamboohr-webhook', (req, res) => { const { type, employeeId } = req.body; console.log(`Received ${type} event for employee ${employeeId}`); // Handle the event res.sendStatus(200); });

Error Handling and Logging

Always be prepared for things to go wrong. Here's a more robust version of our update function:

async function updateEmployee(id, data) { try { const response = await fetch(`https://api.bamboohr.com/api/gateway.php/your-subdomain/v1/employees/${id}`, { method: 'POST', headers, body: JSON.stringify(data) }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return response.json(); } catch (error) { console.error(`Failed to update employee ${id}:`, error); // You might want to add more sophisticated logging here throw error; } }

Best Practices

  1. Cache data where possible to reduce API calls.
  2. Use environment variables for API keys.
  3. Implement exponential backoff for rate limit handling.

Conclusion

And there you have it! You're now equipped to build a robust BambooHR integration. Remember, the key to a great integration is understanding the API's quirks and handling edge cases gracefully.

Keep coding, keep learning, and don't forget to check out BambooHR's API docs for more advanced features. Happy integrating!