Need help with batch inserting data to Airtable
I’m working on a project where I can successfully add one record to my Airtable base, but I can’t figure out how to add multiple records at once.
Here’s my working code for single record creation:
public static async Task CreateSingleEntry()
{
using (var client = new HttpClient())
{
using (var req = new HttpRequestMessage(new HttpMethod("POST"), "https://api.airtable.com/v0/BASE_ID/TableName"))
{
req.Headers.TryAddWithoutValidation("Authorization", "Bearer YOUR_TOKEN");
req.Content = new StringContent("{\"records\": [{ \"fields\": { \"Name\": \"Sample Entry\" }}]}");
req.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
var result = await client.SendAsync(req);
}
}
}
I’ve created these classes for handling the data structure:
using System.Collections.Generic;
using System.Text.Json.Serialization;
namespace MyProject
{
public class AirtableModel
{
public class ItemFields
{
[JsonPropertyName("Name")]
[JsonInclude]
public string Name { get; set; }
}
public class DataRecord
{
[JsonPropertyName("fields")]
[JsonInclude]
public ItemFields fields { get; set; }
}
public class RequestBody
{
[JsonPropertyName("records")]
[JsonInclude]
public List<DataRecord> records { get; set; }
}
}
}
The expected JSON format should look like this:
{
"records": [
{
"id": "rec123ABC",
"fields": {
"Name": "First Item"
}
},
{
"id": "rec456DEF",
"fields": {
"Name": "Second Item"
}
}
]
}
How can I populate my model from a data source like a list or grid and serialize it properly for the API request? Any guidance would be appreciated.