# 画个标素材 API
面向公司内部项目的**素材检索服务**:按关键词查图标/海报素材,直接拿到可嵌入的 SVG。
- 线上地址:`https://drawlogo.zhuig.com/api/v1`
- 鉴权:API Key(`X-API-Key` 请求头)
- 数据规模:图标 **371,504** 个 / 224 源;海报 **1,582** 个 / 25 源;技能元数据 130 个
- 特性:支持**中文语义检索**(搜「学校」命中 school / graduation-cap / book…)、按协议过滤商用可用素材、默认排除 `-off`/`-error` 等负面变体
> 本服务只提供**素材与技能元数据**,不提供 AI 生成 LOGO(当前 AI 生成质量不足以对外提供)。
---
## 一、快速开始
```bash
# 1. 搜索(中文或英文)
curl -H "X-API-Key: <你的Key>" \
"https://drawlogo.zhuig.com/api/v1/icons/search?q=学校&limit=5"
# 2. 直接拿 SVG(可直接放进
或 fetch)
curl -H "X-API-Key: <你的Key>" \
"https://drawlogo.zhuig.com/api/v1/icons/lucide/school.svg"
# 3. 查看有哪些源(免鉴权)
curl "https://drawlogo.zhuig.com/api/v1/sources"
```
---
## 二、鉴权
请求头二选一:
```
X-API-Key: dl_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Authorization: Bearer dl_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
```
- Key 由管理员用 `node admin.js create --name "调用方"` 生成,**明文只显示一次**
- 服务端只存 SHA-256 摘要,无法反查明文
- 每个 Key 独立配置:分钟限流、每日配额、启用/禁用
**免鉴权端点**(仅基础信息):`/health`、`/stats`、`/sources`
---
## 三、端点
所有成功响应:
```json
{ "ok": true, "data": ..., "meta": { "total": 1059, "limit": 5, "offset": 0 } }
```
所有失败响应:
```json
{ "ok": false, "error": { "code": "UNAUTHORIZED", "message": "缺少 API Key" } }
```
### 1. 健康检查
```
GET /api/v1/health # 免鉴权
```
```json
{ "ok": true, "data": { "status": "up", "version": "1.0.0", "builtAt": "...", "uptime": 3600 } }
```
### 2. 运行统计
```
GET /api/v1/stats # 免鉴权
```
### 3. 源清单
```
GET /api/v1/sources?kind=icons|posters # 免鉴权
```
返回每个源的 `id / name / nameCn / desc / type / license / commercial / count / workspaces`。
**接入前建议先读这个**,确认目标源协议是否允许商用。
### 4. 图标搜索
```
GET /api/v1/icons/search
```
| 参数 | 说明 | 默认 |
|---|---|---|
| `q` | 关键词(英文名 / 中文分类 / 中文语义) | 空=全部 |
| `source` | 限定源 id,如 `lucide` | 全部 |
| `type` | `icon`(单色)/ `emoji`(多色)/ `brand`(品牌) | 全部 |
| `commercial` | `1`=仅商用可用(排除 BY-NC/BY-SA/BY-ND/GPL) | 不过滤 |
| `dedupe` | `1`=按图标名去重(多源同名只留最优一个) | `0` |
| `include_negative` | `1`=包含 `-off`/`-error` 等负面变体 | `0` |
| `limit` | 1–100 | `20` |
| `offset` | 分页偏移 | `0` |
响应项:
```json
{
"id": "school",
"name": "school",
"categoryCn": "教育学习",
"type": "icon",
"source": "lucide",
"sourceNameCn": "Lucide 图标",
"license": "ISC",
"commercial": true,
"index": 1321
}
```
> `index` 是该素材在源内的序号,用于内部定位;外部调用请用 `source` + `name`。
排序规则:名称精确 > 名称前缀 > 名称包含 > 中文别名精确 > 分类匹配 > 别名包含;同分时主流库(lucide/tabler/heroicons/feather…)优先、短名优先。
### 5. 图标详情 / SVG 直链
```
GET /api/v1/icons/{source}/{name} # JSON(含 svg 原文)
GET /api/v1/icons/{source}/{name}.svg # image/svg+xml
```
SVG 直链返回:
```html
```
- 单色图标(`type=icon`)带 `fill="currentColor"`,颜色由 CSS 的 `color` 控制
- 多色图标(`type=emoji/brand`)保留原色,不加 `fill`
- 建议在 `
` 中用时手动加 `style="color:#1B4F72"`
### 6. 随机图标
```
GET /api/v1/icons/random?count=10&source=lucide
```
适合做占位图、灵感墙、测试数据。
### 7. 海报素材
```
GET /api/v1/posters/search?q=&type=&source=&limit=
GET /api/v1/posters/{source}/{name}
```
`type` 为 `palette`(配色)/ `font`(字体)/ `bg`(背景)/ `decoration`(装饰)。
配色类素材的 `data` 字段内联了色值数组;字体类给出 `file` 路径(相对静态站根目录)。
### 8. 技能元数据
```
GET /api/v1/skills?workspace=logo&step=3&category=color
GET /api/v1/skills/{id}
```
返回技能的 `id / name / category / scene / summary / params / step / kind / workspaces`。
**仅元数据**(可直接当设计知识/评审规则用),不执行 `run`。
---
## 四、中文检索说明
图标名是英文(`graduation-cap`),分类是中文宽类目(`教育学习`)。为了支持「学校」「医院」「餐厅」这类词,服务内置了一张**中文语义别名表**(约 240 个高频词 → 英文词根),构建索引时为每个图标预计算别名。
- 搜「学校」→ school / graduation-cap / book / backpack…
- 搜「医院」→ hospital / ambulance / stethoscope / pill…
- 搜「工厂」→ factory / building-factory…
- 搜「飞机」→ plane / plane-landing / plane-takeoff…
词表在 [build-index.js](./build-index.js) 的 `CN_ALIAS`,新增行业词直接加进去并重建索引即可。
---
## 五、限流与配额
| 项 | 默认 | 说明 |
|---|---|---|
| 分钟限流 | 60 次/分钟/Key | 创建时可调,如 `--rate 120` |
| 每日配额 | 20,000 次/天/Key | 创建时可调,如 `--quota 30000` |
响应头:
```
X-RateLimit-Limit: 120
X-RateLimit-Remaining: 118
```
超限返回 `429`,`Retry-After: 60`。
### 错误码
| HTTP | code | 含义 |
|---|---|---|
| 400 | `BAD_REQUEST` | 参数非法 |
| 401 | `UNAUTHORIZED` | 未带 Key |
| 403 | `INVALID_KEY` | Key 无效或已禁用 |
| 404 | `NOT_FOUND` | 素材/源/技能不存在 |
| 405 | `METHOD_NOT_ALLOWED` | 仅支持 GET |
| 429 | `RATE_LIMITED` | 分钟限流 |
| 429 | `DAILY_QUOTA_EXCEEDED` | 日配额用尽 |
| 500 | `INTERNAL` | 服务内部错误 |
| 503 | `NOT_READY` | 索引未构建 |
---
## 六、接入示例
### JavaScript / 前端
```js
const KEY = '...'; // 注意:放前端会暴露给访客,建议走后端代理
const r = await fetch('/api/v1/icons/search?q=学校&limit=8', {
headers: { 'X-API-Key': KEY }
});
const { data } = await r.json();
data.forEach(ic => {
const img = new Image();
img.src = `/api/v1/icons/${ic.source}/${ic.name}.svg`;
});
```
### Node / 后端
```js
const res = await fetch('https://drawlogo.zhuig.com/api/v1/icons/search?q=hospital&limit=5', {
headers: { 'X-API-Key': process.env.DRAWLOGO_API_KEY }
});
const { data } = await res.json();
```
### Java(Spring RestTemplate)
```java
HttpHeaders h = new HttpHeaders();
h.set("X-API-Key", System.getenv("DRAWLOGO_API_KEY"));
String url = "https://drawlogo.zhuig.com/api/v1/icons/search?q=%E5%AD%A6%E6%A0%A1&limit=5";
ResponseEntity resp = new RestTemplate().exchange(url, HttpMethod.GET, new HttpEntity<>(h), String.class);
```
### Python
```python
import os, requests
r = requests.get(
"https://drawlogo.zhuig.com/api/v1/icons/search",
params={"q": "学校", "limit": 5},
headers={"X-API-Key": os.environ["DRAWLOGO_API_KEY"]},
)
print(r.json()["data"])
```
---
## 七、协议合规(重要)
- 调用 `/api/v1/sources` 可看到每个源的 `license` 与 `commercial`
- 商业项目请加 `commercial=1`,或自行校验 `commercial: true`
- 以下协议**不可商用**:`BY-NC`、`BY-SA`、`BY-ND`、`GPL`
- 品牌类图标(`type=brand`,如 Instagram / 微信 logo)有商标风险,做 LOGO 时避免直接使用
- 建议在产物中标注素材来源与协议(源 `name` + `license`)
---
## 八、运维
### 重建索引(素材库更新后)
```bash
cd /opt/drawlogo-api
DRAWLOGO_ROOT=/opt/drawlogo node --max-old-space-size=1536 build-index.js
sudo systemctl restart drawlogo-api
```
索引产物在 `data/`(约 530 MB),**不纳入 git**。
### Key 管理
**Web 管理后台**(推荐):`https://drawlogo.zhuig.com/api-admin.html`
- 输入管理口令进入(口令 = 服务器 `config.json` 的 `adminToken`)
- 可视化签发 Key:填写**使用者**、**备注**、**每分钟限流**、**每日配额**、**有效期**
- 列表可查看今日用量、最近使用时间,支持**启用/禁用/编辑/删除**
- 明文 Key 只在签发时显示一次,页面提供一键复制
> 管理口令与普通 API Key 完全分离;普通 Key 无法访问管理接口。口令为空时后台接口整体关闭。
**命令行**(服务器 `/opt/drawlogo-api`):
```bash
cd /opt/drawlogo-api
node admin.js create --name "项目A" --rate 120 --quota 30000
node admin.js list
node admin.js disable
node admin.js enable
node admin.js revoke
```
Key 文件 `data/apikeys.json` 权限 600,服务每 5 秒热加载,**改 Key 无需重启**。
### 服务与反向代理
```bash
sudo systemctl status drawlogo-api
sudo journalctl -u drawlogo-api -f
```
- systemd 单元:[deploy/drawlogo-api.service](./deploy/drawlogo-api.service)
- nginx 片段:[deploy/nginx-location.conf](./deploy/nginx-location.conf)(必须 `location ^~ /api/`)
- 幂等写入脚本:`sudo python3 deploy/patch-nginx.py`
### 更新 API 代码
```bash
# 本地
scp api/server.js api/build-index.js api/admin.js ubuntu@119.45.242.239:/opt/drawlogo-api/
scp api/deploy/* ubuntu@119.45.242.239:/opt/drawlogo-api/deploy/
# 服务器
sudo systemctl restart drawlogo-api
```
> 注意:静态站的 Gitea 自动部署只更新 `/opt/drawlogo`,**不会**更新 `/opt/drawlogo-api`,API 代码需按上面的步骤单独发布。
---
## 九、架构
```
公司各项目
│ HTTPS + X-API-Key
▼
nginx (drawlogo.zhuig.com)
├── / → /opt/drawlogo(静态站)
└── ^~ /api/ → 127.0.0.1:7891(drawlogo-api)
├── data/*.idx.json 常驻内存(检索索引)
├── data/*.jsonl 按字节偏移随机读取(素材正文)
├── data/apikeys.json Key(SHA-256)
└── data/skills.json 技能元数据
```
- 零第三方依赖(Node 原生 `http`/`fs`)
- 索引常驻约 130–170 MB;素材正文按需 seek,单次读几百字节
- 服务只监听 `127.0.0.1`,公网仅经 nginx HTTPS 进入
- 内存上限 900 MB(systemd `MemoryMax`),避免影响同机其他项目