如何通过GraphQL与API获取Shopify已安装店铺并检查活跃订阅
实现步骤与示例代码
一、获取所有安装应用的店铺列表
要获取所有安装你应用的店铺,必须使用Shopify Partner GraphQL API,而非单个店铺的API。请求端点为 https://partners.shopify.com/{{partner_id}}/api/2024-07/graphql.json,其中{{partner_id}}替换为你的合作伙伴ID。
前提条件
- 拥有Shopify Partner账户,且具备该应用的管理权限
- 持有Partner API密钥(用于请求身份验证)
GraphQL查询示例
query GetInstalledShops($first: Int!, $after: String) { app(id: "gid://shopify/App/{{your_app_id}}") { shops(first: $first, after: $after) { edges { node { id name myshopifyDomain createdAt } } pageInfo { hasNextPage endCursor } } } }
参数说明:
{{your_app_id}}:替换为你的应用ID(可在Partner后台应用详情页找到)first:单页返回的店铺数量,最大值为100after:分页游标,用于获取下一页数据(首次请求可传null)
Node.js示例代码
const fetch = require('node-fetch'); // 替换为你的实际信息 const PARTNER_ID = '你的合作伙伴ID'; const APP_ID = '你的应用ID'; const PARTNER_API_TOKEN = '你的Partner API密钥'; async function getAllInstalledShops() { let allShops = []; let hasNextPage = true; let endCursor = null; while (hasNextPage) { const query = ` query GetInstalledShops($first: Int!, $after: String) { app(id: "gid://shopify/App/${APP_ID}") { shops(first: $first, after: $after) { edges { node { id name myshopifyDomain createdAt } } pageInfo { hasNextPage endCursor } } } } `; const response = await fetch(`https://partners.shopify.com/${PARTNER_ID}/api/2024-07/graphql.json`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Shopify-Access-Token': PARTNER_API_TOKEN }, body: JSON.stringify({ query, variables: { first: 100, after: endCursor } }) }); const data = await response.json(); const shopsPage = data.data.app.shops.edges.map(edge => edge.node); allShops = [...allShops, ...shopsPage]; hasNextPage = data.data.app.shops.pageInfo.hasNextPage; endCursor = data.data.app.shops.pageInfo.endCursor; } return allShops; } // 调用示例 getAllInstalledShops().then(shops => console.log(shops)).catch(err => console.error(err));
二、逐个检查店铺的活跃订阅计划
每个店铺的订阅信息需通过单个店铺的GraphQL API查询,端点为 https://{{shop_domain}}/admin/api/2024-07/graphql.json,需使用该店铺对应的应用访问令牌(应用安装时获取并存储)。
前提条件
- 应用已申请
read_own_subscriptions权限(在Partner后台应用设置的权限配置中添加) - 已存储每个店铺的应用访问令牌
GraphQL查询示例(获取活跃订阅)
query GetActiveSubscriptions { currentAppInstallation { activeSubscriptions { id name status lineItems { plan { pricingDetails { ... on AppRecurringPricing { price { amount currencyCode } interval } } } } trialDaysRemaining createdAt currentPeriodEnd } } }
核心字段说明:
status:订阅状态,ACTIVE表示当前活跃lineItems.plan.pricingDetails:包含订阅价格、结算周期(如MONTHLY/ANNUALLY)currentPeriodEnd:当前订阅周期的结束时间
Node.js示例代码(批量检查)
async function checkShopSubscriptions(shops) { const results = []; for (const shop of shops) { // 从你的数据库中取出该店铺对应的应用访问令牌 const SHOP_ACCESS_TOKEN = '该店铺的应用访问令牌'; const shopDomain = shop.myshopifyDomain; try { const query = ` query GetActiveSubscriptions { currentAppInstallation { activeSubscriptions { id name status lineItems { plan { pricingDetails { ... on AppRecurringPricing { price { amount currencyCode } interval } } } } trialDaysRemaining createdAt currentPeriodEnd } } } `; const response = await fetch(`https://${shopDomain}/admin/api/2024-07/graphql.json`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Shopify-Access-Token': SHOP_ACCESS_TOKEN }, body: JSON.stringify({ query }) }); const data = await response.json(); // 过滤出活跃状态的订阅 const activeSubscriptions = data.data.currentAppInstallation.activeSubscriptions.filter(sub => sub.status === 'ACTIVE'); results.push({ shopId: shop.id, shopName: shop.name, shopDomain: shopDomain, activeSubscriptions: activeSubscriptions }); } catch (err) { results.push({ shopId: shop.id, shopName: shop.name, shopDomain: shopDomain, error: err.message }); } } return results; } // 调用示例:先获取店铺列表,再批量检查订阅 getAllInstalledShops() .then(shops => checkShopSubscriptions(shops)) .then(results => console.log(results)) .catch(err => console.error(err));
关键注意事项
- API版本:示例使用2024-07版本,建议使用Shopify最新的稳定API版本
- 速率限制:Shopify API有请求频率限制,批量查询时需控制请求间隔,避免被限流
- 令牌安全:务必加密存储Partner API密钥和店铺访问令牌,防止泄露
内容的提问来源于stack exchange,提问作者R P
相关产品推荐
相关产品推荐

