How to add custom.js file in to my theme's asset folder by using php API call

this is my API request to add custom.js file to my theme’s asset folder "

<?php include_once("includes/mysql_connect.php"); include_once("includes/shopify.php"); $shopify = new Shopify(); $parameters = $_GET; include_once("includes/check_token.php"); $script_url="[https://7157-182-161-9-226.ngrok-free.app/permalinks/scripts/custom.js](https://7157-182-161-9-226.ngrok-free.app/permalinks/scripts/custom.js)"; if($_SERVER['REQUEST_METHOD'] == 'POST'){ if($_POST['action_type']=='create_script'){ // Get the active theme $themes = $shopify->rest_api('/admin/api/2023-04/themes.json', array(), 'GET'); $themes = json_decode($themes['body'], true); $active_theme = null; foreach ($themes['themes'] as $theme) { if ($theme['role'] === 'main') { $active_theme = $theme; break; } } if ($active_theme !== null) { $themeId = $active_theme['id']; echo print_r($themeId); // Update the custom.js file in the theme assets $theme_script_data = array( "asset" => array( "key" => "custom.js", "value" => file_get_contents('custom.js'), ), ); $update_asset = $shopify->rest_api('/admin/api/2023-04/themes/' . 150597304615 . '/assets.json', $theme_script_data, 'PUT'); $update_asset = json_decode($update_asset['body'], true); // Create or update the script tag $scriptTag_data = array( "script_tag" => array( "event" => "onload", "src" => $script_url, ), ); // Check if the script tag already exists $existing_script_tags = $shopify->rest_api('/admin/api/2023-04/script_tags.json', array(), 'GET'); $existing_script_tags = json_decode($existing_script_tags['body'], true); $scriptTagExists = false; foreach ($existing_script_tags['script_tags'] as $existing_tag) { if ($existing_tag['src'] === $script_url) { $scriptTagExists = true; $scriptTag_data['script_tag']['id'] = $existing_tag['id']; break; } } // Create or update the script tag based on existence if ($scriptTagExists) { $update_script_tag = $shopify->rest_api('/admin/api/2023-04/script_tags/' . $scriptTag_data['script_tag']['id'] . '.json', $scriptTag_data, 'PUT'); $update_script_tag = json_decode($update_script_tag['body'], true); } else { $create_script_tags = $shopify->rest_api('/admin/api/2023-04/script_tags.json', $scriptTag_data, 'POST'); $create_script_tags = json_decode($create_script_tags['body'], true); } } } } $collections_query = array( "query" => '{ collections(first: 10) { edges { node { id title description metafield(key: "custom.short_description") { value } } } } }' ); $collections_response = $shopify->grapg_ql($collections_query); $collections_response = json_decode($collections_response['body'], true); $collections = array(); if (isset($collections_response['data']['collections']['edges'])) { $collection_edges = $collections_response['data']['collections']['edges']; foreach ($collection_edges as $collection_edge) { $collection_node = $collection_edge['node']; $collection_id = $collection_node['id']; $collection_title = $collection_node['title']; $collection_description = $collection_node['description']; $short_description = isset($collection_node['metafield']['value']) ? $collection_node['metafield']['value'] : ''; $collections[$collection_id] = array( 'title' => $collection_title, 'description' => $collection_description, 'short_description' => $short_description ); } } $script_tags = $shopify->rest_api('/admin/api/2023-04/script_tags.json', array(), 'GET'); $script_tags = json_decode($script_tags['body'], true); // echo print_r($script_tags); ?> <?php include_once("header.php") ?>
Create Script Tag

Collections:

<?php foreach ($collections as $collection_id => $collection_data) { $collection_title = $collection_data['title']; $collection_description = $collection_data['description']; $short_description = $collection_data['short_description'];

echo ‘

’;
echo ‘’;
echo ‘’;
echo ‘’;
echo ‘’;
}
?>

Title Main Description Short Description
’ . $collection_title . ‘’ . $collection_description . ‘’ . $short_description . ‘
<?php include_once("footer.php") ?>" here I use rest API call , in shopify.php my restapi call function is "

public function rest_api($api_endpoint, $query = array(), $method = ‘GET’){
$url = ‘https://’ . $this->shop_url . $api_endpoint;

if (in_array($method, array(‘GET’,‘DELETE’)) && !is_null($query) ){
$url = $url . ‘?’ . http_build_query($query);
}

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);

$headers = “”;

if(!is_null( $this->access_token )){
$headers = "X-Shopify-Access-Token: ". $this->access_token;
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}

if ($method !=‘GET’ && in_array($method, array(‘POST’, ‘PUT’))){
if (is_array($query) ) $query = http_build_query($query);
curl_setopt($curl, CURLOPT_POSTFIELDS, $query);

}

$responce = curl_exec($curl);
$error = curl_errno($curl);
$error_msg=curl_error($curl);

curl_close($curl);

if($error){
return $error_msg;
}else{
$responce = preg_split(“/\r\n\r\n|\n\n|\r\r/”, $responce, 2);
// echo print_r($responce);

$headers = array();
$headers_content = explode(“\n”, $responce[0]);

$headers[‘status’] = $headers_content[0];

array_shift($headers_content);

foreach($headers_content as $content){
$data = explode(‘:’, $content);
$headers[ trim( $data[0] ) ] = trim($data[1]);

}

return array(‘headers’ => $headers, ‘body’ => $responce[1]);
}
}"

my restapi function is working properly with other requests. but I need to add custom.js file to my theme’s asset folder making API call. its not working like I cant find what the error is.

The asset api is going away at the end of the year and if you’re on the latest api version it’s already gone.

You’ll need to find another way to achieve your goal.