Getting HTTP 422 error when creating records via Airtable REST API

I’m having trouble with my C# code that tries to add a new entry to Airtable through their API. Every time I run it, I get a 422 status code back.

public async Task CreateEntry(CustomerData data)
{
    var endpoint = "https://api.airtable.com/v0/appyyy/Users";
    var apiKey = "keyzzzz";
    using (var httpClient = new HttpClient())
    {
        httpClient.BaseAddress = new Uri(endpoint);
        httpClient.DefaultRequestHeaders.Clear();
        httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + apiKey);
        try
        {
            string jsonData = JsonConvert.SerializeObject(data);
            StringContent requestContent = new StringContent(jsonData);
            HttpResponseMessage result = null;
            result = await httpClient.PostAsync(endpoint, requestContent);
            if (result.IsSuccessStatusCode) Debug.WriteLine("Data saved successfully");
        }
        catch (Exception error)
        {
            Debug.WriteLine("Error occurred: {0}", error.Message);
        }
    }
}

My data class looks like this:

public class CustomerData
{
    public string UserName { get; set; }
    public string Avatar { get; set; }
}

When I test the same request in Postman, it works fine and returns:

{
  "records": [
    {
      "id": "recABC123xyz",
      "createdTime": "2023-03-03T14:22:08.000Z",
      "fields": {
        "UserId": 25,
        "Avatar": "https://example.com/uploads/avatar.png",
        "UserName": "TestUser"
      }
    }
  ]
}

What could be causing this 422 error in my code? The Postman request works but my C# implementation doesn’t. Any ideas what I’m missing here?

Your C# code is sending raw CustomerData but Airtable needs it wrapped properly. You’re missing the records structure and the fields nesting.

I hit this exact same issue 2 years ago when building an integration for our customer management system. The JSON structure has to match exactly what Airtable expects.

Try this instead:

var payload = new
{
    records = new[]
    {
        new
        {
            fields = new
            {
                UserName = data.UserName,
                Avatar = data.Avatar
            }
        }
    }
};

string jsonData = JsonConvert.SerializeObject(payload);
StringContent requestContent = new StringContent(jsonData, Encoding.UTF8, "application/json");

Also check your field names in Airtable. In your Postman response I see “UserId” but your C# class doesn’t have that field. Make sure the field names match exactly what you have in your Airtable base.

If you’re still getting 422 errors after this, check out this video that covers the most common causes:

One more thing - always log the response body when you get errors. It usually tells you exactly what’s wrong:

if (!result.IsSuccessStatusCode)
{
    var errorContent = await result.Content.ReadAsStringAsync();
    Debug.WriteLine($"Error: {result.StatusCode} - {errorContent}");
}

The 422 error likely stems from the way you’re structuring your JSON payload. Airtable expects the data to be wrapped in a “records” array and nested under “fields”. So you need to adjust your serialization to match this requirement. Additionally, ensure that you set the Content-Type header of your request to “application/json” with: requestContent.Headers.ContentType = new MediaTypeHeaderValue(“application/json”);. I experienced a similar issue previously and found that it was essential to adhere closely to the expected JSON format.