Shopify Admin API returns 404 error when updating theme assets

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?

Check your permissions. You can read theme assets, but updating them needs write_themes scope in your API token. I hit this same issue - GET requests worked fine but PUT/POST failed with 404s because I only had read_themes permission. Make sure your API credentials have write permissions for theme changes. Also, some stores block theme API modifications with extra security settings, especially on live themes.

Check how you’re building the endpoint URL. Make sure your store_name variable has the full myshopify.com domain format. I hit this same issue when I forgot the .myshopify.com part in my store name variable. Also verify your auth headers have the right API version in the X-Shopify-Access-Token format. Sometimes you get a 404 when the auth context doesn’t match between GET and PUT requests, even though GET works fine. Log the exact URLs for both requests and compare them - you’ll probably spot the difference.

had the same prob before. shopify needs POST instead of PUT for asset updates. just swap requests.put with requests.post but keep the endpoint the same and it should work!