//native
/**
* Get Discount Code
* Get a code discount by one of its redeem codes (e.g. `SUMMER10`) or by its ID (numeric ID or DiscountCodeNode GID). Returns its settings and up to 50 of its codes. Needs the read_discounts scope.
*/
export async function main(
auth: RT.Shopify,
code: string | undefined,
discount_id: string | undefined
) {
if (!code && !discount_id) {
throw new Error("Pass either code or discount_id")
}
const id = discount_id
? discount_id.startsWith("gid://")
? discount_id
: `gid://shopify/DiscountCodeNode/${discount_id}`
: undefined
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: `query ($id: ID!, $code: String!, $byId: Boolean!) { byId: discountNode(id: $id) @include(if: $byId) { id discount { __typename ...B ...X ...S ...A } } byCode: codeDiscountNodeByCode(code: $code) @skip(if: $byId) { id discount: codeDiscount { __typename ...B ...X ...S ...A } } } fragment B on DiscountCodeBasic { title status summary startsAt endsAt usageLimit asyncUsageCount appliesOncePerCustomer createdAt updatedAt codesCount { count } codes(first: 50) { nodes { id code asyncUsageCount } } } fragment X on DiscountCodeBxgy { title status summary startsAt endsAt usageLimit asyncUsageCount appliesOncePerCustomer createdAt updatedAt codesCount { count } codes(first: 50) { nodes { id code asyncUsageCount } } } fragment S on DiscountCodeFreeShipping { title status summary startsAt endsAt usageLimit asyncUsageCount appliesOncePerCustomer createdAt updatedAt codesCount { count } codes(first: 50) { nodes { id code asyncUsageCount } } } fragment A on DiscountCodeApp { title status startsAt endsAt usageLimit asyncUsageCount appliesOncePerCustomer createdAt updatedAt codesCount { count } codes(first: 50) { nodes { id code asyncUsageCount } } }`,
variables: {
id: id || "gid://shopify/DiscountCodeNode/0",
code: code || "",
byId: !!id,
},
}),
}
)
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))
}
return id ? data.byId : data.byCode
}
Submitted by hugo989 3 hours ago