//native
/**
* Add Discount Codes (Batch)
* Add up to 250 extra redeem codes to an existing code discount (numeric ID or DiscountCodeNode GID). Runs asynchronously: the returned bulkCreation has done=false at first; poll it with Get Discount Code (or GraphQL Query on discountRedeemCodeBulkCreation) to see importedCount and failedCount. Needs the write_discounts scope.
*/
export async function main(
auth: RT.Shopify,
discount_id: string,
codes: string[]
) {
const id = discount_id.startsWith("gid://")
? discount_id
: `gid://shopify/DiscountCodeNode/${discount_id}`
const response = await fetch(
`https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
{
method: "POST",
headers: {
"X-Shopify-Access-Token": auth.token,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({
query: `mutation ($discountId: ID!, $codes: [DiscountRedeemCodeInput!]!) { discountRedeemCodeBulkAdd(discountId: $discountId, codes: $codes) { bulkCreation { id done codesCount importedCount failedCount createdAt } userErrors { field message } } }`,
variables: { discountId: id, codes: codes.map((code) => ({ code })) },
}),
}
)
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const { data, errors } = await response.json()
if (errors) {
throw new Error(JSON.stringify(errors))
}
if (data.discountRedeemCodeBulkAdd.userErrors.length) {
throw new Error(JSON.stringify(data.discountRedeemCodeBulkAdd.userErrors))
}
return data.discountRedeemCodeBulkAdd
}
Submitted by hugo989 3 hours ago