360 Secure SkillHub
360 安全技能中心

Sanity CMS

该 Skill 提供一组 curl 命令示例,用 GROQ 从 Sanity 数据集读取文档,并通过 mutate 接口创建、更新、删除文档及发布草稿。需要配置项目 ID、数据集和 API 令牌三个环境变量,正文含 GROQ 语法要点与并发、权限提示。删除操作具有破坏性,无回滚或确认流程。

dwhite-oss
dwhite-oss
热度 51.19
最近更新 2026-05-11 17:37:27
分类:自动化依赖:代码执行 / 第三方 API应用场景:API 集成 / 数据处理 / 自动化脚本 / 工作流

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add sanity
Metadata
name
sanity
description
Query and manage Sanity CMS content via GROQ queries and the Sanity HTTP API. Use when asked to fetch content from a Sanity dataset, create or update documents, upload assets, run GROQ queries, manage drafts, or interact with Sanity Content Lake. Requires SANITY_PROJECT_ID, SANITY_DATASET, and SANITY_API_TOKEN env vars.

Sanity CMS Skill

Sanity API base: https://<projectId>.api.sanity.io/v2021-10-21

Env Vars

SANITY_PROJECT_ID=abc123
SANITY_DATASET=production   # or staging
SANITY_API_TOKEN=sk...      # from sanity.io/manage > API > Tokens
SANITY_BASE="https://$SANITY_PROJECT_ID.api.sanity.io/v2021-10-21"

GROQ Queries (Read)

Fetch All Documents of a Type

curl -G "$SANITY_BASE/data/query/$SANITY_DATASET" \
  -H "Authorization: Bearer $SANITY_API_TOKEN" \
  --data-urlencode 'query=*[_type == "post"]{_id, title, slug, publishedAt}'

Fetch Single Document by Slug

curl -G "$SANITY_BASE/data/query/$SANITY_DATASET" \
  -H "Authorization: Bearer $SANITY_API_TOKEN" \
  --data-urlencode 'query=*[_type == "post" && slug.current == $slug][0]' \
  --data-urlencode '$slug="my-blog-post"'

Complex Query with References

# Posts with author name resolved
*[_type == "post"]{title, "author": author->name, "category": category->title, body}

Count Documents

count(*[_type == "post"])

Mutations (Create/Update/Delete)

Create Document

curl -X POST "$SANITY_BASE/data/mutate/$SANITY_DATASET" \
  -H "Authorization: Bearer $SANITY_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "mutations": [{
      "create": {
        "_type": "post",
        "_id": "drafts.new-post-id",
        "title": "New Blog Post",
        "slug": {"_type": "slug", "current": "new-blog-post"},
        "publishedAt": "2026-03-27T00:00:00Z"
      }
    }]
  }'

Update Document (Patch)

curl -X POST "$SANITY_BASE/data/mutate/$SANITY_DATASET" \
  -H "Authorization: Bearer $SANITY_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "mutations": [{
      "patch": {
        "id": "<document_id>",
        "set": {"title": "Updated Title", "status": "published"}
      }
    }]
  }'

Delete Document

-d '{"mutations": [{"delete": {"id": "<document_id>"}}]}'

Publish Draft

# Move draft to published: delete draft, create published version
-d '{
  "mutations": [
    {"delete": {"id": "drafts.<doc_id>"}},
    {"createOrReplace": {"_id": "<doc_id>", "_type": "post", ...fields}}
  ]
}'

GROQ Syntax Reference

  • Filter: *[_type == "post" && status == "published"]
  • Sort: | order(publishedAt desc)
  • Slice: [0...10]
  • Projection: {title, "authorName": author->name}
  • References: -> dereferences a reference field
  • $param for query parameters (pass as $paramName=value)

Tips

  • IDs starting with drafts. are unpublished drafts
  • Use _rev field to detect conflicts on concurrent edits
  • Token needs "Editor" role minimum for mutations; "Viewer" for queries only
  • GROQ playground: sanity.io/manage > your project > Vision tab