Save PriceRule does not return created object

Topic summary

Main issue: Creating a Shopify Admin REST PriceRule via admin.rest.resources.PriceRule.save() successfully creates the rule, but the awaited return value is undefined, contrary to expectations from the docs.

Observed behavior: Logging either the saved result or the awaited call returns undefined. However, the rule is created and visible when listing all PriceRules, indicating the API call succeeds.

Resolution: Use save({ update: true }) and read the updated price_rule instance instead of relying on the function’s return value. The SDK mutates the existing resource object with server-assigned fields (including id), allowing immediate access via price_rule.id for subsequent discount code creation.

Outcome/Status: Problem resolved by not assigning the await result to a variable and instead inspecting the original price_rule after save(). No further action items or open questions noted; a follow-up confirms and thanks the solution.

Summarized with AI on January 8. AI used: gpt-5.

I am attempting to create a price rule using admin.rest.resource. the documentation states that the response will contain the created price rule. however, when I attempt to retrieve the response, it is returning undefined.

https://shopify.dev/docs/api/admin-rest/2024-01/resources/pricerule#post-price-rules

const price_rule = new admin.rest.resources.PriceRule({session: session});
price_rule.title = “TEST”;
price_rule.target_type = “line_item”;
price_rule.target_selection = “all”;
price_rule.allocation_method = “across”;
price_rule.value_type = “fixed_amount”;
price_rule.value = “-10.0”;
price_rule.customer_selection = “all”;
price_rule.starts_at = “2018-03-22T00:00:00-00:00”;

const priceResponse = await price_rule.save({
update: true,
});
console.log(‘priceResponse:’+JSON.stringify(priceResponse));

I have also tried just logging the response without using a variable:

console.log(‘save:’+JSON.stringify(await price_rule.save({
update: true,
})));

it also returns undefined.

Both of these will create the price rule and I can retrieve it if i list all the price rules:

console.log(‘price rule list:’+JSON.stringify(await admin.rest.resources.PriceRule.all({
session: session,
})));

How do I get the id of the created price rule so I can use it in creating a discount code? I don’t want to query the price rules every time i want to create a discount and loop over those trying to find my newly created price rule.

Thanks

I found the solution. I don’t need to assign the await to a new variable. the update:true will update the price_rule variable with the newly created id.

This works and returns an id i can pass into the create discount code:

const price_rule = new admin.rest.resources.PriceRule({session: session});
price_rule.title = “Price Rule Title”;
price_rule.target_type = “line_item”;
price_rule.target_selection = “all”;
price_rule.allocation_method = “across”;
price_rule.value_type = “fixed_amount”;
price_rule.value = “-10.0”;
price_rule.customer_selection = “all”;
price_rule.starts_at = “2018-03-22T00:00:00-00:00”;

await price_rule.save({
update: true,
});
console.log(‘price_rule:’+JSON.stringify(price_rule));

Glad you figured this out and thanks for coming back to post your solution!