← 返回
未分类 Key 中文

Sanity CMS

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 documen...
通过GROQ查询和Sanity HTTP API查询和管理Sanity CMS内容。用于在被要求从Sanity数据集中获取内容、创建或更新文档时使用。
dwhite-oss dwhite-oss 来源
未分类 clawhub v1.0.0 1 版本 100000 Key: 需要
★ 0
Stars
📥 354
下载
💾 1
安装
1
版本
#cms#content#groq#headless-cms#latest#sanity

概述

Sanity CMS Skill

Sanity API base: https://.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

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-05-07 05:02 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

dev-programming

Mcporter

steipete
使用 mcporter CLI 直接列出、配置、认证及调用 MCP 服务器/工具(支持 HTTP 或 stdio),涵盖临时服务器、配置编辑及 CLI/类型生成功能。
★ 195 📥 67,629
it-ops-security

Fly.io

dwhite-oss
使用 flyctl CLI 和 Machines API 在 Fly.io 上部署和管理应用,包括部署、扩缩机器、查看状态和日志等。
★ 0 📥 394
dev-programming

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 677 📥 327,225