Dedicated to the Hydrogen framework, headless commerce, and building custom storefronts using the Storefront API.
Hello!
I'm looking for a way to change the prices of items in my storefront programmatically.
I'm sure there's an API for it, I'm just a bit overwhelmed with all of the lingo I'm seeing, "apps" and whatnot.
Also noticed that the API's are broken in to many subcategories (Admin, Partner, App Bridge, Remix, Payments, Shopify Function, and Multipass) so was wondering someone could guide me towards which might be best suited for changing item prices?
Solved! Go to the solution
This is an accepted solution.
Hey there Mgiara!
Let's break down the components involved in programmatically changing item prices on a Shopify storefront and understand the relevant APIs and technologies:
Shopify Storefront:
Admin API:
Storefront API:
Remix:
App Bridge:
Shopify Functions:
Multipass:
To programmatically change item prices on your Shopify storefront, you'll primarily want to use the Shopify Admin API. Here's a high-level overview of the steps involved:
Here is an example of how to authenticate to a Shopify API in Python / Flask with an Access Token in the header of the request. This is a GET and a PUT request, for your edification.
@Anonymous_bp.route('/')
def index():
return render_template('index.html')
@products_bp.route('/products', methods=['GET'])
def get_all_products():
# Define the URL for the REST API request
url = f'https://{SHOP_NAME}.myshopify.com/admin/api/{ADMIN_API_VERSION}/products.json'
# Headers for the REST API request
headers = {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': ADMIN_API_ACCESS_TOKEN
}
# Make the REST API request
response = requests.get(url, headers=headers)
# Check if the request was successful
if response.status_code == 200:
return jsonify(response.json())
else:
return jsonify({'error': 'Unable to fetch products'}), response.status_code
@products_bp.route('/products/<product_id>', methods=['GET'])
def get_product_by_id(product_id):
try:
# Define the URL for the REST API request
url = f'https://{SHOP_NAME}.myshopify.com/admin/api/{ADMIN_API_VERSION}/products/{product_id}.json'
# Headers for the REST API request
headers = {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': ADMIN_API_ACCESS_TOKEN
}
# Make the REST API request
response = requests.get(url, headers=headers)
# Check if the request was successful
if response.status_code == 200:
return jsonify(response.json())
elif response.status_code == 404:
return jsonify({'error': 'Product not found'}), 404
else:
return jsonify({'error': 'Unable to fetch product'}), response.status_code
except Exception as e:
return jsonify({'error': 'An error occurred', 'message': str(e)}), 500
@products_bp.route('/update_product/<product_id>', methods=['PUT'])
def update_product(product_id):
try:
# Extract the product details from the request body
product_details = request.json
# Define the URL for the REST API request
url = f"https://{SHOP_NAME}.myshopify.com/admin/api/{ADMIN_API_VERSION}/products/{product_id}.json"
# Headers for the REST API request
headers = {
"Content-Type": "application/json",
"X-Shopify-Access-Token": ADMIN_API_ACCESS_TOKEN
}
# Prepare the data payload with the new product details
payload = {
"product": product_details
}
# Send the request to Shopify via REST API
response = requests.put(url, json=payload, headers=headers)
# Log response for debugging
print(f"Response Status Code: {response.status_code}")
print(f"Response Body: {response.text}")
# Check if the request was successful
if response.status_code in [200, 201, 202]:
return jsonify({"success": True, "message": "Product updated successfully"})
else:
return jsonify({"success": False, "message": "Failed to update product", "error": response.text}), response.status_code
except Exception as e:
return jsonify({"success": False, "message": "An error occurred", "error": str(e)}), 500
This is an accepted solution.
Hey there Mgiara!
Let's break down the components involved in programmatically changing item prices on a Shopify storefront and understand the relevant APIs and technologies:
Shopify Storefront:
Admin API:
Storefront API:
Remix:
App Bridge:
Shopify Functions:
Multipass:
To programmatically change item prices on your Shopify storefront, you'll primarily want to use the Shopify Admin API. Here's a high-level overview of the steps involved:
Here is an example of how to authenticate to a Shopify API in Python / Flask with an Access Token in the header of the request. This is a GET and a PUT request, for your edification.
@Anonymous_bp.route('/')
def index():
return render_template('index.html')
@products_bp.route('/products', methods=['GET'])
def get_all_products():
# Define the URL for the REST API request
url = f'https://{SHOP_NAME}.myshopify.com/admin/api/{ADMIN_API_VERSION}/products.json'
# Headers for the REST API request
headers = {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': ADMIN_API_ACCESS_TOKEN
}
# Make the REST API request
response = requests.get(url, headers=headers)
# Check if the request was successful
if response.status_code == 200:
return jsonify(response.json())
else:
return jsonify({'error': 'Unable to fetch products'}), response.status_code
@products_bp.route('/products/<product_id>', methods=['GET'])
def get_product_by_id(product_id):
try:
# Define the URL for the REST API request
url = f'https://{SHOP_NAME}.myshopify.com/admin/api/{ADMIN_API_VERSION}/products/{product_id}.json'
# Headers for the REST API request
headers = {
'Content-Type': 'application/json',
'X-Shopify-Access-Token': ADMIN_API_ACCESS_TOKEN
}
# Make the REST API request
response = requests.get(url, headers=headers)
# Check if the request was successful
if response.status_code == 200:
return jsonify(response.json())
elif response.status_code == 404:
return jsonify({'error': 'Product not found'}), 404
else:
return jsonify({'error': 'Unable to fetch product'}), response.status_code
except Exception as e:
return jsonify({'error': 'An error occurred', 'message': str(e)}), 500
@products_bp.route('/update_product/<product_id>', methods=['PUT'])
def update_product(product_id):
try:
# Extract the product details from the request body
product_details = request.json
# Define the URL for the REST API request
url = f"https://{SHOP_NAME}.myshopify.com/admin/api/{ADMIN_API_VERSION}/products/{product_id}.json"
# Headers for the REST API request
headers = {
"Content-Type": "application/json",
"X-Shopify-Access-Token": ADMIN_API_ACCESS_TOKEN
}
# Prepare the data payload with the new product details
payload = {
"product": product_details
}
# Send the request to Shopify via REST API
response = requests.put(url, json=payload, headers=headers)
# Log response for debugging
print(f"Response Status Code: {response.status_code}")
print(f"Response Body: {response.text}")
# Check if the request was successful
if response.status_code in [200, 201, 202]:
return jsonify({"success": True, "message": "Product updated successfully"})
else:
return jsonify({"success": False, "message": "Failed to update product", "error": response.text}), response.status_code
except Exception as e:
return jsonify({"success": False, "message": "An error occurred", "error": str(e)}), 500
This has to be the best reply I've ever received from a question on any kind of forum.
Can't thank you enough James.
Hi - Can you elaborate on REST API's and GRAPHQL API's and where does these stand between ADMIN API and STOREFRONT API?
That is an amazing reply. Shows your dedication in the community. One thing, what is the difference between Storefront API, AJAX API and Headless Storefront API?