Troubleshooting 422 error when posting to Airtable API

I’m having trouble with my Airtable API integration. When I try to POST a new record, I keep getting a 422 error. Here’s what I’ve done so far:

I set up a method to insert a record:

async Task AddNewEntry(Item data)
{
    string endpoint = "https://api.airtable.com/v0/myappid/Items";
    string apiKey = "myapikey";

    using (var httpClient = new HttpClient())
    {
        httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");

        try
        {
            var jsonData = JsonConvert.SerializeObject(data);
            var content = new StringContent(jsonData);
            var response = await httpClient.PostAsync(endpoint, content);

            if (response.IsSuccessStatusCode)
                Console.WriteLine("Entry added successfully");
            else
                Console.WriteLine($"Error: {response.StatusCode}");
        }
        catch (Exception e)
        {
            Console.WriteLine($"Exception: {e.Message}");
        }
    }
}

My Item class looks like this:

class Item
{
    public string Name { get; set; }
    public string ImageUrl { get; set; }
}

When I test with Postman, it works fine. But my C# code always returns a 422 error. What am I missing? Any help would be appreciated!

I’ve encountered this issue before. The problem likely lies in how you’re structuring your JSON payload. Airtable requires a specific format for POST requests.

Try modifying your code to wrap the data in a ‘fields’ object:

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

Also, ensure you’re setting the correct Content-Type header:

httpClient.DefaultRequestHeaders.Add(“Content-Type”, “application/json”);

These changes should resolve the 422 error. If you’re still having issues, double-check that your field names in the C# class exactly match those in your Airtable base, including capitalization. Airtable is quite particular about these details.

Let us know if this solves your problem or if you need further assistance.

bro, u gotta wrap ur data in a ‘fields’ object. airtable’s picky bout that stuff. try this:

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

also, check if ur field names match EXACTLY with airtable. thats probs why postman works but ur code don’t. good luck man!