How to send multiple records to Airtable using C# and REST API?

Hey everyone,

I’m learning about APIs and JSON, and I’m stuck on a challenge. I want to post multiple records at once using Airtable’s REST API. I managed to send one record with my code, but now I need to expand this to include several entries at the same time.

Here’s a revised version of my code:

public async Task SendMultipleRecords()
{
    var httpClient = new HttpClient();
    var request = new HttpRequestMessage(HttpMethod.Post, "https://api.airtable.com/v0/MY_BASE/MyTable");
    request.Headers.Add("Authorization", "Bearer MY_API_KEY");

    var data = "{\"records\": [{\"fields\": {\"Name\": \"Test Record\"}}]}";
    request.Content = new StringContent(data);
    request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

    await httpClient.SendAsync(request);
}

I’ve also set up a model to represent my data, but I’m unsure how to initialize it from a DataGridView or a list. Any help figuring out how to structure the object would be really appreciated. Thanks!

hey there flyingeagle! to send multiple records, you gotta modify your JSON data structure. instead of just one record, add more objects to the “records” array. something like:

{"records": [{"fields": {"Name": "Test1"}}, {"fields": {"Name": "Test2"}}]}

this way you can send as many records as you want in one go. hope that helps!

To send multiple records, you’ll need to modify your data structure and serialization approach. Instead of manually constructing the JSON string, use a library like Newtonsoft.Json to serialize your data objects. Here’s how you can restructure your code:

Create a class to represent your record:

public class AirtableRecord
{
public Dictionary<string, object> fields { get; set; }
}

Then, create a list of these records and serialize it:

var records = new List
{
new AirtableRecord { fields = new Dictionary<string, object> { { “Name”, “Test1” } } },
new AirtableRecord { fields = new Dictionary<string, object> { { “Name”, “Test2” } } }
};

var data = JsonConvert.SerializeObject(new { records });

This approach allows you to easily add multiple records and handle complex data structures. Remember to add the Newtonsoft.Json NuGet package to your project.