Reputation: 1
I want to fetch Shopify orders data in any given range but not able to do so
The code is written below:
I have used postman to call the get requests
import requests
import pandas as pd
url = "https://{apikey}:{passcode_with_token}@{store_name}/admin/api/2022-10/orders.json?status=any"
payload={}
headers = {
'Authorization': 'Bearer token_value'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
Upvotes: 0
Views: 1146
Reputation: 1
Definitely have a browse on the Github repo: https://github.com/Shopify/shopify_python_api
Also, something useful I found when playing around with it myself is to be ultra specific in your params. Example code below:
##### Parameters #####
params = {
##### Having a specific timeframe seems to return more accurate results #####
"created_at_max": "2025-01-01T00:00:00+00:00",
"created_at_min": "2024-12-01T00:00:00+00:00",
##### Max limit is 250. Any more and an error will be returned #####
"limit": 250,
##### Statuses. Must list all statuses for all orders to be returned #####
"status": "any",
"financial_status": "paid, partially_paid, partially_refunded, refunded",
"fulfillment_status": "fulfilled, partial, restocked, unfulfilled",
##### Fields to return. Prevents unescessary clutter #####
"fields": "tags, email, created_at, discount_codes, total_price",
}
This is the function I made to return the order results:
def get_orders(shop_endpoint, params):
response = requests.get(shop_endpoint, params=params)
data = response.json()["orders"]
return data
data = get_orders(shop_endpoint, params)
Hopefully this helps!
Upvotes: 0
Reputation: 1965
I suggest you to use this library https://github.com/Shopify/shopify_python_api
Furthermore, to retrieve orders between a certain period I suggest you to use graphql instead of the REST api.
To give you an example
{
orders(query:"created_at:>='2022-01-01T00:00:00BST' AND created_at:<='2022-12-31T23:59:59BST')", sortKey: CREATED_AT, reverse: true, first: 20) {
pageInfo {
hasNextPage
}
edges {
cursor
node {
createdAt
name
lineItems(first: 20) {
pageInfo {
hasNextPage
}
edges {
cursor
node {
variant {
title
inventoryQuantity
}
sku
title
quantity
}
}
}
}
}
}
}
shopify.Session.setup(api_key=client_id, secret=client_secret)
if password: # depending if you have a password (private app) or access token (custom/public app)
shopify_session = shopify.Session(store, api_version, password)
else:
shopify_session = shopify.Session(store, version=api_version, token=access_token)
shopify.ShopifyResource.activate_session(shopify_session)
def retrieve_orders():
with open('query.graphql', 'r') as fp:
query = fp.read()
result = json.loads(shopify.GraphQL().execute(query))
# ...
return resutl['edges']
Keep in mind that you have to handle pagination in the result (that would be the same with the REST api) and to retrieve orders older than 120 days (I can be wrong but is something similar) you need a specific permission.
Upvotes: 0