Shopify API 开发:Admin、Storefront 与 Webhook 封面图

Shopify API 开发:Admin、Storefront 与 Webhook

摘要

手动操作太低效?用 API 打通你的业务系统!全面对比 REST 与 GraphQL,实战演示 Webhook 配置与库存同步,让店铺真正实现自动化运转。

先判断问题出现在哪里

不要让人工去干机器该干的事。通过 Admin API 读写店铺数据,通过 Storefront API 构建前端体验,通过 Webhook 实现系统间的实时通信,彻底打通 Shopify 与外部 ERP/CRM 的任督二脉。

Shopify API 开发实战:Admin API、Storefront 与 Webhook 应用 补充检查图 1
这张补充图把正文里的判断、步骤和检查项压缩成清单,方便读者边看边核对。
Shopify API 开发实战:Admin API、Storefront 与 Webhook 应用 补充检查图 2
这张补充图把正文里的判断、步骤和检查项压缩成清单,方便读者边看边核对。

实战步骤

步骤 1:创建自定义应用获取 API 密钥

不要把店铺的后台账号密码给外部服务商,必须通过自定义应用发放权限极小的 Token。

操作路径Shopify后台 -> 设置 -> 应用和销售渠道 -> 开发应用 -> 创建应用

  1. 命名为“ERP库存同步系统”。

  2. 点击 配置 Admin API 范围,只勾选 write_inventoryread_products(权限给得越少越安全)。

  3. 点击 安装应用,系统会生成一串 shpat_ 开头的 Admin API 访问令牌(注意:此 Token 只显示一次,必须立刻复制保存!)

步骤 2:使用 GraphQL 高效批量更新库存

REST API 更新 1000 个 SKU 的库存需要发 1000 次请求,极易触发限流(Rate Limit)。必须改用 GraphQL 批量处理。

操作路径使用 Postman 或你的后端代码发起 POST 请求

// 请求 URL: https://{store-name}.myshopify.com/admin/api/2026-01/graphql.json
// Headers: {"X-Shopify-Access-Token": "shpat_xxxxxxxxxxxx", "Content-Type": "application/json"}

// GraphQL Mutation: 一次性更新多个位置的库存
mutation inventoryBulkAdjustQuantityAtLocation($inventoryItemAdjustments: [InventoryAdjustItemInput!]!, $locationId: ID!) {
  inventoryBulkAdjustQuantityAtLocation(inventoryItemAdjustments: $inventoryItemAdjustments, locationId: $locationId) {
    inventoryLevels {
      id
      available
      item {
        sku
      }
    }
    userErrors {
      field
      message
    }
  }
}

// Variables (传入的具体数据)
{
  "locationId": "gid://shopify/Location/1234567890",
  "inventoryItemAdjustments": [
    {"inventoryItemId": "gid://shopify/InventoryItem/11111111", "availableDelta": 50},
    {"inventoryItemId": "gid://shopify/InventoryItem/22222222", "availableDelta": -10}
  ]
}

步骤 3:配置 Webhook 实现订单实时推送

不要让 ERP 每隔 5 分钟来轮询 Shopify 有没有新订单,这会浪费大量服务器资源。让 Shopify 在客户付款成功的很快,主动把订单数据推给 ERP。

操作路径Shopify后台 -> 设置 -> 通知 -> 滚动到最底部 (Webhooks) -> 创建 Webhook

  1. 事件 (Event):选择 订单创建 (Order creation)订单付款 (Order payment)

  2. 格式 (Format):选择 JSON

  3. URL:填入你 ERP 系统的接收地址(如 https://api.your-erp.com/shopify/webhook/order)。

  4. Webhook API 版本:选择最新的稳定版(如 2026-01)。

常见误区与处理方法

误区一:无视 API 速率限制 (Rate Limits) 导致接口被封

写了个脚本,一秒钟向 Shopify 发送了 50 次请求去抓取订单数据。Shopify 的 GraphQL 接口采用漏桶算法(Bucket limit),一旦超限,会直接返回 429 Too Many Requests 错误,甚至临时封禁你的 IP。

规避方法:在你的后端代码中必须实现 重试机制 (Retry Logic) 和退避算法 (Exponential Backoff)。每次请求 Shopify 时,检查响应头中的 X-Shopify-Shop-Api-Call-Limit,如果发现即将触顶,强制让代码 sleep(2000) 暂停 2 秒再发。

误区二:Webhook 接收端没有验证 HMAC 签名

你的 ERP 开放了一个接收订单的 URL。黑客发现了这个 URL,伪造了一堆“已付款”的假订单 JSON 发给你的 ERP,导致仓库发出了价值 10 万美金的货。

规避方法绝对不能无条件信任收到的 Webhook 数据! Shopify 在发送 Webhook 时,会在 HTTP 请求头 X-Shopify-Hmac-Sha256 中附带一个签名。你的服务器接收到数据后,必须使用你在 Shopify 后台获取的 Webhook 密钥 (Client Secret) 对收到的 Body 进行 SHA256 哈希计算,比对结果是否与请求头中的签名一致。不一致的请求直接丢弃并报警。

常见问题

修改API 开发前要不要备份主题?

要。主题开发、Liquid、API 或性能优化都建议先复制主题或使用 Git 分支,改完后再检查首页、产品页、购物车和结账路径。

没有开发经验可以照着做吗?

可以先做低风险配置和页面检查;涉及代码、API、Webhook 或结账逻辑时,建议先在测试主题或测试店铺验证,再同步到线上主题。

API 开发应该先看哪个核心指标?

先看能直接影响决策的指标,不要只看曝光或访问量。新手可以把转化率、获客成本、客单价、复购或退款情况放在同一张表里,每周复盘一次。

做API 开发前需要准备什么?

先确认目标、当前数据、页面或后台路径,再准备一份改动记录。这样出现波动时能追溯原因,也方便后续把有效动作沉淀成 SOP。

API 开发多久复盘一次比较合适?

运营类动作建议每周小复盘、每月大复盘;广告或转化测试不要因为单日波动频繁改动,至少等到有足够样本后再判断。

新手最容易踩的坑是什么?

最常见的问题是同时改太多变量,最后不知道是哪一步带来结果。每次只改一个关键点,保留截图、数据和发布时间,后续才有可复用的经验。

下一步阅读

0 comments

Leave a comment

Please note, comments need to be approved before they are published.

📢 Share this article

Any other questions?

Our professional team is ready to answer your questions.

Was this article helpful to me?

This article is suitable for all merchants and developers who want to learn about Shopify. Whether you are a beginner just starting out with Shopify or an advanced user looking to improve your skills, you will gain practical knowledge and techniques from it. The methods in this article have all been tested and proven in practice and can be directly applied to your projects.

How can we apply the methods described in the article?

Each step in this article comes with detailed instructions and code examples, which you can directly copy and use. It's recommended to try it in a test environment first to confirm the results before applying it to the production site. If you encounter any problems during implementation, feel free to leave a comment or join our discussion group for help; we and our community members will be happy to assist you.

Can the code in the article be used directly?

Yes! All the code examples we provide have been tested and can be used directly in your Shopify theme. Remember to adjust the parameters and styles according to your actual needs. If you encounter any problems, feel free to leave a message for discussion.

How often will new content be updated?

We publish 2-3 high-quality Shopify tutorials and operational tips every week. Follow our WeChat official account or join our discussion group to get the latest content and exclusive resources first.

Can I get help if I encounter a problem?

Of course! You can leave a comment below the article or join our WeChat group to connect with 1000+ Shopify merchants and developers. We'll get back to you as soon as possible.

Ready to get started?

Follow us to get the latest Shopify tutorials and operational tips.

Join the community Contact Us