DEV Community

Fabian Anguiano
Fabian Anguiano

Posted on

A Guide to Pagination in the 3dcart API: Fetching Sales Data

Introduction

Working with APIs often involves retrieving large amounts of data. To efficiently handle all of this data, pagination is a crucial technique. This article aims to guide you through the process of implementing pagination with the 3dcart API, using a Python script as an example. By the end, you'll have a clear understanding of how to fetch sales data from the 3dcart API using pagination.

Prerequisites

Before diving into pagination, ensure you have the following prerequisites:

  1. Basic knowledge of Python programming.
  2. Familiarity with making API requests using Python.

Setting Up the Script

To begin, let's set up a script that fetches sales data from the 3dcart API. The script provided below serves as a starting point:

# Import the necessary libraries
import requests
import json
import time

def fetch_sales_data_from_api(sales_order_names):
    # Load headers from the config file
    with open('config.json') as config_file:
        config = json.load(config_file)

    # Set up the base URL and headers
    base_url = "https://apirest.3dcart.com/3dCartWebAPI/v1/Orders"
    headers = {
        'SecureURL': config['SecureURL'],
        'PrivateKey': config['PrivateKey'],
        'Token': config['Token']
    }

    # Set pagination parameters
    limit = 100  # Number of results per page
    offset = 0   # Initial offset value
    max_retries = 3

    all_order_data = []

    # Iterate over the order statuses
    for order_status in sales_order_names:
        # Remove the "WH-" prefix from the order status
        invoice_number = order_status.replace("WH-", "")

        # Reset the offset for each order status
        offset = 0

        # Construct the initial URL
        url = f"{base_url}?invoicenumber={invoice_number}&limit={limit}&offset={offset}"

        # Retry logic
        success = False
        retries = 0

        while not success and retries < max_retries:
            response = requests.request("GET", url, headers=headers)

            if response.status_code == 200:
                success = True
                print(f"Successfully fetched data from {url}")
                orders = response.json()
                if orders:
                    all_order_data.extend(orders)
                break

            else:
                print("Retrying...")
                retries += 1
                time.sleep(10)

        if not success:
            print("Failed to fetch data after multiple retries.")

    return all_order_data
Enter fullscreen mode Exit fullscreen mode

What is Pagination ?

Pagination is a technique used to retrieve large amounts of data in manageable chunks or pages. The 3dcart API supports pagination by allowing you to specify the limit and offset parameters in your API requests.

  • The limit parameter determines the number of results per page.
  • The offset parameter indicates the starting position of the data to be retrieved.

Basically think of pagination as breaking up large chunks of data to sort data faster.

Implementing Pagination

In the provided script, pagination is implemented in the fetch_sales_data_from_api function. Here's a breakdown of how it works:

  1. The script loads the necessary headers from a configuration file (config.json).
  2. It sets up the base URL and constructs the headers for the API request.
  3. Pagination parameters are defined:
    • limit: The number of results to retrieve per page. In the example, it's set to 100.
    • offset: The initial starting position. It's reset for each order status in the loop.
  4. The script initializes an empty list, all_order_data, to store the fetched order data.
  5. For each order status, the script constructs the URL by combining the base URL, the invoice number (with the "WH-" prefix removed), the limit, and the offset.
  6. A retry loop is implemented to handle potential failures during the API request. It tries up to max_retries times, with a 10-second delay between retries.
  7. Upon a successful response (status code 200), the script extracts the orders data from the response and appends it to the all_order_data list.
  8. If the maximum number of retries is reached without success, an appropriate message is printed.
  9. Finally, the function returns the complete all_order_data list.

Conclusion

In this article, we explored how to implement pagination when working with the 3dcart API. By using the provided script as a starting point, you can fetch sales data from the API while handling pagination seamlessly. Remember to adapt the script and configuration file to suit your specific requirements.

Pagination is a powerful technique that allows you to work with large datasets more efficiently. With this knowledge, you're now equipped to fetch data from the 3dcart API using pagination effectively.

Here is a gist of the code
https://gist.github.com/tunahorse/01d74fd67af06b625ce5c59481f906d9

Follow me on twitter

Top comments (0)