dev.to

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Dev.to Publisher

Dev.to 发布器

Use the Dev.to API via direct
curl
calls to publish and manage articles.
Official docs:
https://developers.forem.com/api/v1

通过直接调用
curl
来使用Dev.to API,以发布和管理文章
官方文档:
https://developers.forem.com/api/v1

When to Use

适用场景

Use this skill when you need to:
  • Publish articles to Dev.to
  • Update existing articles
  • Manage drafts
  • Fetch your published articles

当你需要以下操作时使用本技能:
  • 在Dev.to上发布文章
  • 更新已有文章
  • 管理草稿
  • 获取已发布的文章

Prerequisites

前置条件

  1. Go to https://dev.to/settings/extensions
  2. Scroll to "DEV Community API Keys"
  3. Generate a new API key
bash
export DEVTO_API_KEY="your-api-key"

Important: When using
$VAR
in a command that pipes to another command, wrap the command containing
$VAR
in
bash -c '...'
. Due to a Claude Code bug, environment variables are silently cleared when pipes are used directly.
bash
bash -c 'curl -s "https://api.example.com" -H "Authorization: Bearer $API_KEY"'
  1. 访问 https://dev.to/settings/extensions
  2. 滚动到“DEV Community API Keys”区域
  3. 生成新的API密钥
bash
export DEVTO_API_KEY="your-api-key"

重要提示: 当在包含管道的命令中使用
$VAR
时,请将包含
$VAR
的命令用
bash -c '...'
包裹。由于Claude Code的一个bug,直接使用管道时环境变量会被静默清除。
bash
bash -c 'curl -s "https://api.example.com" -H "Authorization: Bearer $API_KEY"'

How to Use

使用方法

All examples below assume you have
DEVTO_API_KEY
set.
以下所有示例均假设你已设置好
DEVTO_API_KEY
环境变量。

1. Publish an Article

1. 发布文章

Write to
/tmp/devto_request.json
:
json
{
  "article": {
    "title": "My Awesome Article",
    "body_markdown": "## Introduction\n\nThis is my article content.\n\n## Conclusion\n\nThanks for reading!",
    "published": false,
    "tags": ["javascript", "webdev"]
  }
}
Then run:
bash
bash -c 'curl -s -X POST "https://dev.to/api/articles" -H "api-key: ${DEVTO_API_KEY}" -H "Content-Type: application/json" -d @/tmp/devto_request.json' | jq '{id, url, published}'
Response:
json
{
  "id": 123456,
  "url": "https://dev.to/username/my-awesome-article-abc",
  "published": false
}
编写内容到
/tmp/devto_request.json
json
{
  "article": {
    "title": "My Awesome Article",
    "body_markdown": "## Introduction\n\nThis is my article content.\n\n## Conclusion\n\nThanks for reading!",
    "published": false,
    "tags": ["javascript", "webdev"]
  }
}
然后运行:
bash
bash -c 'curl -s -X POST "https://dev.to/api/articles" -H "api-key: ${DEVTO_API_KEY}" -H "Content-Type: application/json" -d @/tmp/devto_request.json' | jq '{id, url, published}'
响应:
json
{
  "id": 123456,
  "url": "https://dev.to/username/my-awesome-article-abc",
  "published": false
}

2. Publish Immediately

2. 立即发布

Set
published: true
to publish right away:
Write to
/tmp/devto_request.json
:
json
{
  "article": {
    "title": "Published Article",
    "body_markdown": "Content here...",
    "published": true,
    "tags": ["tutorial"]
  }
}
Then run:
bash
bash -c 'curl -s -X POST "https://dev.to/api/articles" -H "api-key: ${DEVTO_API_KEY}" -H "Content-Type: application/json" -d @/tmp/devto_request.json' | jq '{id, url, published}'
设置
published: true
即可立即发布:
编写内容到
/tmp/devto_request.json
json
{
  "article": {
    "title": "Published Article",
    "body_markdown": "Content here...",
    "published": true,
    "tags": ["tutorial"]
  }
}
然后运行:
bash
bash -c 'curl -s -X POST "https://dev.to/api/articles" -H "api-key: ${DEVTO_API_KEY}" -H "Content-Type: application/json" -d @/tmp/devto_request.json' | jq '{id, url, published}'

3. Publish with Cover Image

3. 带封面图发布

Write to
/tmp/devto_request.json
:
json
{
  "article": {
    "title": "Article with Cover",
    "body_markdown": "Content here...",
    "published": true,
    "tags": ["webdev", "tutorial"],
    "main_image": "https://example.com/cover.png"
  }
}
Then run:
bash
bash -c 'curl -s -X POST "https://dev.to/api/articles" -H "api-key: ${DEVTO_API_KEY}" -H "Content-Type: application/json" -d @/tmp/devto_request.json' | jq '{id, url}'
编写内容到
/tmp/devto_request.json
json
{
  "article": {
    "title": "Article with Cover",
    "body_markdown": "Content here...",
    "published": true,
    "tags": ["webdev", "tutorial"],
    "main_image": "https://example.com/cover.png"
  }
}
然后运行:
bash
bash -c 'curl -s -X POST "https://dev.to/api/articles" -H "api-key: ${DEVTO_API_KEY}" -H "Content-Type: application/json" -d @/tmp/devto_request.json' | jq '{id, url}'

4. Publish from Markdown File

4. 从Markdown文件发布

To publish from a markdown file, first convert it to JSON:
bash
cat article.md | jq -Rs '.' > /tmp/content.json
Write to
/tmp/devto_request.json
:
json
{
  "article": {
    "title": "My Article Title",
    "body_markdown": "Your article content here...",
    "published": false,
    "tags": ["programming"]
  }
}
Then run:
bash
bash -c 'curl -s -X POST "https://dev.to/api/articles" -H "api-key: ${DEVTO_API_KEY}" -H "Content-Type: application/json" -d @/tmp/devto_request.json' | jq '{id, url, published}'

要从Markdown文件发布,首先将其转换为JSON:
bash
cat article.md | jq -Rs '.' > /tmp/content.json
编写内容到
/tmp/devto_request.json
json
{
  "article": {
    "title": "My Article Title",
    "body_markdown": "Your article content here...",
    "published": false,
    "tags": ["programming"]
  }
}
然后运行:
bash
bash -c 'curl -s -X POST "https://dev.to/api/articles" -H "api-key: ${DEVTO_API_KEY}" -H "Content-Type: application/json" -d @/tmp/devto_request.json' | jq '{id, url, published}'

Managing Articles

文章管理

5. Get Your Articles

5. 获取你的文章

bash
bash -c 'curl -s "https://dev.to/api/articles/me?per_page=10" -H "api-key: ${DEVTO_API_KEY}"' | jq '.[] | {id, title, published, url}'
bash
bash -c 'curl -s "https://dev.to/api/articles/me?per_page=10" -H "api-key: ${DEVTO_API_KEY}"' | jq '.[] | {id, title, published, url}'

6. Get Published Articles Only

6. 仅获取已发布文章

bash
bash -c 'curl -s "https://dev.to/api/articles/me/published?per_page=10" -H "api-key: ${DEVTO_API_KEY}"' | jq '.[] | {id, title, url}'
bash
bash -c 'curl -s "https://dev.to/api/articles/me/published?per_page=10" -H "api-key: ${DEVTO_API_KEY}"' | jq '.[] | {id, title, url}'

7. Get Unpublished (Drafts)

7. 获取未发布文章(草稿)

bash
bash -c 'curl -s "https://dev.to/api/articles/me/unpublished" -H "api-key: ${DEVTO_API_KEY}"' | jq '.[] | {id, title}'
bash
bash -c 'curl -s "https://dev.to/api/articles/me/unpublished" -H "api-key: ${DEVTO_API_KEY}"' | jq '.[] | {id, title}'

8. Get Single Article

8. 获取单篇文章

Replace
<your-article-id>
with an actual article ID from the "List My Articles" response (example 5) or from the
id
field in the create article response (example 1).
bash
bash -c 'curl -s "https://dev.to/api/articles/<your-article-id>" -H "api-key: ${DEVTO_API_KEY}"' | jq '{id, title, url, published}'
<your-article-id>
替换为“列出我的文章”响应(示例5)或创建文章响应(示例1)中的实际文章ID。
bash
bash -c 'curl -s "https://dev.to/api/articles/<your-article-id>" -H "api-key: ${DEVTO_API_KEY}"' | jq '{id, title, url, published}'

9. Update an Article

9. 更新文章

Replace
<your-article-id>
with an actual article ID from the "List My Articles" response (example 5) or from the
id
field in the create article response (example 1).
Write to
/tmp/devto_request.json
:
json
{
  "article": {
    "title": "Updated Title",
    "body_markdown": "Updated content..."
  }
}
Then run:
bash
bash -c 'curl -s -X PUT "https://dev.to/api/articles/<your-article-id>" -H "api-key: ${DEVTO_API_KEY}" -H "Content-Type: application/json" -d @/tmp/devto_request.json' | jq '{id, url}'
<your-article-id>
替换为“列出我的文章”响应(示例5)或创建文章响应(示例1)中的实际文章ID。
编写内容到
/tmp/devto_request.json
json
{
  "article": {
    "title": "Updated Title",
    "body_markdown": "Updated content..."
  }
}
然后运行:
bash
bash -c 'curl -s -X PUT "https://dev.to/api/articles/<your-article-id>" -H "api-key: ${DEVTO_API_KEY}" -H "Content-Type: application/json" -d @/tmp/devto_request.json' | jq '{id, url}'

10. Publish a Draft

10. 发布草稿

Replace
<your-article-id>
with an actual article ID from the "List My Articles" response (example 5) or from the
id
field in the create article response (example 1).
Write to
/tmp/devto_request.json
:
json
{
  "article": {
    "published": true
  }
}
Then run:
bash
bash -c 'curl -s -X PUT "https://dev.to/api/articles/<your-article-id>" -H "api-key: ${DEVTO_API_KEY}" -H "Content-Type: application/json" -d @/tmp/devto_request.json' | jq '{id, url, published}'

<your-article-id>
替换为“列出我的文章”响应(示例5)或创建文章响应(示例1)中的实际文章ID。
编写内容到
/tmp/devto_request.json
json
{
  "article": {
    "published": true
  }
}
然后运行:
bash
bash -c 'curl -s -X PUT "https://dev.to/api/articles/<your-article-id>" -H "api-key: ${DEVTO_API_KEY}" -H "Content-Type: application/json" -d @/tmp/devto_request.json' | jq '{id, url, published}'

Article Parameters

文章参数

ParameterTypeDescription
title
stringArticle title (required)
body_markdown
stringContent in Markdown
published
boolean
true
to publish,
false
for draft
tags
arrayUp to 4 tags (lowercase, no spaces)
main_image
stringCover image URL
canonical_url
stringOriginal article URL (for cross-posts)
series
stringSeries name to group articles

参数类型描述
title
字符串文章标题(必填)
body_markdown
字符串Markdown格式的内容
published
布尔值
true
表示发布,
false
表示草稿
tags
数组最多4个标签(小写,无空格)
main_image
字符串封面图片URL
canonical_url
字符串原文URL(用于交叉发布)
series
字符串用于分组文章的系列名称

Examples

示例

Tech Tutorial

技术教程

Write to
/tmp/devto_request.json
:
json
{
  "article": {
    "title": "Getting Started with Docker",
    "body_markdown": "## What is Docker?\n\nDocker is a platform for developing...\n\n## Installation\n\n```bash\nbrew install docker\n```\n\n## Your First Container\n\n```bash\ndocker run hello-world\n```",
    "published": true,
    "tags": ["docker", "devops", "tutorial", "beginners"]
  }
}
Then run:
bash
bash -c 'curl -s -X POST "https://dev.to/api/articles" -H "api-key: ${DEVTO_API_KEY}" -H "Content-Type: application/json" -d @/tmp/devto_request.json' | jq '{url}'
编写内容到
/tmp/devto_request.json
json
{
  "article": {
    "title": "Getting Started with Docker",
    "body_markdown": "## What is Docker?\n\nDocker is a platform for developing...\n\n## Installation\n\n```bash\nbrew install docker\n```\n\n## Your First Container\n\n```bash\ndocker run hello-world\n```",
    "published": true,
    "tags": ["docker", "devops", "tutorial", "beginners"]
  }
}
然后运行:
bash
bash -c 'curl -s -X POST "https://dev.to/api/articles" -H "api-key: ${DEVTO_API_KEY}" -H "Content-Type: application/json" -d @/tmp/devto_request.json' | jq '{url}'

Cross-post from Blog

从博客交叉发布

Write to
/tmp/devto_request.json
:
json
{
  "article": {
    "title": "My Blog Post",
    "body_markdown": "Content from my blog...",
    "published": true,
    "canonical_url": "https://myblog.com/original-post",
    "tags": ["webdev"]
  }
}
Then run:
bash
bash -c 'curl -s -X POST "https://dev.to/api/articles" -H "api-key: ${DEVTO_API_KEY}" -H "Content-Type: application/json" -d @/tmp/devto_request.json' | jq '{url}'
编写内容到
/tmp/devto_request.json
json
{
  "article": {
    "title": "My Blog Post",
    "body_markdown": "Content from my blog...",
    "published": true,
    "canonical_url": "https://myblog.com/original-post",
    "tags": ["webdev"]
  }
}
然后运行:
bash
bash -c 'curl -s -X POST "https://dev.to/api/articles" -H "api-key: ${DEVTO_API_KEY}" -H "Content-Type: application/json" -d @/tmp/devto_request.json' | jq '{url}'

Article in a Series

系列文章

Write to
/tmp/devto_request.json
:
json
{
  "article": {
    "title": "React Hooks - Part 1: useState",
    "body_markdown": "First part of the series...",
    "published": true,
    "series": "React Hooks Deep Dive",
    "tags": ["react", "javascript", "hooks"]
  }
}
Then run:
bash
bash -c 'curl -s -X POST "https://dev.to/api/articles" -H "api-key: ${DEVTO_API_KEY}" -H "Content-Type: application/json" -d @/tmp/devto_request.json' | jq '{url}'

编写内容到
/tmp/devto_request.json
json
{
  "article": {
    "title": "React Hooks - Part 1: useState",
    "body_markdown": "First part of the series...",
    "published": true,
    "series": "React Hooks Deep Dive",
    "tags": ["react", "javascript", "hooks"]
  }
}
然后运行:
bash
bash -c 'curl -s -X POST "https://dev.to/api/articles" -H "api-key: ${DEVTO_API_KEY}" -H "Content-Type: application/json" -d @/tmp/devto_request.json' | jq '{url}'

Guidelines

注意事项

  1. Max 4 tags: Dev.to limits articles to 4 tags
  2. Lowercase tags: Tags should be lowercase without spaces
  3. Escape markdown: Use
    jq -Rs
    to properly escape markdown content
  4. Cover images: Must be URLs, not local files
  5. Draft first: Set
    published: false
    to review before publishing
  6. Check response: Always verify the returned URL
  1. 最多4个标签:Dev.to限制文章最多添加4个标签
  2. 标签小写:标签需为小写且无空格
  3. 转义Markdown:使用
    jq -Rs
    来正确转义Markdown内容
  4. 封面图片:必须是URL,不能是本地文件
  5. 先存草稿:设置
    published: false
    以便发布前审核
  6. 检查响应:始终验证返回的URL