My Account
Actions & Action Templates
How to Change Your Commerce Type
Inviting New Users
Managing Notifications
Partnership Billing Settings
Passwordless Sign-in
Privacy Policy
Setting Billing Address
Signing Up
Single Sign-On (SSO)
Switching Between Accounts
Terms of Service
Viewing Subscription Info
Buyers
Automating Price List Creation via Actions
Autonomous Merchandising
Buyer Product Validation Guide
Check Order Status
Downloading Assets
How to Delay an Invoice
Inviting Sellers
Packing Slips - Buyers
Selecting Products
Seller SLAs
Shopify Product Syncing
Signing Up As A Buyer
Step by Step for Buyer Onboarding
Syncing Order Updates
Wholesale Guide for Buyers
Sellers
Onboarding
Adding Partners
Connecting Your Platform
Exclusive Collaborations - Syncing Unpublished Products
Fixing SKUs in Scientific Notation
How to Set Up Pricing
Marking Products as Active or Inactive
Seller Quickstart Guide
The 4Ps of Seller Onboarding
Resources
Adding Barcode Values in WooCommerce
Creating Product SKUs
Downloadable Image Links
Handling Returns
How Invoicing Works
Importing Products
Inviting Customers
Manual Fulfillment
Packing Slips - Sellers
Taxes and Manual Invoicing
Wholesale Guide for Sellers
Editing Price Lists
Sending Test Orders
Step by Step for Seller Onboarding
Integrations
WooCommerce
Shopify
Connecting Shopify
Updating Permissions for the Convictional App on Shopify
Updating Shopify Store URL
EDI
Connecting through Seller EDI - Invoice (810) Specification
Connecting through Seller EDI for Dropship
Connecting through Seller EDI for Dropship - Advance Ship Notice (856) Specification
Connecting through Seller EDI for Dropship - Inventory Update (846) Specification
Connecting through Seller EDI for Dropship - Purchase Order (850) Specification
Connecting through Seller EDI for Dropship - Purchase Order Acknowledgements (855) Specification
Connecting through Seller EDI for Wholesale - Advance Ship Notice (856) Specification
Connecting through Seller EDI for Wholesale - Purchase Order (850) Specification
Webhooks
Building API Integrations
Connecting BigCommerce
Connecting Easypost
Connecting Magento 2 - Buyer
Connecting Magento 2 - Seller
Connecting through SFTP
Getting Started with the Buyer API
How Inventory Sync Works
Integrating your CRM
Supported Connection Methods
Updating Shipping Information
Security
Tutorials
Downloading EDI Files related to Purchase Orders
How To Update All Margins In A Price List
How can I confirm that Convictional can pull my Products from my WooCommerce Store?
How to Create a Test Order as an EDI Seller
How to Create an Order for the Buyer API
How to Determine the Remaining onboarding steps for a Partner
How to Resend an Advance Ship Notice
How to Resolve " Your SFTP user requires delete permission on your SFTP server."
How to Resolve "Already Processed Files"
How to Resolve "Unable to access your SFTP server"
How to Resolve "Your SFTP user requires read permission on your SFTP server"
How to Resolve "Your SFTP user requires write permission on your SFTP server."
How to Total Number of some kind of Resource using the API
How to check if you are a buyer or seller on Convictional?
Migrating Ecommerce Platforms
Troubleshooting
- All Categories
- Tutorials
- How to Total Number of some kind of Resource using the API
How to Total Number of some kind of Resource using the API
Updated
by Keith Weaver
In this tutorial, we will count the total number of resource (such as orders, products, prices, etc.). We will do this using the API. depending on the resource and your commerce type (buyer vs seller), you will need to find the appropriate endpoint.
We will write a basic loop that iterates through each page of results till the number of results is less than the limit passed in. Below is sample Python code for counting the total numbers of products.
import time
import requests # Uses the request library
import sys
def get_results(url str) -> int:
api_key = os.getenv('CONVICTIONAL_API_KEY') # Follow this guide for your API Key: https://developers.convictional.com/reference/introduction#get-your-api-key
headers = {
'Authorization': api_key,
'Content-Type': 'application/json',
}
r = requests.get(url, headers=headers)
if r.status_code != 200: # All non 200 will exit the total program
print ('Request failed', r.status_code, r.text)
sys.exit(1)
return len(r.json()['data']) # The response can be found from the Convictional API docs. https://developers.convictional.com/reference/
def main():
base_url = 'https://api.convictional.com/products'
last_page_found = False
total = 0 # Keep a running total
page = 0 # Convictional starts paging at 0
limit = 10 # Include a maximum of 10 results per request
while not last_page_found:
url = base_url + '?limit=' + str(limit) + '&page=' + str(page)
number_of_results = get_results(url)
total += limit # Must have return 10
if number_of_results < limit:
# End of results reached
last_page_found = True
break
page += 1 # Move onto next page
time.sleep(0.25) # Pause for 0.25 seconds to avoid hitting the rate limiter. You can view more details around rate limiting here: https://developers.convictional.com/docs/rate-limiting
print ('Total number of products is: ' + str(total))
main()