Airtable Object Mapper

Class Library
C#
GitHub Gist

What It Is

Code for retrieving records from Airtable and dynamically creating C# objects from them

Status

Last Reviewed:

I use this in a scheduled job that runs every day. It’s the most reliable part of that code.

I had to make a minor edit recently because Airtable changed how they did authentication, but otherwise it’s worked unchanged since 2017.

Details

This is a C# class that will retrieve a bunch of records from Airtable and convert them to POCOs. It reflects the class definition and applies Airtable data based on designated attributes.

(Note: it only retrieves all the records from a table. I don’t think it would be hard to change this.)

var airtable = new Airtable("myAccountId", "myApiKey");
var people = airtable.GetObjects<Person>("myTableName");

public class Person
{
  [AirtableId]
  public string Id { get; set; }

  [AirtableColumn("Full Name")] 
  public string Name { get; set; }
}

This will pull all the records from that table and turn them into POCOs. I use this a lot for batch processging stuf.

You can call it simply like that, but there are a lot of events to inject logic into the process:

This event will fire when the JSON comes back, so you can do something with that. I think I just used this to log the response for debugging.

airtable.OnHttpResponse += (sender, e) => {

  File.WriteAllText(@"C:\airtable.json", e.HttpResponse);

};

Just before the property assginment happens for each record/object, an event will present you with all the field names and values it intends to assign. You can modify them, or even skip this one entirely.

airtable.OnBeforeAssignValues += (sender, e) =>
{
  if(e.Values["FirstName"] == "Deane" && e.Values["LastName"] == "Barker")
  {
    e.Values["JobTitle"] = "Secret Agent";
  }

  if(e.Values["LastName"] == "Blofeld")
  {
    e.Cancel = true; // This record will be skipped
  }
};

The system does its best to convert values to the right types, but you can write your own convertors for specific columns:

var dateConvertor = new AirtableColumnConvertor()
{
  // This will convert anything from this Airtable column
  ColumnName = "DateOfBirth", 

  // This is the actual conversion function
  // Accepts a string, returns an object
  Function = (s) =>
  {
    if(DateTime.TryParse(s), out DateTime result)
    {
      return result;
    } else {
     return DateTime.MinValue;
    }
  };
}

airtable.AddConvertor(dateConvertor);

If a conversion fails, you have a chance to handle that:

airtable.OnTypeConversionFailure += (sender, e) =>
{
  if(e.FieldName == "Date") { e.FieldValue = DateTime.MinValue}
};

While designed for Airtable, it’s a handy model and reference for a lot of problems you’d find when auto-populating any data in C#. There are a lot of patterns in here that are applicable to anything (swap Airtable out for any other datasource…)

I sent this to Airtable, and they tweeted it from their account at one point. I use it on this site, to retrieve data for The Interesting List.