Hey there! In this guide, we'll explore how to work with external APIs that have pagination or limits and offsets using Xano. Have you ever encountered an API that returns data in chunks, and you need to continuously call it to retrieve all the data? Well, you're in luck because Xano makes this task a breeze!
To illustrate this process, we'll be using the PokeAPI, a handy API that provides information about different Pokemon. If you visit PokeAPI.co, you'll see a "Try it out" section where you can fetch a list of Pokemon.
The API has parameters like limit and offset that control how many Pokemon are returned and where to start in the list, respectively. For example, setting limit=100 and offset=200 will return 100 Pokemon starting from the 201st entry.
First, let's copy the API URL into Xano and run it. You'll notice that the response includes various details like the request headers, response headers, and the actual Pokemon data.
To work with the data more easily, create a new variable using the "Sub Path" feature. This allows you to drill down and select the specific data you want to work with.
results using the "Sub Path" feature.result.results) and select it.Next, we'll create variables to control the pagination process. Since we want to fetch the last 118 Pokemon, we'll set the initial offset to 1000 and the limit to 2 (for demonstration purposes).
offset and set its value to 1000.limit and set its value to 2.Now, we'll update the API call to include the limit and offset parameters using the variables we just created.
limit with the value set to the limit variable.offset with the value set to the offset variable.To gather all the Pokemon data from the paginated API, we'll use a while loop. This loop will continue iterating until we've retrieved all the data.
while loop with the condition set to true.results.next is equal to null. If it is, break out of the loop using break.offset variable by adding the current limit value to it.listOfNames and initialize it as an empty array.listOfNames variable by merging it with the results.results.name values from the API response.Now you're ready to run the script and see the accumulated list of Pokemon names! If you want to fetch a different range of Pokemon, adjust the initial offset value and the condition in the conditional statement.
For example, to fetch the first 25 Pokemon, set the initial offset to 0, and change the conditional statement to break the loop when offset is greater than or equal to 25.
And there you have it! You've successfully learned how to work with paginated APIs in Xano. This technique can be applied to various APIs, allowing you to gather data efficiently and seamlessly.
Feel free to experiment and adapt the code to suit your specific needs. Happy coding!
Join 100,000+ people already building with Xano.
Start today and scale to millions.
Start building for free