I’m having trouble with my Python Flask app that connects to Airtable. When I try to get a specific field from a record using the record ID, it returns “not found” error. But when I fetch all fields from the same record, everything works fine.
Here’s my current code:
from flask import Flask, render_template, request
import requests
app = Flask(__name__)
@app.route('/')
def home_page():
return render_template('index.html')
@app.route('/data')
def get_data():
record_id = request.args.get('record_id')
api_endpoint = f"https://api.airtable.com/v0/appYZ123ABC456DEF/database_table/{record_id}"
auth_headers = {
"Authorization": "Bearer YOUR_TOKEN_HERE"
}
api_response = requests.get(api_endpoint, headers=auth_headers)
response_data = api_response.json()
target_field_value = response_data.get('fields', {}).get('record_id', 'Not found')
print("Record ID:", record_id)
print("API Response:", response_data)
print("Status Code:", api_response.status_code)
return render_template('data.html', target_field=target_field_value)
if __name__ == '__main__':
app.run(debug=True)
When I modify the code to get all fields instead, it works perfectly:
api_response = requests.get(api_endpoint, headers=auth_headers)
response_data = api_response.json()
# Get everything
all_fields = response_data.get('fields', {})
I’ve tried changing the field type in Airtable and tweaking my code several times. What could be causing this issue? The record exists because I can see all the data when fetching everything at once.
ah i see the issue here! you’re mixing up the record ID with field names. the record_id
variable is just the airtable record identifier, but you’re trying to access it as a field name in your table. check what your actual field names are called in airtable - they’re probably something like ‘name’ or ‘value’ not ‘record_id’. just print out the fields object to see whats actualy there.
The problem is definitely in this line: target_field_value = response_data.get('fields', {}).get('record_id', 'Not found')
. You’re using the same variable name record_id
for two different things - the Airtable record identifier AND as a field name lookup. This creates confusion because Airtable’s record ID (like “recABC123”) is not stored as a field called “record_id” inside the fields object. Instead, it’s returned as a separate property called “id” at the root level of the response. Your actual field names in Airtable are completely different - they might be called “Name”, “Status”, “Description” or whatever you named them when creating your table. I had this exact same issue when I started working with Airtable API last year. Try printing response_data.get('fields').keys()
to see your actual field names, then replace ‘record_id’ with the correct field name you want to retrieve.
Looking at your code, I think there’s a naming confusion that’s causing the issue. You’re trying to access a field called ‘record_id’ from the response, but ‘record_id’ is actually the Airtable record identifier, not a field name within your table. The record ID is returned separately in the response structure as ‘id’, not within the ‘fields’ object. Your field names in Airtable are probably something different entirely. When you fetch all fields with response_data.get('fields', {})
, you’re getting the actual field data, but when you try response_data.get('fields', {}).get('record_id', 'Not found')
, you’re looking for a field literally named ‘record_id’ which probably doesn’t exist in your table schema. Check what your actual field names are in Airtable and use those instead. You can see them when you print the full response.