0

Get Discount Code

by
Published Nov 8, 2023

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.

Script shopify Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * Get Discount Code
5
 * 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.
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  code: string | undefined,
10
  discount_id: string | undefined
11
) {
12
  if (!code && !discount_id) {
13
    throw new Error("Pass either code or discount_id")
14
  }
15
  const id = discount_id
16
    ? discount_id.startsWith("gid://")
17
      ? discount_id
18
      : `gid://shopify/DiscountCodeNode/${discount_id}`
19
    : undefined
20

21
  const response = await fetch(
22
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
23
    {
24
      method: "POST",
25
      headers: {
26
        "X-Shopify-Access-Token": auth.token,
27
        "Content-Type": "application/json",
28
        Accept: "application/json",
29
      },
30
      body: JSON.stringify({
31
        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 } } }`,
32
        variables: {
33
          id: id || "gid://shopify/DiscountCodeNode/0",
34
          code: code || "",
35
          byId: !!id,
36
        },
37
      }),
38
    }
39
  )
40

41
  if (!response.ok) {
42
    throw new Error(`${response.status} ${await response.text()}`)
43
  }
44

45
  const { data, errors } = await response.json()
46
  if (errors) {
47
    throw new Error(JSON.stringify(errors))
48
  }
49

50
  return id ? data.byId : data.byCode
51
}
52

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Shopify = {
    2
      token: string;
    3
      store_name: string;
    4
    };
    5
    /**
    6
     * Retrieves a single discount code
    7
     * Retrieves a single discount code
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      price_rule_id: string,
    13
      discount_code_id: string
    14
    ) {
    15
      const url = new URL(
    16
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/price_rules/${price_rule_id}/discount_codes/${discount_code_id}.json`
    17
      );
    18
    
    
    19
      const response = await fetch(url, {
    20
        method: "GET",
    21
        headers: {
    22
          "X-Shopify-Access-Token": auth.token,
    23
        },
    24
        body: undefined,
    25
      });
    26
      if (!response.ok) {
    27
        const text = await response.text();
    28
        throw new Error(`${response.status} ${text}`);
    29
      }
    30
      return await response.json();
    31
    }
    32