文章 API
通过 RESTful API 以编程方式创建和管理博客文章
概述
文章 API 允许您使用 API 密钥认证,以编程方式创建、发布和管理博客文章。您可以创建富文本内容的文章、AI 生成的内容,并管理可见性设置。
调试器
您可以在文章 API 调试器中测试 API 接口。
身份验证
所有接口都需要 API 密钥认证。您可以通过以下两种方式使用 API 密钥:
-
Authorization 请求头(推荐):
Authorization: Bearer YOUR_API_KEY -
查询参数:
?key=YOUR_API_KEY
速率限制
速率限制根据您的订阅套餐配置:
- 免费版: 每小时 200 次请求
- Pro 版: 每小时 2000 次请求
- 终身版: 每小时 2000 次请求
接口
创建文章
POST /api/v1/posts创建新的博客文章,可选择使用 AI 生成内容。
请求体
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
title | string | 是 | 文章标题(最多 200 字符) |
content | string | 是 | 文章内容(支持 Markdown) |
summary | string | 否 | 文章摘要 |
coverImage | string | 否 | 封面图片 URL |
tags | string[] | 否 | 标签数组 |
visible | boolean | 否 | 可见性(默认:true) |
published | boolean | 否 | 发布状态(默认:true) |
useAI | boolean | 否 | 使用 AI 增强内容(默认:false) |
aiModel | string | 否 | 用于内容生成的 AI 模型 |
slug | string | 否 | 自定义 URL 别名 |
请求示例
curl -X POST "https://like.do/api/v1/posts" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"title": "LikeDo API 入门指南",
"content": "在这篇文章中,我们将探讨如何使用 LikeDo API...",
"summary": "了解如何将 LikeDo API 集成到您的应用程序中",
"tags": ["教程", "api", "文档"],
"visible": true,
"published": true
}'成功响应 (201 Created)
{
"success": true,
"data": {
"id": "post123",
"title": "LikeDo API 入门指南",
"slug": "likedo-api-getting-started",
"content": "在这篇文章中,我们将探讨如何使用 LikeDo API...",
"summary": "了解如何将 LikeDo API 集成到您的应用程序中",
"coverImage": null,
"tags": ["教程", "api", "文档"],
"visible": true,
"published": true,
"views": 0,
"createdAt": "2026-01-04T12:00:00.000Z",
"updatedAt": "2026-01-04T12:00:00.000Z",
"postUrl": "https://like.do/blog/likedo-api-getting-started"
}
}获取文章列表
GET /api/v1/posts检索分页的博客文章列表,支持筛选和排序选项。
查询参数
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
page | number | 1 | 页码(最小值:1) |
limit | number | 10 | 每页数量(1-100) |
search | string | - | 在标题、内容、摘要中搜索 |
sortBy | string | createdAt | 排序字段:title、createdAt、views、updatedAt |
sortOrder | string | desc | 排序顺序:asc 或 desc |
tags | string | - | 按标签筛选(多个标签用逗号分隔) |
published | boolean | - | 按发布状态筛选 |
visible | boolean | - | 按可见性筛选 |
请求示例
curl -X GET "https://like.do/api/v1/posts?page=1&limit=10&published=true" \
-H "Authorization: Bearer YOUR_API_KEY"成功响应 (200 OK)
{
"success": true,
"data": {
"items": [
{
"id": "post123",
"title": "LikeDo API 入门指南",
"slug": "likedo-api-getting-started",
"summary": "了解如何将 LikeDo API 集成到您的应用程序中",
"coverImage": null,
"tags": ["教程", "api", "文档"],
"views": 42,
"published": true,
"visible": true,
"createdAt": "2026-01-04T12:00:00.000Z",
"updatedAt": "2026-01-04T12:00:00.000Z"
}
],
"pagination": {
"page": 1,
"limit": 10,
"total": 1,
"totalPages": 1,
"hasNext": false,
"hasPrevious": false
}
}
}通过 ID 获取文章
GET /api/v1/posts/:id通过 ID 检索特定文章的详细信息。
请求示例
curl -X GET "https://like.do/api/v1/posts/post123" \
-H "Authorization: Bearer YOUR_API_KEY"成功响应 (200 OK)
{
"success": true,
"data": {
"id": "post123",
"title": "LikeDo API 入门指南",
"slug": "likedo-api-getting-started",
"content": "在这篇文章中,我们将探讨如何使用 LikeDo API...",
"summary": "了解如何将 LikeDo API 集成到您的应用程序中",
"coverImage": null,
"tags": ["教程", "api", "文档"],
"visible": true,
"published": true,
"views": 42,
"createdAt": "2026-01-04T12:00:00.000Z",
"updatedAt": "2026-01-04T12:00:00.000Z",
"author": {
"id": "user123",
"name": "张三"
}
}
}更新文章
PUT /api/v1/posts/:id更新现有博客文章。只有文章所有者可以更新文章。
请求体
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
title | string | 否 | 更新的文章标题 |
content | string | 否 | 更新的文章内容 |
summary | string | 否 | 更新的摘要 |
coverImage | string | 否 | 更新的封面图片 URL |
tags | string[] | 否 | 更新的标签 |
visible | boolean | 否 | 更新的可见性 |
published | boolean | 否 | 更新的发布状态 |
请求示例
curl -X PUT "https://like.do/api/v1/posts/post123" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"title": "LikeDo API 入门指南(已更新)",
"published": true
}'成功响应 (200 OK)
{
"success": true,
"data": {
"id": "post123",
"title": "LikeDo API 入门指南(已更新)",
"slug": "likedo-api-getting-started",
"content": "在这篇文章中,我们将探讨如何使用 LikeDo API...",
"summary": "了解如何将 LikeDo API 集成到您的应用程序中",
"published": true,
"updatedAt": "2026-01-04T12:30:00.000Z"
}
}删除文章
DELETE /api/v1/posts/:id删除博客文章。只有文章所有者可以删除文章。
请求示例
curl -X DELETE "https://like.do/api/v1/posts/post123" \
-H "Authorization: Bearer YOUR_API_KEY"成功响应 (200 OK)
{
"success": true,
"message": "文章删除成功"
}错误响应
所有错误响应遵循一致的格式:
{
"success": false,
"error": "描述错误的消息"
}常见错误代码
| 状态码 | 描述 |
|---|---|
| 400 | 错误请求 - 无效的请求数据或缺少必填字段 |
| 401 | 未授权 - API 密钥缺失或无效 |
| 403 | 禁止访问 - API 密钥有效但您不拥有此资源 |
| 404 | 未找到 - 文章不存在 |
| 409 | 冲突 - 文章别名已存在 |
| 429 | 请求过多 - 超出速率限制 |
| 500 | 服务器内部错误 - 服务器端出现问题 |
AI 驱动的内容生成
当使用 useAI: true 创建文章时,API 将使用 AI 来增强您的内容:
- 标题增强: 生成吸引人的标题
- 摘要生成: 自动生成文章摘要
- 内容增强: 改善可读性和结构
- SEO 优化: 添加 SEO 友好的元数据
使用 AI 的示例:
curl -X POST "https://like.do/api/v1/posts" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"title": "API 集成指南",
"content": "关于 API 集成的基本内容...",
"useAI": true,
"aiModel": "gpt-4"
}'最佳实践
- 使用有意义的别名: 创建 SEO 友好且描述性的自定义别名
- 优化图片: 使用优化的图片作为封面图片以提高页面加载速度
- 一致的标签: 使用一致的标签以更好地组织内容
- 先保存草稿: 在发布之前将文章创建为草稿(
published: false) - 批量操作: 分散文章创建请求以避免达到速率限制
- 缓存响应: 在适当的情况下缓存文章数据以减少 API 调用
代码示例
JavaScript (Node.js)
const fetch = require('node-fetch');
async function createPost(apiKey, postData) {
const response = await fetch('https://like.do/api/v1/posts', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(postData),
});
const result = await response.json();
if (!response.ok) {
throw new Error(result.error || '创建文章失败');
}
return result.data;
}
// 使用示例
const post = await createPost(process.env.API_KEY, {
title: '我的博客文章',
content: '文章内容在这里...',
tags: ['教程', 'api'],
published: true,
});
console.log('文章已创建:', post.postUrl);Python
import requests
import os
def create_post(api_key, post_data):
response = requests.post(
'https://like.do/api/v1/posts',
headers={
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json',
},
json=post_data
)
result = response.json()
if not response.ok:
raise Exception(result.get('error', '创建文章失败'))
return result['data']
# 使用示例
post = create_post(os.getenv('API_KEY'), {
'title': '我的博客文章',
'content': '文章内容在这里...',
'tags': ['教程', 'api'],
'published': True,
})
print('文章已创建:', post['postUrl'])技术支持
需要文章 API 帮助?
LikeDo文档