Create shopify_example.py
Browse files- shopify_example.py +32 -0
shopify_example.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
# 1) Read from env
|
| 5 |
+
SHOP = os.getenv("SHOPIFY_STORE")
|
| 6 |
+
TOKEN = os.getenv("SHOPIFY_TOKEN")
|
| 7 |
+
if not SHOP or not TOKEN:
|
| 8 |
+
raise RuntimeError("Set SHOPIFY_STORE and SHOPIFY_TOKEN environment variables")
|
| 9 |
+
|
| 10 |
+
# 2) Prepare headers
|
| 11 |
+
headers = {
|
| 12 |
+
"Content-Type": "application/json",
|
| 13 |
+
"X-Shopify-Access-Token": TOKEN
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
# 3) Choose your API version & resource
|
| 17 |
+
API_VERSION = "2025-04"
|
| 18 |
+
resource = "products" # could also be "orders", "customers", etc.
|
| 19 |
+
|
| 20 |
+
# 4) Build URL
|
| 21 |
+
url = f"https://{SHOP}/admin/api/{API_VERSION}/{resource}.json"
|
| 22 |
+
|
| 23 |
+
# 5) Make the request
|
| 24 |
+
response = requests.get(url, headers=headers)
|
| 25 |
+
response.raise_for_status() # will raise if status != 200
|
| 26 |
+
|
| 27 |
+
# 6) Parse and print
|
| 28 |
+
data = response.json()
|
| 29 |
+
print(f"Fetched {len(data.get(resource, []))} {resource}:")
|
| 30 |
+
for item in data.get(resource, []):
|
| 31 |
+
# For products, print id and title
|
| 32 |
+
print(f" • {item['id']}: {item.get('title')}")
|