I’m working on injecting a JavaScript widget into the main theme file of a Shopify store through their Admin API.
theme_asset_endpoint = f"https://{store_name}/admin/api/2025-01/themes/{current_theme}/assets.json"
query_params = {"asset[key]": "layout/theme.liquid"}
# This works perfectly - returns 200
get_response = requests.get(theme_asset_endpoint, headers=auth_headers, params=query_params)
if get_response.status_code != 200:
return {"error": "Cannot fetch theme file", "info": get_response.json()}
file_info = get_response.json().get("asset", {})
existing_content = file_info.get("value", "")
widget_code = f'<script type="module" src="{WIDGET_SOURCE}"></script>'
if widget_code in existing_content:
return {"message": "Widget already exists in theme file"}
if "</body>" in existing_content:
modified_content = existing_content.replace("</body>", widget_code + "\n</body>")
else:
modified_content = existing_content + "\n" + widget_code
update_data = {"asset": {"key": "layout/theme.liquid", "value": modified_content}}
update_endpoint = f"https://{store_name}/admin/api/2025-01/themes/{current_theme}/assets.json"
# This fails with 404 error
put_response = requests.put(update_endpoint, headers=auth_headers, json=update_data)
print(put_response.status_code) # Shows 404
The weird thing is that reading the asset works fine but updating it throws a 404. I’m using the same API version and authentication for both calls. The theme file definitely exists since I can retrieve it. Anyone know what might be causing this issue?