Hey there, fellow JavaScript devs! Ready to dive into the world of SafetyCulture API? Let's get our hands dirty with some data syncing for user-facing integrations. Buckle up, because we're about to make your life a whole lot easier.
First things first, let's get you authenticated. Grab your API credentials and let's implement that OAuth 2.0 flow. Here's a quick snippet to get you started:
const axios = require('axios'); async function getAccessToken(code) { const response = await axios.post('https://api.safetyculture.io/auth/token', { grant_type: 'authorization_code', code: code, client_id: YOUR_CLIENT_ID, client_secret: YOUR_CLIENT_SECRET }); return response.data.access_token; }
Now that you're in, let's grab some data. Here's how you can fetch a user's inspections:
async function getInspections(accessToken) { const response = await axios.get('https://api.safetyculture.io/inspections/search', { headers: { 'Authorization': `Bearer ${accessToken}` } }); return response.data.inspections; }
Time to flex those writing muscles. Creating a new inspection? Here's how:
async function createInspection(accessToken, inspectionData) { const response = await axios.post('https://api.safetyculture.io/inspections', inspectionData, { headers: { 'Authorization': `Bearer ${accessToken}` } }); return response.data; }
Real-time or periodic? The choice is yours. Here's a basic sync function to get you started:
async function syncInspections(accessToken, lastSyncTime) { const inspections = await getInspections(accessToken); return inspections.filter(inspection => new Date(inspection.modified_at) > lastSyncTime); }
Don't let errors catch you off guard. Implement some retry logic:
async function apiCallWithRetry(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))); } } }
Handle large datasets like a champ with pagination:
async function getAllInspections(accessToken) { let allInspections = []; let nextToken = null; do { const response = await axios.get('https://api.safetyculture.io/inspections/search', { headers: { 'Authorization': `Bearer ${accessToken}` }, params: { next_token: nextToken } }); allInspections = allInspections.concat(response.data.inspections); nextToken = response.data.next_token; } while (nextToken); return allInspections; }
Stay up-to-date with webhooks. Here's how to process those payloads:
function handleWebhook(payload) { const inspectionId = payload.payload.inspection_id; // Do something with the updated inspection console.log(`Inspection ${inspectionId} updated`); }
Don't forget to use SafetyCulture's sandbox environment for testing. It's your playground to break things without consequences!
There you have it, folks! You're now armed with the knowledge to read and write data like a SafetyCulture API ninja. Remember, practice makes perfect, so get out there and start coding. And hey, if you get stuck, the SafetyCulture docs are your best friend.
Happy coding, and may your integrations be ever smooth and your data always in sync!