You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Sanity v3脚本中获取Sanity客户端?

在Sanity v3脚本中获取客户端的正确方法

我之前也被这个问题卡了好一会儿——Sanity v3里,之前v2版本常用的*part:@sanity/base/client*导入客户端的方式已经完全失效了,不过替代方案超简单:

直接从sanity/cli里导入getCliClient方法,再配置好指定的API版本,就能拿到可用的客户端实例:

import { getCliClient } from 'sanity/cli';

const client = getCliClient().withConfig({ apiVersion: '2022-09-09' });

之后执行你的脚本时,用这条命令即可:

node_modules/.bin/sanity exec path-to/my-script.js --with-user-token

这样你就能正常使用这个客户端来操作你的Sanity数据集啦。

内容的提问来源于stack exchange,提问作者Wellerson Miranda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 17:30:44