How to fetch the current billing cycle through the GraphQL API?

Solved

How to fetch the current billing cycle through the GraphQL API?

sinanspd
Shopify Partner
4 0 1

I am developing a Shopify app and I need to fetch some information on whether the user's subscription is active. If it is they will be allowed to use the app and if it isn't they will be redirected to a page prompting them to re-subscribe. 

 

I am looking at the current stable GraphQL API (2024-01) and there are no queries under the billing section that would provide me this information. 

 

There are various objects listed, including `AppSubscription` (https://shopify.dev/docs/api/admin-graphql/2024-01/objects/AppSubscription) that contains the information I need, but there doesn't seem to be any queries that return this object. There are only mutations, and I surely shouldn't need to mutate the billing records just to retrieve some information about them. 

 

Does anyone know how I can fetch this info/object? 

Accepted Solution (1)

ThatOneCoder
Shopify Partner
1 1 1

This is an accepted solution.

I also spend a lot of time trying to figure this out, as they lack documentation to guide developers with this task. I discovered this with lots of trial and error using the GraphQL tool they offer. This is the snippet that I use to pull out IDs of the active subscriptions that are linked to your app.

export async function CheckForSubscriptions(admin: AdminApiContext) {
    const gqlRes = await admin.graphql(
        `#graphql
        {
            currentAppInstallation {
                activeSubscriptions {
                id
                lineItems {
                    id
                    plan {
                        pricingDetails {
                            ... on AppUsagePricing {
                            __typename
                            }
                        }
                    }
                }
                status
                currentPeriodEnd
                name
                }
            }
        }
        `
    )
    const res = await gqlRes.json() as SubscriptionCheck;
    console.log('subscription check', res.data.currentAppInstallation.activeSubscriptions)
    return res
}

I hope it helps.

View solution in original post

Reply 1 (1)

ThatOneCoder
Shopify Partner
1 1 1

This is an accepted solution.

I also spend a lot of time trying to figure this out, as they lack documentation to guide developers with this task. I discovered this with lots of trial and error using the GraphQL tool they offer. This is the snippet that I use to pull out IDs of the active subscriptions that are linked to your app.

export async function CheckForSubscriptions(admin: AdminApiContext) {
    const gqlRes = await admin.graphql(
        `#graphql
        {
            currentAppInstallation {
                activeSubscriptions {
                id
                lineItems {
                    id
                    plan {
                        pricingDetails {
                            ... on AppUsagePricing {
                            __typename
                            }
                        }
                    }
                }
                status
                currentPeriodEnd
                name
                }
            }
        }
        `
    )
    const res = await gqlRes.json() as SubscriptionCheck;
    console.log('subscription check', res.data.currentAppInstallation.activeSubscriptions)
    return res
}

I hope it helps.