I'm trying to add a chat script to layout/theme.liquid in a Shopify store using the Admin API.
GET request works fine (200 OK), and I successfully retrieve the asset content. PUT request fails with a 404 Not Found error when trying to update the asset.
asset_url = f"https://{shop}/admin/api/2025-01/themes/{theme_id}/assets.json"
params = {"asset[key]": "layout/theme.liquid"}
# here 200
asset_resp = requests.get(asset_url, headers=headers, params=params)
if asset_resp.status_code != 200:
return {"error": "Failed to get theme.liquid", "details": asset_resp.json()}
asset_data = asset_resp.json().get("asset", {})
current_content = asset_data.get("value", "")
script_tag = f'<script type="module" src="{SCRIPT_URL}"></script>'
if script_tag in current_content:
return {"message": "Script already injected in theme.liquid"}
if "</body>" in current_content:
new_content = current_content.replace("</body>", script_tag + "\n</body>")
print("script iputed")
else:
new_content = current_content + "\n" + script_tag
update_payload = {"asset": {"key": "layout/theme.liquid", "value": new_content}}
update_url = f"https://{shop}/admin/api/2025-01/themes/{theme_id}/assets.json"
# BUT here 404
update_resp = requests.put(update_url, headers=headers, json=update_payload)
print(update_resp.url)
print(update_resp.status_code)
API version is consistent (2025-01 for both GET and PUT). Access token is correct (same token works for GET). The asset key exists (GET confirms it). I’m using JSON body for PUT (not params).