Struggling to Pass an Array of Records to Airtable in NodeJS

I am trying to add multiple records into Airtable using NodeJS. When I send a single record as an object, it works, but an array fails with a missing ‘fields’ error. Check out the updated code below:

const airtableService = new Airtable({
  apiKey: process.env.AIRTABLE_KEY
}).base(process.env.AIRTABLE_DB);

const participantList = params.participants || [];

const recordsPayload = participantList.map(p => {
  const packageDetails = params.items.find(i => i.name === p.pkgName);
  return {
    "Complete Name": p.firstName + ' ' + p.lastName,
    "First Name": p.firstName,
    "Last Name": p.lastName,
    Phone: p.contact,
    Email: p.email,
    "Tour Title": params.tour.title,
    "Start Date": dayjs(params.tour.start).format('YYYY-MM-DD'),
    "End Date": dayjs(params.tour.end).format('YYYY-MM-DD'),
    Total: participantList.length,
    "Price": formatPrice(packageDetails.price),
    "Status": "Completed"
  };
});

airtableService(process.env.AIRTABLE_TABLE_ID)
  .create(recordsPayload)
  .then(response => console.log('Record added successfully'))
  .catch(error => console.error('Error adding record:', error));

I encountered this issue when working with Airtable’s API. I discovered that the array of records must be formatted correctly, meaning each record must be an object enclosing a ‘fields’ property with the key-value pairs. In my case, wrapping the mapped record in a { fields: { … } } object resolved the issue. For instance, instead of returning the record object directly in the map, I returned { fields: { … } }. Once I made that change, bulk insert worked fine in my NodeJS project.

The challenge you’re facing is quite familiar. In my experience, the error regarding a missing ‘fields’ property arises when the payload sent to Airtable isn’t properly wrapped. I solved a similar issue by ensuring that every record in the array was structured as an object with a ‘fields’ key, which contains the actual data. The key is to map the participant list to objects in the form { fields: { … } }. This change ensured the API recognized the structure and processed the bulk insert correctly. Always verify that the data conforms to the API’s expected format.

hey, i had this too. wrapping each record in {fields: …} did the trick. airtable expects that slightly diff structure. double-check your date formats too if needed.

I faced a similar issue in one of my projects when trying to insert multiple records into Airtable. Initially, I overlooked the need to wrap each record in a ‘fields’ object, which led to recurring errors. I eventually narrowed the problem down by testing with a single record and then gradually building up to an array. I also paid attention to ensure that all field values were in the correct format, particularly the dates and prices. Once I restructured my code to create each record as { fields: { … } }, everything worked as expected.