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)
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.