Back

Reading and Writing Data Using the Recruitee API

Aug 17, 20246 minute read

Hey there, fellow JavaScript enthusiasts! Ready to dive into the world of Recruitee API integration? Let's roll up our sleeves and get our hands dirty with some code. We'll focus on syncing data for a user-facing integration, so buckle up!

Authentication: Your Golden Ticket

First things first, you'll need to grab your API credentials. Head over to your Recruitee account settings and snag that API key. If you're dealing with OAuth 2.0, here's a quick snippet to get you started:

const getAccessToken = async () => { const response = await axios.post('https://api.recruitee.com/oauth/token', { client_id: YOUR_CLIENT_ID, client_secret: YOUR_CLIENT_SECRET, grant_type: 'client_credentials' }); return response.data.access_token; };

Reading Data: Fetch Like a Pro

Now that you're in, let's grab some data. Here's how you might fetch candidates:

const getCandidates = async (accessToken) => { const response = await axios.get('https://api.recruitee.com/c/YOUR_COMPANY_ID/candidates', { headers: { Authorization: `Bearer ${accessToken}` } }); return response.data.candidates; };

Pro tip: Keep an eye on those rate limits and implement pagination to avoid any nasty surprises!

Writing Data: Create and Update with Ease

Time to add some data to the mix. Here's a quick example of creating a new candidate:

const createCandidate = async (accessToken, candidateData) => { const response = await axios.post('https://api.recruitee.com/c/YOUR_COMPANY_ID/candidates', { candidate: candidateData }, { headers: { Authorization: `Bearer ${accessToken}` } }); return response.data.candidate; };

Syncing Strategies: Stay Up to Date

Webhooks are your best friend for real-time updates. Set them up in your Recruitee account and listen for those sweet, sweet notifications:

app.post('/webhook', (req, res) => { const event = req.body; // Handle the event based on its type if (event.type === 'candidate.created') { // Sync the new candidate } res.sendStatus(200); });

Error Handling and Logging: Don't Let Errors Ruin Your Day

Always expect the unexpected. Implement retry logic for those pesky network hiccups:

const apiCall = async (fn, maxRetries = 3) => { for (let i = 0; i < maxRetries; i++) { try { return await fn(); } catch (error) { if (i === maxRetries - 1) throw error; await new Promise(resolve => setTimeout(resolve, 1000 * Math.pow(2, i))); } } };

Performance Optimization: Speed Demon

Cache frequently accessed data to keep things zippy:

const cache = new Map(); const getCachedData = async (key, fetchFn) => { if (cache.has(key)) return cache.get(key); const data = await fetchFn(); cache.set(key, data); return data; };

Testing and Validation: Trust, but Verify

Always test your API interactions. Here's a quick Jest test to get you started:

jest.mock('axios'); test('getCandidates returns candidates', async () => { axios.get.mockResolvedValue({ data: { candidates: [{ id: 1, name: 'John Doe' }] } }); const candidates = await getCandidates('fake_token'); expect(candidates).toHaveLength(1); expect(candidates[0].name).toBe('John Doe'); });

Wrapping Up

And there you have it, folks! You're now armed with the knowledge to build a robust Recruitee API integration. Remember, the key to a great integration is consistent syncing, error handling, and performance optimization. Now go forth and code!

For more details, check out the Recruitee API docs. Happy coding, and may your API calls always return 200 OK!