AgentSkillsCN

shopify-api-integration

当用户询问“Shopify GraphQL”“Admin API”“元字段”“Webhook”“速率限制”“分页”“App Bridge”,或任何 Shopify API 集成相关需求时,可运用此技能。它将提供 Shopify API 模式、速率限制的处理方法,以及各类最佳实践。

SKILL.md
--- frontmatter
name: shopify-api-integration
description: Use this skill when the user asks about "Shopify GraphQL", "Admin API", "metafields", "webhooks", "rate limiting", "pagination", "App Bridge", or any Shopify API integration work. Provides Shopify API patterns, rate limit handling, and best practices.

Shopify API Best Practices

Quick Reference

TopicReference File
GraphQL Queries, Pagination, Mutationsreferences/graphql.md
HMAC Verification, Webhook Handlersreferences/webhooks.md
Metafield Types, Batch Operationsreferences/metafields.md
Direct API Calls, Resource Pickerreferences/app-bridge.md

API Version Check (CRITICAL)

Always verify API version before implementing!

javascript
// Check what version your app uses
// shopify.app.toml
[api]
api_version = "2024-10"  // Verify this matches your implementation

API Selection Guide

NeedSolution
Customize checkout UICheckout UI Extension
Apply discountsDiscount Function
Validate cartCart Validation Function
React to eventsWebhooks
Read/write dataGraphQL Admin API
Sync large dataBulk Operations
Store custom dataMetafields/Metaobjects

Volume Decision Guide

VolumeStrategy
< 50 itemsRegular GraphQL
50-500 itemsBatch with Cloud Tasks + rate limiting
500+ itemsBulk Operations API

For detailed bulk mutation patterns, see: shopify-bulk-operations skill


Rate Limiting

Rate Limits:

  • Regular metafield API: 2 requests/second, 40 requests/minute
  • Bulk Operations: No rate limits - runs server-side on Shopify

Cloud Tasks (Recommended for Rate Limits)

javascript
// BAD: In-function sleep wastes CPU time
await sleep(60000); // 60s sleep = 60s CPU billed

// GOOD: Schedule retry with Cloud Tasks
async function scheduleRetry(payload, delaySeconds) {
  await client.createTask({
    parent: client.queuePath(project, location, 'shopify-retry'),
    task: {
      httpRequest: {
        url: `${baseUrl}/api/retry-shopify`,
        body: Buffer.from(JSON.stringify(payload)).toString('base64')
      },
      scheduleTime: {
        seconds: Math.floor(Date.now() / 1000) + delaySeconds
      }
    }
  });
}

Quick Patterns

GraphQL Query

javascript
const response = await shopify.graphql(`
  query getProduct($id: ID!) {
    product(id: $id) {
      id
      title
    }
  }
`, { id: productId });

Set Metafield

javascript
await shopify.graphql(`
  mutation metafieldsSet($metafields: [MetafieldsSetInput!]!) {
    metafieldsSet(metafields: $metafields) {
      userErrors { field message }
    }
  }
`, {
  metafields: [{
    ownerId: customerId,
    namespace: 'loyalty',
    key: 'points',
    type: 'number_integer',
    value: '500'
  }]
});

Webhook Response (CRITICAL: < 5 seconds)

javascript
app.post('/webhooks/orders/create', async (req, res) => {
  if (!verifyHmac(req)) {
    return res.status(401).send('Unauthorized');
  }

  await enqueue('orders/create', req.body);
  res.status(200).send('OK');
});