Getting 422 error when posting data to Airtable API

I’m having trouble with a 422 status code when trying to add new records to my Airtable base through their REST API. My C# code seems correct but something is going wrong.

public async Task AddNewEntry(CustomerData entry)
{
    var endpoint = "https://api.airtable.com/v0/appyyy/CustomerTable";
    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(entry);
            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(@"\tERROR {0}", error.Message);
        }
    }
}

My data class looks like this:

public class CustomerData
{
    public string CustomerName { get; set; }
    public string PhotoUrl { get; set; }
}

When I test with Postman, everything works fine and I get a proper response:

{
"records": [
    {
        "id": "recAbc123Def456",
        "createdTime": "2023-03-03T14:22:08.000Z",
        "fields": {
            "CustomerId": 25,
            "PhotoUrl": "https://example.com/photos/customer1.jpg",
            "CustomerName": "John Smith"
        }
    }
]
}

But my C# application keeps returning a 422 error. What could be causing this issue? Any suggestions would be appreciated.

You’re missing the Content-Type header, but there’s another issue nobody mentioned - you’re setting BaseAddress and then passing the full URL to PostAsync, which screws up routing. I hit this exact problem with Airtable last month. The 422 error usually means bad JSON structure, but your HttpClient setup is also broken. Drop the BaseAddress line since you’re already giving it the full endpoint URL. Here’s what fixed it for me: csharp using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + apiKey); var requestBody = new { records = new[] { new { fields = entry } } }; string jsonData = JsonConvert.SerializeObject(requestBody); var content = new StringContent(jsonData, Encoding.UTF8, "application/json"); var result = await httpClient.PostAsync(endpoint, content); } Key differences: no BaseAddress, proper Content-Type header, and wrapping your data in the records array that Airtable wants. This killed my 422 errors instantly.

had this same problem last week! you’re forgetting the content-type header - that’s what’s messing up your request. use new StringContent(jsonData, Encoding.UTF8, "application/json") instead of just new StringContent(jsonData). also, wrap your data like others said, but the missing header is likely your main issue.

Check your Airtable field names against what you’re sending. I hit this same 422 error when my C# properties didn’t match the actual column names. Your Postman test works, but the field mapping might be off.

You’re not handling the response body on 422 errors. Add this to see what Airtable’s actually complaining about:

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

The error response tells you exactly which field’s broken. I had “Customer_Name” in Airtable but sent “CustomerName” in JSON. Small stuff like underscores or spaces triggers 422s even when everything else looks right.

Writing all this C# code for API calls is exactly why I stopped doing manual integrations years ago. You’re dealing with headers, JSON serialization, error handling, and data mapping - it’s a nightmare to maintain.

I used to write similar code for Airtable connections until I realized I was wasting hours on boilerplate that breaks every time something changes. Now I just set up these workflows in Latenode instead.

You can connect to Airtable with zero code. Just map your fields visually and it handles all the JSON structure nonsense automatically. No more debugging 422 errors or wrestling with HTTP clients.

I built a customer data sync that used to be 200+ lines of C# code. Replaced it with a 5 minute Latenode workflow that’s been running perfectly for months. When requirements change, I just drag and drop new fields instead of rewriting serialization logic.

Your current approach works but you’ll spend more time debugging API quirks than building actual features. Save yourself the headache.

Yeah, that’s definitely a data structure issue. But you’ve got another problem too.

You’re setting BaseAddress on HttpClient AND passing the full endpoint URL to PostAsync. That creates a double path mess. Pick one - either use BaseAddress with a relative path, or skip BaseAddress and just use the full URL.

Here’s how I handle Airtable API calls:

using (var httpClient = new HttpClient())
{
    httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + apiKey);
    
    var airtableRecord = new {
        records = new[] {
            new {
                fields = entry
            }
        }
    };
    
    string jsonData = JsonConvert.SerializeObject(airtableRecord);
    var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
    
    var result = await httpClient.PostAsync(endpoint, content);
}

See how I’m not bothering with BaseAddress? Just throwing the full endpoint URL straight into PostAsync.

One more thing - double-check that your CustomerData property names match exactly what Airtable wants. Case sensitivity will bite you here.

The Problem:

You’re receiving a 422 (Unprocessable Entity) error when attempting to add new records to your Airtable base using the C# REST API. Your code appears correct when tested with Postman, but the C# application fails. This indicates a discrepancy between the data structure your C# code sends and what the Airtable API expects.

:thinking: Understanding the “Why” (The Root Cause):

The Airtable API requires a specific JSON structure for creating new records. Your current C# code directly serializes the CustomerData object, which doesn’t conform to Airtable’s expected format. Airtable anticipates a JSON payload with a records array, where each element contains a fields object mapping your data to the corresponding Airtable columns. The mismatch in structure leads to the 422 error.

:gear: Step-by-Step Guide:

Step 1: Modify the JSON Payload Structure:

The core issue lies in how you structure the data sent to the Airtable API. You need to create a wrapper object that conforms to Airtable’s requirements. Here’s the corrected C# code:

public async Task AddNewEntry(CustomerData entry)
{
    var endpoint = "https://api.airtable.com/v0/appyyy/CustomerTable";
    var apiKey = "keyzzzz";
    using (var httpClient = new HttpClient())
    {
        httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + apiKey);
        httpClient.DefaultRequestHeaders.Add("Content-Type", "application/json"); // Add this header

        var payload = new
        {
            records = new[]
            {
                new
                {
                    fields = new
                    {
                        CustomerName = entry.CustomerName,
                        PhotoUrl = entry.PhotoUrl
                    }
                }
            }
        };

        string jsonData = JsonConvert.SerializeObject(payload);
        var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
        HttpResponseMessage result = await httpClient.PostAsync(endpoint, content);

        if (result.IsSuccessStatusCode)
        {
            Debug.WriteLine(@"\Data saved successfully.");
        }
        else
        {
            var errorContent = await result.Content.ReadAsStringAsync();
            Debug.WriteLine($"\tERROR {result.StatusCode}: {errorContent}"); //Improved error handling
        }
    }
}

Notice the key changes:

  • The Content-Type header is now explicitly set to "application/json". This informs Airtable of the data format.
  • The CustomerData object is now nested within a records array, with each record containing a fields object. This matches Airtable’s expected structure.
  • Improved error handling now includes the response body from Airtable for detailed diagnostics.

Step 2: Verify Airtable Field Names:

Double-check that the CustomerName and PhotoUrl properties in your CustomerData class exactly match the names of your Airtable columns. Case sensitivity and any extra spaces or underscores matter. Any discrepancy will result in a 422 error.

Step 3: Check for other potential issues:

If the problem persists, consider these points:

  • API Key Validation: Ensure your apiKey is correct and has the necessary permissions to write to the specified Airtable base and table.
  • Network Connectivity: Verify that your application can connect to api.airtable.com. A firewall or network issue might be preventing communication.

:mag: Common Pitfalls & What to Check Next:

  • Incorrect Field Names: Airtable is very case-sensitive. Verify your field names (e.g., CustomerName in your code vs. Customer Name in Airtable).
  • Data Type Mismatches: Ensure the data types of your C# properties (string, int, etc.) match the data types of your Airtable columns.
  • Rate Limiting: Airtable has API rate limits. If you’re making many requests, implement retry logic and handle rate limit responses appropriately. Look at the error message for clues.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.