Airtable Pagination

Hey

I am using Postman to make an API call from Airtable to list all 14,000 of my records to store in a variable and pass onto voiceflow.

This is my pre-request script:

function getAllRecords() {
    const url = 'https://api.airtable.com/v0/baseID/tableID';
    let records = [];

    // First request
    requestAirtable(url, null, function (initial_response) {
        records.push(...initial_response.records);
        let offset = initial_response.offset;
        // Handle pagination recursively
        function fetchNextPage(offset) {
            if (!offset) {
                console.log(`✅ Data Fetch Complete! Total records: ${records.length}`);
                pm.environment.set("voiceflow_data", JSON.stringify({ fields: records }));
                return;
            }
            requestAirtable(url, offset, function (response) {
                records.push(...response.records);
                fetchNextPage(response.offset);
            });
        }
        fetchNextPage(offset);
    });
}

function requestAirtable(url, offset, callback) {
    const api_key = pm.environment.get("AIRTABLE_API_KEY"); // Securely store API key
    if (offset) {
        url += `?offset=${offset}`;
    }
    const options = {
        url: url,
        method: 'GET',
        header: { // Fix: Correct 'header' to 'headers'
            'Authorization': `Bearer ${api_key}`, // Fix: Ensure key is inside string
            'Content-Type': 'application/json'
        }
    };

    pm.sendRequest(options, function (err, res) {
        if (err) {
            console.log("Airtable Request Error:", err);
            return;
        }
        callback(res.json());
    });
}

// Run the function
getAllRecords();

I used ChatGPT for this as I am not a developer, but it’s not working. Any help would be greatly appreciated.

Thanks