CodeBoxCodeBox 文档

生成二维码

POST /generate - 只需传入 content 即可生成二维码

接口说明

将文本或链接生成二维码图片。只需传入 content 字段即可,其他参数均为可选。

POST https://www.codebox.club/api/v1/qrcode/generate
Content-Type: application/json

快速开始

最简调用,只传 content

curl -X POST https://www.codebox.club/api/v1/qrcode/generate \
  -H "Content-Type: application/json" \
  -d '{"content": "https://example.com"}' \
  -o qrcode.png

返回 base64 格式:

curl -X POST https://www.codebox.club/api/v1/qrcode/generate \
  -H "Content-Type: application/json" \
  -d '{"content": "https://example.com", "responseFormat": "json"}'

请求参数

必填

参数类型说明
contentstring要编码的内容(链接或文本),最大 4000 字符

可选 - 基础

参数类型默认值说明
sizeinteger300图片尺寸(像素),50-2000
formatstringpng输出格式:png / svg
margininteger10边距(像素),0-100
errorCorrectionLevelstringM纠错等级:L / M / Q / H
responseFormatstringbinary响应格式:json(base64)/ binary(图片流)
styleModestringnormalnormal 普通 / image 图片模式(支持背景图)

可选 - 样式

参数类型默认值说明
dotsStylestringrounded点阵形状:square / dots / rounded / extra-rounded / classy / classy-rounded
dotsColorstring#f59e0b点阵颜色,十六进制
dotRadiusRationumber0.5点阵大小比例,0.1-0.5
cornersSquareStylestringdot定位角外框:square / extra-rounded / dot
cornersSquareColorstring同 dotsColor定位角外框颜色
cornersDotStylestringdot定位角内点:square / dot / extra-rounded
cornersDotColorstring同 dotsColor定位角内点颜色
backgroundColorstring#FFFFFF背景颜色

可选 - 渐变

参数类型说明
dotsGradient.typestringlinearradial
dotsGradient.rotationnumber渐变角度(度)
dotsGradient.colorStopsarray色标数组,至少 2 个,含 offset(0-1) 和 color
参数类型默认值说明
logoUrlstring-Logo 图片 URL
logoSizenumber0.2Logo 大小比例,0.1-0.5
logoMargininteger5Logo 留白(像素),0-50
logoXnumber0.5Logo X 轴位置,0-1
logoYnumber0.5Logo Y 轴位置,0-1

可选 - 背景图(image 模式)

参数类型默认值说明
backgroundImagestring-背景图片 URL
backgroundImageSizenumber1.0缩放比例,0.1-2.0
backgroundImageOpacitynumber1.0透明度,0.1-1.0

请求示例

{
  "content": "https://www.codebox.club"
}
{
  "content": "https://www.codebox.club",
  "dotsStyle": "dots",
  "dotsColor": "#e94560",
  "cornersSquareStyle": "extra-rounded",
  "responseFormat": "json"
}
{
  "content": "https://www.codebox.club",
  "dotsGradient": {
    "type": "linear",
    "rotation": 45,
    "colorStops": [
      { "offset": 0, "color": "#e94560" },
      { "offset": 1, "color": "#0f3460" }
    ]
  },
  "responseFormat": "json"
}
{
  "content": "https://www.codebox.club",
  "styleMode": "image",
  "backgroundImage": "https://example.com/bg.jpg",
  "backgroundImageOpacity": 0.6,
  "responseFormat": "json"
}

代码示例

# 最简调用,下载 PNG
curl -X POST https://www.codebox.club/api/v1/qrcode/generate \
  -H "Content-Type: application/json" \
  -d '{"content": "https://example.com"}' \
  -o qrcode.png

# 带 API Key + 自定义样式
curl -X POST https://www.codebox.club/api/v1/qrcode/generate \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer cb_sk_xxxxxxxx" \
  -d '{
    "content": "https://example.com",
    "dotsStyle": "dots",
    "dotsColor": "#e94560",
    "responseFormat": "json"
  }'
async function generateQR(content, options = {}) {
  const res = await fetch('https://www.codebox.club/api/v1/qrcode/generate', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ content, responseFormat: 'json', ...options }),
  });
  const data = await res.json();
  return data.data.image; // base64 图片
}

// 最简调用
const img = await generateQR('https://example.com');

// 自定义样式
const styledImg = await generateQR('https://example.com', {
  dotsStyle: 'dots',
  dotsColor: '#e94560',
});
import requests, base64

def generate_qr(content, **options):
    res = requests.post(
        'https://www.codebox.club/api/v1/qrcode/generate',
        json={'content': content, 'responseFormat': 'json', **options},
    )
    return res.json()['data']['image']

# 最简调用
image = generate_qr('https://example.com')

# 保存为文件
_, encoded = image.split(',', 1)
with open('qrcode.png', 'wb') as f:
    f.write(base64.b64decode(encoded))

响应格式

JSON 格式(responseFormat: "json"

{
  "success": true,
  "data": {
    "image": "data:image/png;base64,iVBORw0KGgo...",
    "format": "png",
    "size": 300,
    "mimeType": "image/png"
  }
}

Binary 格式(默认)

直接返回图片二进制数据,Content-Typeimage/pngimage/svg+xml

错误响应

{
  "error": "Validation error",
  "details": "Field \"content\" is required and must be a non-empty string."
}
状态码说明
400参数错误
429频率超限
500服务器错误

On this page