代码美化图片 API 接入文档code-beautify
将代码片段渲染为精美的SVG/PNG卡片图片。支持16种编程语言语法高亮、8套设计主题,适合社交分享和技术文档配图。
1. 基本信息
| 接口地址 | https://v1.apizero.cn/api/code-beautify |
|---|---|
| 请求方法 | POST |
| 分类 | dev |
| 提供方 | 极数本源 |
| 计费模式 | 免费试用 |
| 单次消耗 | 0 积分 |
| 起步价 | — |
| QPS 限制 | 3 req/s |
| 每日免费额度 | 3000 次(已认证用户) |
| 匿名每日额度 | 2000 次(无 API Key) |
| VIP 免费 | 否 |
| 调用次数 |
2. 认证
需要 API Key。登录用户每日 30 次免费;匿名每日 20 次。
获取 API Key:登录 https://apizero.cn/account/keys
3. 请求参数
| 参数名 | 类型 | 必填 | 说明 | 示例 |
|---|---|---|---|---|
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 |
theme | string | 否 | 主题:aurora/sunset/forest/midnight/rose/ocean/volcano/mono | aurora |
title | string | 否 | 卡片顶部标题 | snippet.ts |
line_numbers | int | 否 | 是否显示行号 1/0 | 1 |
scale | int | 否 | PNG放大倍数 1-4 | 2 |
output | string | 否 | 输出格式:svg/png/json | json |
4. 请求头
| Header | 类型 | 必填 | 说明 | 示例 |
|---|---|---|---|---|
Authorization | string | 是 | — | — |
5. 请求示例
以下 5 种语言示例都是可直接运行的,只需把 YOUR_API_KEY 替换为实际 Key。
cURL
curl -X POST "https://v1.apizero.cn/api/code-beautify" \
-H "X-Api-Key: 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"
}'Python
import requests
resp = requests.request(
"POST",
"https://v1.apizero.cn/api/code-beautify",
headers={"X-Api-Key": "YOUR_API_KEY", "Content-Type": "application/json"},
json={
"code": "const sum = (a, b) => a + b;",
"language": "typescript",
"theme": "aurora",
"title": "snippet.ts",
"line_numbers": "1",
"scale": "2",
"output": "json",
},
timeout=15,
)
resp.raise_for_status()
print(resp.json())JavaScript (Node.js)
// Node.js 18+ / 浏览器原生 fetch
const res = await fetch("https://v1.apizero.cn/api/code-beautify", {
method: "POST",
headers: {
"X-Api-Key": "YOUR_API_KEY",
"Content-Type": "application/json",
},
body: JSON.stringify({
"code": "const sum = (a, b) => a + b;",
"language": "typescript",
"theme": "aurora",
"title": "snippet.ts",
"line_numbers": "1",
"scale": "2",
"output": "json"
}),
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();
console.log(data);Go
package main
import (
"bytes"
"fmt"
"io"
"net/http"
)
func main() {
body := []byte(`{"code":"const sum = (a, b) => a + b;","language":"typescript","theme":"aurora","title":"snippet.ts","line_numbers":"1","scale":"2","output":"json"}`)
req, _ := http.NewRequest("POST", "https://v1.apizero.cn/api/code-beautify", bytes.NewBuffer(body))
req.Header.Set("X-Api-Key", "YOUR_API_KEY")
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil { panic(err) }
defer resp.Body.Close()
out, _ := io.ReadAll(resp.Body)
fmt.Println(string(out))
}PHP
<?php
$payload = json_encode([
"code" => "const sum = (a, b) => a + b;",
"language" => "typescript",
"theme" => "aurora",
"title" => "snippet.ts",
"line_numbers" => "1",
"scale" => "2",
"output" => "json",
], JSON_UNESCAPED_UNICODE);
$ch = curl_init("https://v1.apizero.cn/api/code-beautify");
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => [
"X-Api-Key: YOUR_API_KEY",
"Content-Type: application/json",
],
CURLOPT_TIMEOUT => 15,
]);
$body = curl_exec($ch);
curl_close($ch);
$data = json_decode($body, true);
print_r($data);6. 响应字段
| 字段 | 类型 | 说明 | 示例 |
|---|---|---|---|
language | string | 识别/指定的语言 | — |
theme | string | 使用的主题key | — |
theme_name | string | 主题中文名 | — |
title | string | 卡片标题 | — |
line_count | int | 代码行数 | — |
width | int | 图片宽度(px) | — |
height | int | 图片高度(px) | — |
svg | string | SVG 字符串 | — |
png_base64 | string | PNG base64(output=json时返回) | — |
7. 响应示例
{
"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"
}8. 错误码
| code | status | 说明 |
|---|---|---|
4000 | — | 缺少 code 参数 |
5020 | — | 代码美化渲染服务不可用 |
9. 变更日志
- 1.0.0(2026-05-08)
- 首次上线
- 支持16种语言、8套主题、SVG/PNG输出
常见问题
代码美化图片 接口怎么免费使用?
未登录用户每个 IP 每天 2000 次免费。登录用户创建 API Key 后每天 3000 次免费,超额部分按点数计费(0 点/次)。
代码美化图片 支持哪些调用方式?
接口使用 POST 请求。文档提供 cURL、Python、JavaScript (Node.js)、Go、PHP 五种语言的可运行示例。也可以下载 /openapi.json 导入 Postman / Insomnia / Apifox 反向生成 SDK。
调用不限额么? QPS 是多少?
本接口 QPS 限制 3 req/s,每个 API Key 每日免费 3000 次。需要更高额度可升级 VIP 套餐或联系售后提高 QPS。
这个接口跟自己直连上游有什么区别?
极数本源 作为中间层提供:统一鉴权(一个 Key 调所有接口)、统一计费(点数制)、统一限流、统一错误码、多上游自动切换。免去逐个对接上游、维护 Key、统计调用量的运维成本。