//native
/**
* List Discount Codes
* List code discounts (basic, buy X get Y, free shipping and app discounts), filtered with Shopify's search syntax (e.g. `status:active`, `discount_type:percentage`, `title:'Summer sale'`). Returns one page; pass pageInfo.endCursor as after for the next. Needs the read_discounts scope.
*/
export async function main(
auth: RT.Shopify,
query: string | undefined,
sort_key:
| "CREATED_AT"
| "UPDATED_AT"
| "STARTS_AT"
| "ENDS_AT"
| "TITLE"
| "ID"
| "RELEVANCE"
| undefined,
reverse: boolean | undefined,
first: number | undefined,
after: string | 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 ($first: Int!, $after: String, $query: String, $sortKey: DiscountSortKeys, $reverse: Boolean) { discountNodes(first: $first, after: $after, query: $query, sortKey: $sortKey, reverse: $reverse) { nodes { id discount { __typename ... on DiscountCodeBasic { title status summary startsAt endsAt usageLimit asyncUsageCount appliesOncePerCustomer createdAt updatedAt codesCount { count } codes(first: 10) { nodes { id code } } } ... on DiscountCodeBxgy { title status summary startsAt endsAt usageLimit asyncUsageCount appliesOncePerCustomer createdAt updatedAt codesCount { count } codes(first: 10) { nodes { id code } } } ... on DiscountCodeFreeShipping { title status summary startsAt endsAt usageLimit asyncUsageCount appliesOncePerCustomer createdAt updatedAt codesCount { count } codes(first: 10) { nodes { id code } } } ... on DiscountCodeApp { title status startsAt endsAt usageLimit asyncUsageCount appliesOncePerCustomer createdAt updatedAt codesCount { count } codes(first: 10) { nodes { id code } } } } } pageInfo { hasNextPage endCursor } } }`,
variables: {
first: first || 50,
after: after || undefined,
query: query ? `method:code ${query}` : "method:code",
sortKey: sort_key || undefined,
reverse: reverse || undefined,
},
}),
}
)
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 data.discountNodes
}
Submitted by hugo989 3 hours ago