MyBlog 提供一套 RESTful API,所有接口基于 /api 路径。GET 请求无需认证,写操作(POST/PUT/DELETE)需要在请求头中携带 JWT Token。
Base URL: https://blog.1752667613.xyz/api
POST /api/auth/login
请求体:
{
"password": "your-password"
}
成功响应 (200):
{
"token": "eyJhbGciOiJIUzI1NiIs...",
"message": "登录成功"
}
失败响应 (401):
{
"error": "密码错误"
}
所有写操作需要在请求头中添加:
Authorization: Bearer <token>
Token 同时也会通过 HttpOnly Cookie 设置,有效期 7 天。
GET /api/auth/check
成功响应: { "authenticated": true }
失败响应: { "authenticated": false }
GET /api/posts?page=1&limit=10&status=published&search=关键词&categoryId=xxx&tag=标签名
响应:
{
"posts": [...],
"pagination": {
"page": 1,
"limit": 10,
"total": 25,
"totalPages": 3
}
}
GET /api/posts/:id
支持通过 ID 或 slug 查询。
响应: 完整文章对象(含 category、tags)
POST /api/posts
Authorization: Bearer <token>
请求体:
{
"title": "文章标题", // 必填
"slug": "article-slug", // 必填,URL 路径
"content": "<p>HTML 内容</p>", // 必填
"summary": "文章摘要",
"coverImage": "https://...",
"status": "published", // published | draft
"categoryId": "分类ID",
"tagNames": ["标签1", "标签2"]
}
成功响应 (201): 创建的文章对象
PUT /api/posts/:id
Authorization: Bearer <token>
请求体: 同创建,所有字段均为可选(只传需要更新的)
成功响应 (200): 更新后的文章对象
DELETE /api/posts/:id
Authorization: Bearer <token>
成功响应: { "message": "删除成功" }
GET /api/notes?page=1&limit=10&status=published&isPublic=true&search=关键词
响应:
{
"notes": [...],
"pagination": { ... }
}
GET /api/notes/:id
POST /api/notes
Authorization: Bearer <token>
请求体:
{
"title": "笔记标题", // 必填
"content": "<p>内容</p>", // 必填
"summary": "笔记摘要",
"status": "published",
"isPublic": true, // 是否公开
"categoryId": "分类ID",
"tagNames": ["标签1"]
}
PUT /api/notes/:id
Authorization: Bearer <token>
请求体: 同创建,所有字段可选
DELETE /api/notes/:id
Authorization: Bearer <token>
GET /api/categories?type=post
type 参数可选: post | note
响应: 分类数组,含 _count 统计
POST /api/categories
Authorization: Bearer <token>
{ "name": "分类名", "type": "post" }
PUT /api/categories/:id
Authorization: Bearer <token>
{ "name": "新名称" }
DELETE /api/categories/:id
Authorization: Bearer <token>
GET /api/tags
响应: 标签数组,含关联文章/笔记数量
POST /api/tags { "name": "标签名" }
PUT /api/tags/:id { "name": "新名称" }
DELETE /api/tags/:id
POST /api/upload
Authorization: Bearer <token>
Content-Type: multipart/form-data
FormData:
file: (二进制文件)
成功响应:
{
"url": "https://xxx.public.blob.vercel-storage.com/...",
"name": "filename.jpg",
"size": 123456
}
GET /api/images?limit=100
响应: { "images": [...], "pagination": {...} }
DELETE /api/images/:id
Authorization: Bearer <token>
同时删除 Vercel Blob 存储和数据库记录
POST /api/revalidate?secret=YOUR_SECRET
手动刷新全站 ISR 缓存。需要在 Vercel 环境变量中设置 REVALIDATE_SECRET。
成功响应: { "revalidated": true, "now": 1710000000000 }
所有错误响应遵循统一格式:
{
"error": "错误描述信息"
}
常见状态码:400(参数错误)、401(未授权)、404(不存在)、500(服务端错误)