devto

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
Call the Forem API v1 (dev.to) with
curl + jq
. The user's API key is in
$DEVTO_API_KEY
; every call needs the headers
api-key: $DEVTO_API_KEY
and
Accept: application/vnd.forem.api-v1+json
. Base URL:
https://dev.to/api
.
Errors come back as JSON with an
error
/
status
field — show them verbatim.
401
means the API key is invalid → the user must re-connect the DEV connector.
Always start by confirming the key and learning the account:
bash
curl -sS -H "api-key: $DEVTO_API_KEY" -H "Accept: application/vnd.forem.api-v1+json" \
  "https://dev.to/api/users/me" | jq '{username, name}'
使用
curl + jq
调用Forem API v1(dev.to)。用户的API密钥存储在
$DEVTO_API_KEY
中;每次调用都需要携带请求头
api-key: $DEVTO_API_KEY
Accept: application/vnd.forem.api-v1+json
。基础URL为:
https://dev.to/api
错误信息会以JSON格式返回,包含
error
/
status
字段——请直接展示这些信息。
401
表示API密钥无效→用户必须重新连接DEV连接器。
请始终先确认密钥有效性并获取账户信息:
bash
curl -sS -H "api-key: $DEVTO_API_KEY" -H "Accept: application/vnd.forem.api-v1+json" \
  "https://dev.to/api/users/me" | jq '{username, name}'

Publish an article

发布文章

Confirm with the user before publishing publicly. Default to a draft (
published:false
) unless they explicitly say publish/now.
bash
TITLE="My title"
BODY_MD="$(cat article.md)"   # full Markdown body
jq -n --arg t "$TITLE" --arg b "$BODY_MD" \
  '{article:{title:$t, body_markdown:$b, published:false, tags:["ai","webdev"]}}' \
| curl -sS -X POST "https://dev.to/api/articles" \
    -H "api-key: $DEVTO_API_KEY" \
    -H "Accept: application/vnd.forem.api-v1+json" \
    -H "Content-Type: application/json" \
    -d @- \
| jq '{id, url, published}'
To publish a draft later (or edit),
PUT /api/articles/{id}
with the same shape (set
published:true
). Front-matter inside
body_markdown
(a
---
block) can also carry
title
,
tags
,
series
,
canonical_url
,
cover_image
.
  • Canonical URL: when cross-posting, set
    "canonical_url":"https://your-blog/original"
    so DEV points SEO back to the source — important for the article→video / cross-publishing flow.
公开发布前请与用户确认。 默认设置为草稿(
published:false
),除非用户明确要求立即发布。
bash
TITLE="My title"
BODY_MD="$(cat article.md)"   # full Markdown body
jq -n --arg t "$TITLE" --arg b "$BODY_MD" \
  '{article:{title:$t, body_markdown:$b, published:false, tags:["ai","webdev"]}}' \
| curl -sS -X POST "https://dev.to/api/articles" \
    -H "api-key: $DEVTO_API_KEY" \
    -H "Accept: application/vnd.forem.api-v1+json" \
    -H "Content-Type: application/json" \
    -d @- \
| jq '{id, url, published}'
如需稍后发布草稿(或编辑文章),请使用
PUT /api/articles/{id}
接口,请求体格式保持一致(设置
published:true
)。
body_markdown
中的前置内容块(
---
包裹的部分)也可以包含
title
tags
series
canonical_url
cover_image
等字段。
  • 规范URL(Canonical URL): 交叉发布时,请设置
    "canonical_url":"https://your-blog/original"
    ,这样DEV会将SEO权重指向原文章来源——这在文章转视频/交叉发布流程中非常重要。

List / inspect my articles

列出/查看我的文章

bash
undefined
bash
undefined

My published + draft articles (paginated; per_page max 1000).

我的已发布+草稿文章(分页;per_page最大值为1000)。

curl -sS -H "api-key: $DEVTO_API_KEY" -H "Accept: application/vnd.forem.api-v1+json"
"https://dev.to/api/articles/me/all?per_page=30"
| jq '.[] | {id, title, published, page_views_count, public_reactions_count, comments_count}'
curl -sS -H "api-key: $DEVTO_API_KEY" -H "Accept: application/vnd.forem.api-v1+json"
"https://dev.to/api/articles/me/all?per_page=30"
| jq '.[] | {id, title, published, page_views_count, public_reactions_count, comments_count}'

A single article's full content + stats.

单篇文章的完整内容+统计数据。

curl -sS -H "api-key: $DEVTO_API_KEY" -H "Accept: application/vnd.forem.api-v1+json"
"https://dev.to/api/articles/ARTICLE_ID" | jq '{title, url, reactions: .public_reactions_count, views: .page_views_count}'
undefined
curl -sS -H "api-key: $DEVTO_API_KEY" -H "Accept: application/vnd.forem.api-v1+json"
"https://dev.to/api/articles/ARTICLE_ID" | jq '{title, url, reactions: .public_reactions_count, views: .page_views_count}'
undefined

Gotchas

注意事项

  • Tags: max 4, lowercase, no spaces (e.g.
    webdev
    ,
    machinelearning
    ).
  • Rate limit: article create/update is throttled (a few per 30s); space out bulk publishes or you'll get
    429
    .
  • body_markdown
    is the source of truth — if you put a
    ---
    front-matter block at the top, its fields override the JSON
    article
    fields.
  • 标签: 最多4个,小写,无空格(例如
    webdev
    machinelearning
    )。
  • 速率限制: 文章创建/更新操作有节流限制(每30秒最多几次);批量发布时请间隔一段时间,否则会收到
    429
    错误。
  • body_markdown
    是信息的唯一来源——如果在顶部添加
    ---
    前置内容块,其中的字段会覆盖JSON中
    article
    的对应字段。

Record the output

记录输出结果

After you successfully publish and obtain the live result URL, call the built-in
publish_artifact
tool ONCE so the user can track this deliverable in My Outputs:
publish_artifact(kind="article", channel="devto", title="<title>", url="<the REAL returned URL>", status="delivered")
Use the real returned URL — never fabricate one. Call it once per published item, only after delivery is confirmed; skip it (or use
status="failed"
) if publishing failed. See
_shared/artifacts.md
.
成功发布并获取到实际结果URL后,请调用内置的
publish_artifact
工具一次,以便用户在「我的输出」中跟踪此交付成果:
publish_artifact(kind="article", channel="devto", title="<title>", url="<the REAL returned URL>", status="delivered")
请使用实际返回的URL——切勿伪造。每个已发布的内容仅调用一次,且仅在确认交付成功后调用;如果发布失败,请跳过调用(或设置
status="failed"
)。详情请查看
_shared/artifacts.md