MyBlog API 接口文档

项目文档

概述

MyBlog 提供完整的 RESTful API,所有写操作(POST/PUT/DELETE)需要 JWT Token 认证,读操作(GET)无需认证。

Base URL: https://your-domain.vercel.app

认证

登录获取 Token

POST /api/auth/login
Content-Type: application/json

{"password": "your-password"}

# 响应
{"token": "eyJhbG...", "message": "登录成功"}

使用 Token

在请求头中添加:

Authorization: Bearer eyJhbG...

检查认证状态

GET /api/auth/check
Authorization: Bearer eyJhbG...

# 响应
{"authenticated": true}

登出

POST /api/auth/logout

博客 API

获取博客列表

GET /api/posts

# 查询参数
?page=1          # 页码(默认 1)
&limit=10        # 每页条数(默认 10)
&status=published # 状态筛选:published | draft
&categoryId=xxx  # 按分类 ID 筛选
&tag=标签名        # 按标签名筛选
&search=关键词     # 全文搜索

获取博客详情

GET /api/posts/:id

# :id 可以是文章 ID 或 slug
GET /api/posts/hello-world
GET /api/posts/cmmj0ogac000551mk...

创建博客

POST /api/posts
Authorization: Bearer TOKEN
Content-Type: application/json

{
  "title": "文章标题",       # 必填
  "slug": "article-slug",   # 必填,URL 路径
  "content": "<p>HTML内容</p>", # 必填
  "summary": "文章摘要",      # 可选
  "coverImage": "https://...", # 可选,封面图 URL
  "status": "published",     # 可选,默认 draft
  "categoryId": "xxx",       # 可选
  "tagNames": ["标签1", "标签2"] # 可选,标签不存在会自动创建
}

更新博客

PUT /api/posts/:id
Authorization: Bearer TOKEN
Content-Type: application/json

{
  "title": "新标题",
  "status": "published"
  // 只传需要更新的字段
}

删除博客

DELETE /api/posts/:id
Authorization: Bearer TOKEN

笔记 API

获取笔记列表

GET /api/notes

# 查询参数(同博客,额外支持)
&isPublic=true   # 筛选公开/私有笔记

创建笔记

POST /api/notes
Authorization: Bearer TOKEN
Content-Type: application/json

{
  "title": "笔记标题",       # 必填
  "content": "<p>内容</p>",   # 必填
  "summary": "摘要",          # 可选
  "status": "published",     # 可选
  "isPublic": true,          # 可选,默认 false
  "categoryId": "xxx",       # 可选
  "tagNames": ["标签"]        # 可选
}

更新 / 删除笔记

PUT /api/notes/:id     # 同博客
DELETE /api/notes/:id  # 同博客

分类 API

获取分类列表

GET /api/categories
GET /api/categories?type=post  # 只获取博客分类
GET /api/categories?type=note  # 只获取笔记分类

创建分类

POST /api/categories
Authorization: Bearer TOKEN

{"name": "分类名", "type": "post"}  # type: post | note

更新 / 删除分类

PUT /api/categories/:id
DELETE /api/categories/:id

标签 API

GET /api/tags              # 获取所有标签
POST /api/tags             # 创建:{"name": "标签名"}
PUT /api/tags/:id          # 更新:{"name": "新名"}
DELETE /api/tags/:id       # 删除

图片 API

上传图片

POST /api/upload
Authorization: Bearer TOKEN
Content-Type: multipart/form-data

# 字段名: file
curl -X POST /api/upload \
  -H "Authorization: Bearer TOKEN" \
  -F "file=@photo.jpg"

# 响应
{"id": "xxx", "url": "https://...blob...", "name": "photo.jpg", "size": 12345}

获取图片列表

GET /api/images?page=1&limit=20

删除图片

DELETE /api/images/:id
Authorization: Bearer TOKEN

快速上手示例

# 1. 登录
TOKEN=$(curl -s -X POST https://your-domain/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{"password":"your-password"}' | jq -r '.token')

# 2. 发布一篇博客
curl -X POST https://your-domain/api/posts \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "title": "我的文章",
    "slug": "my-article",
    "content": "<p>Hello World</p>",
    "status": "published",
    "tagNames": ["技术"]
  }'

# 3. 上传图片
curl -X POST https://your-domain/api/upload \
  -H "Authorization: Bearer $TOKEN" \
  -F "file=@photo.jpg"

错误响应

状态码含义
400请求参数错误
401未授权(需要登录)
404资源不存在
500服务器内部错误