Skip to main content

Goal: importing JSON from an open API into my airtable base.

How: Using a script extension in the base (at the bottom of this message), with proper error logging.

Result: No return after “let response;” The script just seems to stop without any error triggering. The last log is “console.log("⏳ Start fetching domeinen data...")”

Test that does work: A curl request to the API does yield the complete JSON, just as a request through postman. So the API is reachable.

The code to request and import the data:

 

 

// API credentials

let apiKey = "(API key censored, but it's working)";

let apiUrl = "https://opendata.slo.nl/curriculum/api/v1/domein/";



// Function to fetch the domeinen data

async function fetchDomeinen() {

console.log("⏳ Start fetching domeinen data...");



let response;

try {

response = await fetch(apiUrl, {

method: "GET",

headers: {

"Accept": "application/json",

"Authorization": "Basic " + btoa("key:" + apiKey) // Basic Auth with API key

}

});

console.log("Response received:", response); // Log the full response object for debugging



if (!response.ok) {

console.error(`❌ API request failed with status: ${response.status}, ${response.statusText}`);

return null;

}



console.log(`✅ API request completed. Status code: ${response.status}`);

} catch (error) {

console.error("❌ Error during fetch:", error);

return null;

}



// Check if response contains data

try {

let data = await response.json();

if (!data || data.length === 0) {

console.error("⚠️ No data found in API response.");

return null;

}

console.log("✅ Successfully fetched domeinen data:", data);

return data; // Return the domeinen data

} catch (error) {

console.error("❌ Error parsing JSON:", error);

return null;

}

}



// Function to update Airtable with domeinen data

async function updateTableWithDomeinen() {

console.log("⏳ Fetching domeinen...");



// Fetch domeinen data

let domeinen = await fetchDomeinen();



if (!domeinen) {

console.log("❌ No domeinen data found. Exiting...");

return;

}



// Check if the table "Domeinen" exists

let domeinenTable;

try {

domeinenTable = base.getTable("Domeinen");

console.log("✅ Domeinen table found.");

} catch (error) {

console.error("❌ Error: Domeinen table not found in Airtable. Please check table name.", error);

return;

}



console.log("⏳ Starting to insert domeinen data into Airtable...");



// Loop through each domein and create a record in Airtable

for (let domein of domeinen) {

// Check if the essential fields are present in the domein object

if (!domein.uuid || !domein.titel) {

console.error(`❌ Domein missing required fields (UUID or Title). Skipping domein:`, domein);

continue;

}



console.log(`⏳ Inserting domein: UUID = ${domein.uuid}, Title = ${domein.titel}`);



try {

await domeinenTable.createRecordAsync({

"UUID": domein.uuid || "No UUID",

"Naam": domein.titel || "No Title",

"Omschrijving": domein.omschrijving || "No Description",

"Type domein": domein.type || "No Type", // Adjust according to API response

"Release datum": domein.releaseDate ? new Date(domein.releaseDate) : null, // Optional

"URL": `https://opendata.slo.nl/curriculum/uuid/${domein.uuid}`

});

console.log(`✅ Successfully inserted domein: ${domein.uuid}`);

} catch (error) {

console.error(`❌ Error inserting domein: ${domein.uuid}`, error);

}

}



console.log("✅ Finished inserting all domeinen.");

}



// Run the script to update Airtable

updateTableWithDomeinen();

 

 

 

Hi,
try to run it with last line 

await updateTableWithDomeinen();





Thanks! This works and the script continues and logs an actual error. Now I can find the next issue, which might be unrelated: 

console.log
"⏳ Fetching domeinen..."
console.log
"⏳ Start fetching domeinen data..."
console.log
"Response received:"
{}
console.error
"❌ API request failed with status: 401, Unauthorized"
console.log
"❌ No domeinen data found. Exiting..."


Reply