Changing shipping cost in Woocommerce

I am creating a plugin that connects to an 3rd API to request the shipping cost based on the address of the person and the dimensions of the product.

Everything is fine but now I need the values ​​returned by the API to be added to the prices of the shipping methods and I tried with this code but it still doesn’t work.

add_filter('woocommerce_package_rates','test_overwrite_',100,2); function test_overwrite_($  rates,$  package) {  foreach ($  rates as $  rate) {  //Set the price $  rate->cost = $  phpVar1;   <-//variable that receives the value of the shipment }  return $  rates; } 

But I don’t find any way to change the shipping cost with functions..

Is it possible ? Thanks