dev.to
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDev.to Publisher
Dev.to 发布器
Use the Dev.to API via direct calls to publish and manage articles.
curlOfficial docs:https://developers.forem.com/api/v1
通过直接调用来使用Dev.to API,以发布和管理文章。
curl官方文档: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
前置条件
- Go to https://dev.to/settings/extensions
- Scroll to "DEV Community API Keys"
- Generate a new API key
bash
export DEVTO_API_KEY="your-api-key"Important: When usingin a command that pipes to another command, wrap the command containing$VARin$VAR. Due to a Claude Code bug, environment variables are silently cleared when pipes are used directly.bash -c '...'bashbash -c 'curl -s "https://api.example.com" -H "Authorization: Bearer $API_KEY"'
- 访问 https://dev.to/settings/extensions
- 滚动到“DEV Community API Keys”区域
- 生成新的API密钥
bash
export DEVTO_API_KEY="your-api-key"重要提示: 当在包含管道的命令中使用时,请将包含$VAR的命令用$VAR包裹。由于Claude Code的一个bug,直接使用管道时环境变量会被静默清除。bash -c '...'bashbash -c 'curl -s "https://api.example.com" -H "Authorization: Bearer $API_KEY"'
How to Use
使用方法
All examples below assume you have set.
DEVTO_API_KEY以下所有示例均假设你已设置好环境变量。
DEVTO_API_KEY1. Publish an Article
1. 发布文章
Write to :
/tmp/devto_request.jsonjson
{
"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.jsonjson
{
"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 to publish right away:
published: trueWrite to :
/tmp/devto_request.jsonjson
{
"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.jsonjson
{
"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.jsonjson
{
"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.jsonjson
{
"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.jsonWrite to :
/tmp/devto_request.jsonjson
{
"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.jsonjson
{
"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 with an actual article ID from the "List My Articles" response (example 5) or from the field in the create article response (example 1).
<your-article-id>idbash
bash -c 'curl -s "https://dev.to/api/articles/<your-article-id>" -H "api-key: ${DEVTO_API_KEY}"' | jq '{id, title, url, published}'将替换为“列出我的文章”响应(示例5)或创建文章响应(示例1)中的实际文章ID。
<your-article-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 with an actual article ID from the "List My Articles" response (example 5) or from the field in the create article response (example 1).
<your-article-id>idWrite to :
/tmp/devto_request.jsonjson
{
"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}'将替换为“列出我的文章”响应(示例5)或创建文章响应(示例1)中的实际文章ID。
<your-article-id>编写内容到:
/tmp/devto_request.jsonjson
{
"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 with an actual article ID from the "List My Articles" response (example 5) or from the field in the create article response (example 1).
<your-article-id>idWrite to :
/tmp/devto_request.jsonjson
{
"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}'将替换为“列出我的文章”响应(示例5)或创建文章响应(示例1)中的实际文章ID。
<your-article-id>编写内容到:
/tmp/devto_request.jsonjson
{
"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
文章参数
| Parameter | Type | Description |
|---|---|---|
| string | Article title (required) |
| string | Content in Markdown |
| boolean | |
| array | Up to 4 tags (lowercase, no spaces) |
| string | Cover image URL |
| string | Original article URL (for cross-posts) |
| string | Series name to group articles |
| 参数 | 类型 | 描述 |
|---|---|---|
| 字符串 | 文章标题(必填) |
| 字符串 | Markdown格式的内容 |
| 布尔值 | |
| 数组 | 最多4个标签(小写,无空格) |
| 字符串 | 封面图片URL |
| 字符串 | 原文URL(用于交叉发布) |
| 字符串 | 用于分组文章的系列名称 |
Examples
示例
Tech Tutorial
技术教程
Write to :
/tmp/devto_request.jsonjson
{
"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.jsonjson
{
"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.jsonjson
{
"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.jsonjson
{
"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.jsonjson
{
"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.jsonjson
{
"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
注意事项
- Max 4 tags: Dev.to limits articles to 4 tags
- Lowercase tags: Tags should be lowercase without spaces
- Escape markdown: Use to properly escape markdown content
jq -Rs - Cover images: Must be URLs, not local files
- Draft first: Set to review before publishing
published: false - Check response: Always verify the returned URL
- 最多4个标签:Dev.to限制文章最多添加4个标签
- 标签小写:标签需为小写且无空格
- 转义Markdown:使用来正确转义Markdown内容
jq -Rs - 封面图片:必须是URL,不能是本地文件
- 先存草稿:设置以便发布前审核
published: false - 检查响应:始终验证返回的URL