How do I update the Airtable API URL offset via a button click in JavaScript?

I am using JavaScript to request Airtable data and need to update the offset parameter when a next-page button is clicked. How can I refresh the API URL dynamically?

let resourceType = "Discounts";
let authToken = "?token=abcd";
let recordLimit = "&limit=10";
let paginationValue = "";
let apiEndpoint = "https://api.airtable.com/v0/app7890/" + resourceType + authToken + recordLimit + "&page=" + paginationValue;

async function fetchRecords() {
  const response = await fetch(apiEndpoint);
  const result = await response.json();
  // Process the result data here
}

function handleNextPage() {
  paginationValue = "newPageOffset"; // update to new offset
  fetchRecords();
}

document.getElementById('nextBtn').onclick = handleNextPage;

i solved it by rebuilding the full url inside the click function, so each time the offset gets updated, the endpoint changes too. give it a try and see if it works for you!

In my experience, using the URL and URLSearchParams objects can simplify dynamic updates to the API endpoint. Instead of manually concatenating strings, consider initializing a URL instance with the base URL and then using its searchParams.set method to update the offset value. This approach minimizes the risk of mistakes when dealing with multiple query parameters and improves code maintainability. I have encountered similar challenges before and find this method more robust when developing pagination features, especially in projects with evolving parameter requirements.