Duplicate records being generated when sending data to Airtable via API

I’m working on a script that transfers student data from our system to Airtable through their API. The data transfer works fine, but I’m getting duplicate entries created in Airtable even though my payload doesn’t contain any duplicates when I check it.

Here’s my code that identifies which students need to be added:

for (let student of studentArray) {
    if (student.enrollment_status === 'active' || student.enrollment_status === 'pending') {
        const isNewStudent = !existingRecords.find(record => record.student_id === student.student_id);
        if (isNewStudent) {
            studentsToAdd.records.push({
                fields: {
                    ...student
                },
            });
        }
    }
    
    sendToAirtable('appXYZ123','Enrollments', studentsToAdd)
}

Function that handles the API request:

function sendToAirtable(baseId, table, data) {
    var requestOptions = {
        method: 'POST',
        headers: {
            'Authorization': 'Bearer ' + apiKey,
            'Content-Type': 'application/json',
            "Accept": "application/json"
        },
        payload: JSON.stringify(data)
    };
    
    var result = UrlFetchApp.fetch('https://api.airtable.com/v0/' + baseId + "/" + encodeURIComponent(table), requestOptions).getContentText();
    return result;
}

Any ideas why duplicates keep showing up?

You’re calling sendToAirtable inside the loop - that’s your problem. Each student record triggers its own API request, creating a race condition. Multiple requests process at once before your existingRecords array updates with the new entries.

Move sendToAirtable outside the loop and batch all students together in one call. If you absolutely need individual sends, add delays between API calls or refresh existingRecords before each batch to get the current state.

I hit the same issue with our inventory system. Switched to batch processing and the duplicates disappeared instantly.

yeah, i noticed the same! sending students individually can cause duplicates. try batching your records and call sendToAirtable after the loop is done - it’ll send them all at once and should solve your prob!

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.