Add JavaScript code in theme.liquid with out changing exiting code of theme.liquid

Shopify Partner
2 0 0

Hello Devs, 

Actually I want to add JavaScript code in theme.liquid using Asset API, But while editing of theme.liquid,I got http 200 ok and and Javascript code added in theme.liquid but previous exiting code of theme.liquid was totally remove and my new javascript code is added & I don't want to do that.
I just want to add javascript line code in <head> section with existing theme.liquid code. 
Also I am attacing snapshot and API endpoint and sample json submitting.

  1. API Endpoint Name :- 
  2. API doucmentation :-
  3. API key :- 2848e26cb8737f320af05439fdc510eb
  4. Shop Name :-
  5. Method :- Put
  6. Header :-
    1. Content-Type : application/json
    2. X-Shopify-Access-Token :  9f702bc996103d932c989ecdf0824a85
  7. Input Sample Json :- {"asset": {"key": "layout/theme.liquid","value": "{{ content_for_header }}}<script>console.log('foo')</script>{{ content_for_layout }}"    }}
  8. Http Status code :- 200
  9. OutPut Json :- {"asset": {"key": "layout\/theme.liquid","public_url": null,"created_at": "2018-01-05T01:05:07-05:00","updated_at": "2018-01-07T11:41:44-05:00","content_type": "text\/x-liquid","size": 485,"theme_id": 7148240924,"warnings": []}}


1.Image Before Asset API called, theme.liquid context code.


2.After Assest API called previous code is removed and My JavaScript Code added 


Hopefully Above information which is giving by me  it will sufficient.Please help me in these matter.
Last Remember , I just want to add javascript code with exiting theme.liquid in <head> section.

Waiting your reply as soon as possbile. 

Thanks & regards


Shopify Staff
Shopify Staff
1131 82 194

Hey Nisarg, 

When you make a PUT request, whatever you send in the body of your API call will replace what is already there - this is normal and intentional behaviour though and what you're seeing is expected.

What you're trying to accomplish here is possible, however you'd have to fetch the current theme.liquid content first, then insert your code after the <head> tag programmatically somehow, and make your PUT request after that with all of the content combined at the same time. You won't be able to only send in the bits and pieces of information that you need to add. 

Shopify Partner
2 0 0

Hi Josh Wheeler,

Thanks for reply, I understand your explanation for these.


New Member
6 0 0

Hi  Nisarg Raval

Please help .Can you tell me solution . how got your desired output, i am facing same problem. im using put method.its overwrite file. i want to edit any file. 

$addscript = $shopify('PUT /admin/themes/'.$themes[0]['id'].'/assets.json',
                            "asset" => array(
                                "key" => "assets/testing.js",
                                "value" => "{{ 'jquery-1.4.3.min.jsor' | asset_url | script_tag }}",
    $all_assets = $shopify('GET /admin/themes/'.$themes[0]['id'].'/assets.json');
    print_r($all_assets); die('k');


New Member
3 0 0

Has this issue resolved? I also have the same problem. Thank you.