Data Management

How to Store the Result of External API Calls In the Database

Summary

In this guide, we'll walk you through the process of storing data retrieved from an external API into your Xano database. We'll cover two scenarios: storing data from a single API call and storing data from multiple API calls using pagination.

Storing Data from a Single API Call

  1. Set up the External API Call: First, you need to set up your external API call. Go to the "APIs and Lambdas" section, and under "External API Requests," add a new request. Populate the URL, parameters, and headers inputs with the information provided by the API documentation.
  2. Make the API Call: Run the API call to see the response structure. The data you want to store is often nested within the response object.
  3. Update the Variable: Use the "Update Variable" function under "Data Manipulation" to extract the desired data from the API response. Assign the extracted data to a new variable.
  4. Loop Through the Data: Set up a "Forge Loop" to iterate through the data you want to store. Within the loop, you can perform actions like uploading images to Xano's file storage and adding records to your database.
  5. Upload Images (if applicable): If you need to store images, use the "Create Image Metadata" function under "File Storage" to upload the images and create metadata for them.
  6. Add Records to the Database: Use the "Add Records" function under "Database Requests" to add the data to your Xano database table. Map the appropriate fields from your data to the corresponding columns in your table.

Storing Data from Multiple API Calls (Pagination)

Some APIs return data in batches or use pagination, requiring multiple API calls to retrieve all the data. Here's how you can handle that in Xano:

  1. Set up the Initial API Call: Follow the same steps as before to set up the initial API call. This call will typically return the first batch of data along with a cursor or pagination information.
  2. Create Variables for Cursor and Remaining Data: Create two variables: one to store the cursor value (initially set to zero) and another to track the remaining data (initially set to a non-zero value, like 1).
  3. Set up a While Loop: Create a "While Loop" that will run as long as the "remaining" variable is greater than zero. This loop will execute multiple API calls until all data is retrieved.
  4. Update the API Call Parameters: Within the loop, update the API call parameters (e.g., cursor) dynamically using the values from the previous API response.
  5. Store Data in the Database: Use the "Add Bulk Records" function under "Database Requests" to store the retrieved data in your database table.
  6. Update Cursor and Remaining Variables: After each iteration, update the cursor and remaining variables based on the API response to prepare for the next iteration.
  7. Introduce a Delay (optional): If needed, you can add a delay between iterations using the "Sleep" function to avoid overwhelming the server.
  8. Schedule the Task (optional): For large datasets, you can schedule the entire process as a task to run at specific intervals or times.

By following these steps, you can efficiently store data from external APIs into your Xano database, whether it's a single API call or multiple calls with pagination. Remember to adapt the process based on the specific API's response structure and requirements.

This transcript was AI generated to allow users to quickly answer technical questions about Xano.

Was this helpful?

I found it helpful

I need more support
Sign up for XanoSign up for Xano

Build without limits on a secure, scalable backend.

Unblock your team's progress and create a backend that will scale for free.

Start building for free