I need help with reading CSV data and pushing updates to Airtable records through Python.
My database structure looks like this (from API documentation):
# Sample structure for update request
update_payload = {
"records": [
{
"id": "rec123ABC",
"fields": {
"Name": "John",
"LastName": "Doe",
"Active": "Yes",
"ID_Number": "B222222",
"Phone": "0123456789",
"Monthly_Pay": 15000,
"Working_Days": 22,
"Start_Date": "2022-01-15",
"Advance_Rate": 0.25,
"Contact_Email": "[email protected]",
"Max_Advance": 3750,
"Monthly_Advances": 2,
"Free_Advances_Monthly": 1,
"Request_Fee": 8
}
}
]
}
I want to update only the Working_Days field by matching the ID_Number value.
My CSV contains:
ID_Number,Working_Days
Here’s what I’ve done:
- Load CSV content:
with open(csv_file, "r") as file:
reader = csv.DictReader(file)
for index, row in enumerate(reader):
csv_data[index] = row
- Convert to JSON format:
processed_data = json.loads(json.dumps(csv_data, indent=2))
- Need help here - how do I structure this data for the API call?
I know the final structure should be:
api_data = {
"records": [
{
"fields": {
"ID_Number": "value",
"Working_Days": value
}
}
]
}
I’m thinking about either creating an empty template and filling it, or building a list first then adding it to the main object.
Finally, I’ll send the PATCH request:
base_url = f"https://api.airtable.com/v0/{os.environ['app_id']}"
request_headers = {
"Authorization": f"Bearer {os.environ['api_token']}",
"Content-Type": "application/json"
}
def update_table(table_name, payload):
api_url = f"{base_url}/{table_name}"
response = requests.patch(url=api_url, json=payload, headers=request_headers)
return response.json()
What’s the best way to transform my CSV data into the proper structure for the API?