代码美化图片

code-beautifyPOST
POSThttps://v1.apizero.cn/api/code-beautify

概述

将代码片段渲染为精美的SVG/PNG卡片图片。支持16种编程语言语法高亮、8套设计主题,适合社交分享和技术文档配图。

调用约定

  • 网关 · https://v1.apizero.cn
  • 鉴权 · Authorization: Bearer <API Key>
  • 回包 · JSON {code, msg, data}。成功看 code === 0,不要只看 HTTP 200。
/aidocs/code-beautify/raw.md
打开 Markdown

鉴权

需要 API Key。登录用户每日 30 次免费;匿名每日 20 次。

获取 API Key

请求头

Authorizationstring选填

推荐:Bearer <API Key>。兼容请求头 X-API-Key,以及 Query api_key / apikey / key(key 仅当值为 sk_live_/sk_test_/sk_stag_ 形态时生效)。匿名可不传,受每日免费额度限制。

请求参数

POST · APPLICATION/JSON

code(编码)string必填

代码内容

例const sum = (a, b) => a + b;

language(语言)string选填

语言:auto/python/javascript/typescript/json/bash/go/rust/java/c/cpp/html/css/sql/yaml/markdown

例typescript

themestring选填

主题:aurora/sunset/forest/midnight/rose/ocean/volcano/mono

例aurora

titlestring选填

卡片顶部标题

例snippet.ts

line_numbersint选填

是否显示行号 1/0

例1

scaleint选填

PNG放大倍数 1-4

例2

outputstring选填

输出格式:svg/png/json

例json

{
  "code": "const sum = (a, b) => a + b;",
  "language": "typescript",
  "theme": "aurora",
  "title": "snippet.ts",
  "line_numbers": 1,
  "scale": 2,
  "output": "json"
}

返回结果

顶层固定为 code / msg / data。下表一般是 data 内字段。 code · msg · data · tips · request_id

languagestring

识别/指定的语言

themestring

使用的主题key

theme_namestring

主题中文名

titlestring

卡片标题

line_countint

代码行数

widthint

图片宽度(px)

heightint

图片高度(px)

svgstring

SVG 字符串

png_base64string

PNG base64(output=json时返回)

tipsstring

品牌提示(所有接口统一返回):极数本源 · https://apizero.cn

返回示例

{
  "code": 0,
  "msg": "成功",
  "data": {
    "language": "typescript",
    "theme": "aurora",
    "theme_name": "极光",
    "title": "snippet.ts",
    "line_count": 3,
    "width": 680,
    "height": 180,
    "svg": "<svg>...</svg>",
    "png_base64": "iVBORw0..."
  },
  "request_id": "req_abc123",
  "tips": "极数本源 · https://apizero.cn"
}

请求示例

示例都是服务端写法。Python / JavaScript 各有常规写法和官方库。

curl -sS -X POST "https://v1.apizero.cn/api/code-beautify" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "code": "const sum = (a, b) => a + b;",
  "language": "typescript",
  "theme": "aurora",
  "title": "snippet.ts",
  "line_numbers": "1",
  "scale": "2",
  "output": "json"
}'

错误码

先看业务 code。HTTP 也可能不是 200。

0200

成功

4000400

参数错误

4011401

API Key 无效

4013403

API Key 已暂停

4014403

当前 IP 不在 Key 白名单

4015401

此接口需要 API Key

4022402

余额不足

4029429

调用过快(QPS)

4030429

今日免费额度已用完

4040503

接口已下线

4041404

接口不存在

5000500

服务器内部错误

5020502

上游暂时不可用

5021502

上游返回格式异常

5030502

暂无可用节点

调用限制

计费模式
完全免费
QPS 限制
QPS 3
登录免费额度
3000 次(已认证)
匿名每日额度
2000 次(无 API Key)
黄金会员
每天 50,000 次 · QPS 10
钻石会员
每天 100,000 次 · QPS 30
企业会员
每天 1,000,000 次 · QPS 120

购买套餐、看评价请走商城详情。 购买 / 调试

更新日志

  • 1.0.02026-05-08

    首次上线 支持16种语言、8套主题、SVG/PNG输出