I’m having trouble with my Python code that connects to the Airtable API. When I try to retrieve a specific field using an identifier, it returns a ‘not found’ error. However, when I fetch all fields simultaneously, it works perfectly fine.
Here’s the code I’ve written:
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/appXYZ123/database/{record_id}"
auth_headers = {
"Authorization": "Bearer YOUR_TOKEN_HERE"
}
api_response = requests.get(api_endpoint, headers=auth_headers)
json_data = api_response.json()
target_field_value = json_data.get('fields', {}).get('record_id', 'Not found')
print("Record ID:", record_id)
print("API Response:", json_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 change the code to retrieve all fields like this, it works:
api_response = requests.get(api_endpoint, headers=auth_headers)
json_data = api_response.json()
# Get all available fields
all_fields = json_data.get('fields', {})
I’ve attempted modifying the field format in Airtable and adjusting the code multiple times, but I still can’t determine what the issue is. Can anyone help me figure out what’s going wrong?