首页 开发文档 条形码生成

条形码生成

支持 EAN-13、EAN-8、UPC-A、UPC-E、Code 128、Code 39、Code 93、ITF-14、Codabar、Code 11、MSI、Pharmacode 等 20 余种码制。可输出 SVG 源码直接嵌入页面、PNG base64(附带 data URI 可直接放进 img 标签)或 HTML 片段。纯本地生成,不依赖外部服务。

请求信息
请求地址 https://cx4.cn/openApi/dev/barcode 复制
请求方式 GET / POST
接口标识 dev_barcode
鉴权方式 必须
四种方式任选其一:Header「X-Api-Key」/ Header「Authorization: Bearer 你的key」/ URL 参数「?key=你的key」/ POST 参数「key」
请求参数
参数名 类型 必填 说明 示例
text string 是 条码内容,长度需符合对应码制要求 6901234567892
type string 否 码制:code128 / code39 / code93 / ean13 / ean8 / upca / upce / itf14 / codabar / code11 / i25 / s25 / msi / pharma 等 ean13
format string 否 输出格式:svg 源码 / png 返回 base64 / html 片段 svg
width float 否 单条宽度倍数 1-10 2
height float 否 条码高度 10-300 30
color string 否 条码颜色,svg / html 生效 black
返回参数

下表字段位于返回结构的 data 内;外层 code / message 为全平台统一格式,不在此重复列出。

参数名 类型 说明 示例
ok bool 是否成功 true
text string 条码内容 6901234567892
type string 码制 ean13
format string 输出格式 svg
svg string SVG 源码,format=svg 时返回 <svg ...>...</svg>
base64 string PNG base64,format=png 时返回 iVBORw0KGgo...
data_uri string PNG data URI,format=png 时返回 data:image/png;base64,...
html string HTML 片段,format=html 时返回 <div ...>...</div>
返回示例

成功

{
    "code": 200,
    "message": "操作成功",
    "data": {
        "ok": true,
        "text": "6901234567892",
        "type": "ean13",
        "format": "svg",
        "width": 2,
        "height": 30,
        "svg": "<?xml version=\"1.0\" standalone=\"no\" ?>\r\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\r\n<svg width=\"190\" height=\"30\" viewBox=\"0 0 190 30\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\r\n\t<desc>6901234567892</desc>\r\n\t<g id=\"bars\" fill=\"black\" stroke=\"none\">\r\n\t\t<!-- 若干 rect 条码线条 --></g>\r\n</svg>\r\n"
    }
}

失败

{
    "code": 400,
    "message": "待生成内容不能为空",
    "data": null
}
平台通用状态码
状态码 说明
200调用成功
400缺少必填参数或参数格式有误
401密钥缺失或无效
402余额不足,请充值后重试
403未开通该接口,请在本页开通后调用
404接口不存在或已下架
429触发限流,请稍后重试
500接口调用失败(已扣款的会自动退款)
调用示例

cURL 复制

curl -X POST "https://cx4.cn/openApi/dev/barcode" -H "X-Api-Key: 你的SecretKey" -d "text=6901234567892&type=ean13&format=svg&width=2&height=30&color=black"

JavaScript 复制

fetch('https://cx4.cn/openApi/dev/barcode', {
  method: 'POST',
  headers: {
    'X-Api-Key': '你的SecretKey',
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  body: 'text=6901234567892&type=ean13&format=svg&width=2&height=30&color=black'
})
.then(res => res.json())
.then(data => console.log(data));

PHP 复制

post('https://cx4.cn/openApi/dev/barcode', [
    'headers' => ['X-Api-Key' => '你的SecretKey'],
    'form_params' => ['text' => '6901234567892', 'type' => 'ean13', 'format' => 'svg', 'width' => '2', 'height' => '30', 'color' => 'black'],
]);
echo $response->getBody();

Java 复制

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://cx4.cn/openApi/dev/barcode"))
    .header("X-Api-Key", "你的SecretKey")
    .header("Content-Type", "application/x-www-form-urlencoded")
    .POST(HttpRequest.BodyPublishers.ofString("text=6901234567892&type=ean13&format=svg&width=2&height=30&color=black"))
    .build();
HttpResponse response = client.send(
        request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());

Python 复制

import requests

res = requests.post('https://cx4.cn/openApi/dev/barcode',
    headers={'X-Api-Key': '你的SecretKey'},
    data={'text': '6901234567892', 'type': 'ean13', 'format': 'svg', 'width': '2', 'height': '30', 'color': 'black'})
print(res.json())
在线测试 (使用你的密钥真实调用,计费与正常调用一致)

登录后可在线测试: 立即登录

计费说明

¥0.0010

每次调用


每日免费额度:100 次

累计调用:8 次


快速上手

1. 注册并完成邮箱验证:立即注册

2. 登录后复制 Secret Key:去登录

3. 点击右侧「开通接口」按钮

4. 用下方「在线测试」一键体验,或复制下方示例代码直接调用