GraphQL Admin API - List all Discount Codes

Support said that they don’t answer api questions so I am trying here again. I want to use the API to do what you can do through the admin dashboard which is to go to Discounts and then click on a Discount code, then click View all codes. That shows a list of all the individually generated codes and what Date they were created. Using the following query I can get a list of codes and ids (of type DiscountRedeemCode) for the codes. The problem is that I still don’t see how to get the Created On date. I can use REST or whatever API, it doesn’t have to be GraphQL, I just need a count of all the codes created on a date.

{
  codeDiscountNode(id: "gid://shopify/DiscountCodeNode/11111111111") {
    codeDiscount {
      ... on DiscountCodeBasic {
        codeCount
        codes(first: 100) {
          edges {
            node {
              code
              id
            }
          }
        }
        customerSelection {
          __typename
        }
        startsAt
        endsAt
      }
    }
  }
}
1 Like

Your code is almost there, but I think the first line is restricting it.

I think I managed to get what you’re looking for with this:

codeDiscountNodes(first: 10) {
    nodes {
      id
      codeDiscount {
        __typename
        ... on DiscountCodeBasic {
          title
          createdAt
          startsAt
          endsAt
          status
          summary
          codes(first: 2) {
            nodes {
              code
            }
          }
        }
        ... on DiscountCodeBxgy {
          title
          createdAt
          startsAt
          endsAt
          status
          summary
          codes(first: 2) {
            nodes {
              code
            }
          }
        }
        ... on DiscountCodeFreeShipping {
          title
          createdAt
          startsAt
          endsAt
          status
          summary
          codes(first: 2) {
            nodes {
              code
            }
          }
        }
      }
    }
  }

I appreciate the reply. No one replied at all the last time I asked this.

I was restricting it to a specific case trying to explore that one case in which I am interested in. Unfortunately, this still returns just the main code and not the detail you get in the admin interface when you click on Show All Codes. Basically, what you have here:

codes(first: 2) {
            nodes {
              code
            }
          }

is the same info I get back where I have:

codes(first: 100) {
          edges {
            node {
              code
              id
            }
          }
        }

Also:

codeDiscount → DiscountCodeBasic → codes

always returns only one code and that code ends up being the same as the DiscountCodeBasic → title (even when there are actually hundreds of codes under a code). The info I actually want is either not available, or deeper maybe , or somewhere completely different, I think…

Ah, I see now. My dev store doesn’t have multiple codes per discount. I did some poking around but I don’t see a way to query information at the DiscountRedeemCode level. DiscountRedeemCode only has a few fields … unless a Shopify developer chimes in, I don’t think it possible.