API 使用说明

通过 API 接口,您可以在自己的系统中集成条码生成能力。接口需要签名鉴权,请先在 账户管理 → API 密钥 中创建密钥。

接口地址
POSThttps://mantianxing.cc/api/v1/generate
签名鉴权

请求参数

参数类型必填说明
appidstringAPI 密钥 AppKey
datastring请求数据,JSON 格式
timeint当前时间戳(秒),时差不能超过 30 秒
nostrstring随机字符串
signstring签名,生成规则见下方

签名规则

sign = md5(appid + "#" + data + "#" + time + "#" + appkey + "#" + nostr)

appkey 为密钥私钥,仅参与签名,不参与请求传输

业务参数(放在 data 的 JSON 中)
参数类型必填说明
typestring码制类型,如 GS1_128、GS1_DATAMATRIX、QRCODE 等
datastring条码数据
scaleint缩放比例 1-10,默认 2
notextint隐藏文字:1=隐藏 0=显示,默认 1
borderint边框宽度,默认 0
formatstring返回格式:image(图片流,默认)/ url / base64
返回示例

默认返回(图片流,format=image)

返回 PNG 图片二进制数据,Content-Type: image/png,可直接用于 <img src="..."> 或保存为文件。

format=url(返回图片地址)

{
            "code": 1,
            "info": "请求响应成功!",
            "data": {
                "code": 1,
                "info": "生成成功",
                "data": {
                    "url": "/safefile/barcode/abc123.png"
                }
            }
        }

format=base64(返回 Base64 编码)

{
            "code": 1,
            "info": "请求响应成功!",
            "data": {
                "code": 1,
                "info": "生成成功",
                "data": {
                    "base64": "data:image/png;base64,iVBORw0KGgo..."
                }
            }
        }

失败返回

{
            "code": 1,
            "info": "请求响应成功!",
            "data": {
                "code": 0,
                "info": "配额已用完,请升级套餐"
            }
        }
调用示例

PHP

$appKey = 'BCxxxxxxxxxxxxxxxx';
$appSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$time = time();
$nostr = substr(md5(uniqid()), 0, 16);
$data = json_encode([
    'type' => 'GS1_128',
    'data' => '(01)06959385782633(10)202601012A',
    'scale' => 2,
    'format' => 'url',
]);

$sign = md5($appKey . '#' . $data . '#' . $time . '#' . $appSecret . '#' . $nostr);

$result = file_get_contents('https://mantianxing.cc/api/v1/generate', false, stream_context_create([
    'http' => [
        'method' => 'POST',
        'header' => 'Content-Type: application/x-www-form-urlencoded',
        'content' => http_build_query([
            'appid' => $appKey, 'data' => $data, 'time' => $time, 'nostr' => $nostr, 'sign' => $sign,
        ]),
    ],
]));

$res = json_decode($result, true);
// $res['data']['url'] 即图片地址

JavaScript

const appKey = 'BCxxxxxxxxxxxxxxxx';
const appSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
const time = Math.ceil(Date.now() / 1000);
const nostr = Math.random().toString(36).substr(2, 15);
const data = JSON.stringify({ type: 'GS1_128', data: '(01)06959385782633(10)202601012A' });
const sign = md5(appKey + '#' + data + '#' + time + '#' + appSecret + '#' + nostr);

fetch('https://mantianxing.cc/api/v1/generate', {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: new URLSearchParams({ appid: appKey, data, time, nostr, sign }),
})
.then(res => res.blob())
.then(blob => {
    const img = document.createElement('img');
    img.src = URL.createObjectURL(blob);
    document.body.appendChild(img);
});

Python

import hashlib, time, requests, random, string, json

app_key = 'BCxxxxxxxxxxxxxxxx'
app_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
timestamp = int(time.time())
nostr = ''.join(random.choices(string.ascii_lowercase + string.digits, k=16))
data = json.dumps({'type': 'GS1_128', 'data': '(01)06959385782633(10)202601012A'})

sign_str = f"{app_key}#{data}#{timestamp}#{app_secret}#{nostr}"
sign = hashlib.md5(sign_str.encode()).hexdigest()

resp = requests.post('https://mantianxing.cc/api/v1/generate', data={
    'appid': app_key, 'data': data, 'time': timestamp, 'nostr': nostr, 'sign': sign,
})

with open('barcode.png', 'wb') as f:
    f.write(resp.content)

Java

import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.*;

public class BarcodeApi {
    public static void main(String[] args) throws Exception {
        String appKey = "BCxxxxxxxxxxxxxxxx";
        String appSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        long time = System.currentTimeMillis() / 1000;
        String nostr = UUID.randomUUID().toString().replace("-", "").substring(0, 16);
        String data = "{\"type\":\"GS1_128\",\"data\":\"(01)06959385782633(10)202601012A\"}";

        String signStr = appKey + "#" + data + "#" + time + "#" + appSecret + "#" + nostr;
        String sign = md5(signStr);

        URL url = new URL("https://mantianxing.cc/api/v1/generate");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);

        String params = String.format("appid=%s&data=%s&time=%d&nostr=%s&sign=%s",
            URLEncoder.encode(appKey, "UTF-8"),
            URLEncoder.encode(data, "UTF-8"),
            time,
            URLEncoder.encode(nostr, "UTF-8"),
            URLEncoder.encode(sign, "UTF-8")
        );

        try (OutputStream os = conn.getOutputStream()) {
            os.write(params.getBytes(StandardCharsets.UTF_8));
        }

        try (InputStream is = conn.getInputStream();
             FileOutputStream fos = new FileOutputStream("barcode.png")) {
            byte[] buf = new byte[4096];
            int len;
            while ((len = is.read(buf)) > 0) fos.write(buf, 0, len);
        }
    }

    static String md5(String input) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digest = md.digest(input.getBytes(StandardCharsets.UTF_8));
        StringBuilder sb = new StringBuilder();
        for (byte b : digest) sb.append(String.format("%02x", b));
        return sb.toString();
    }
}

Go

package main

import (
    "crypto/md5"
    "fmt"
    "io"
    "net/http"
    "net/url"
    "os"
    "time"
    "math/rand"
)

func main() {
    appKey := "BCxxxxxxxxxxxxxxxx"
    appSecret := "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    timestamp := time.Now().Unix()
    nostr := fmt.Sprintf("%x", rand.Int63())[:16]
    data := `{"type":"GS1_128","data":"(01)06959385782633(10)202601012A"}`

    signStr := fmt.Sprintf("%s#%s#%d#%s#%s", appKey, data, timestamp, appSecret, nostr)
    sign := fmt.Sprintf("%x", md5.Sum([]byte(signStr)))

    resp, _ := http.PostForm("https://mantianxing.cc/api/v1/generate", url.Values{
        "appid": {appKey},
        "data":  {data},
        "time":  {fmt.Sprintf("%d", timestamp)},
        "nostr": {nostr},
        "sign":  {sign},
    })
    defer resp.Body.Close()

    file, _ := os.Create("barcode.png")
    defer file.Close()
    io.Copy(file, resp.Body)
}

cURL(命令行)

# 生成签名
appKey="BCxxxxxxxxxxxxxxxx"
appSecret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
time=$(date +%s)
nostr=$(openssl rand -hex 8)
data='{"type":"GS1_128","data":"(01)06959385782633(10)202601012A"}'
sign=$(echo -n "$appKey#$data#$time#$appSecret#$nostr" | md5sum | cut -d' ' -f1)

# 请求生成条码
curl -X POST https://mantianxing.cc/api/v1/generate \
  -d "appid=$appKey" \
  -d "data=$data" \
  -d "time=$time" \
  -d "nostr=$nostr" \
  -d "sign=$sign" \
  -o barcode.png
系统支持的码制及示例
分类码制名称type 值示例数据
物流工业Code 128CODE128ABC123456
物流工业Code 128BCODE128BabcABC123
物流工业Code 39CODE39CODE39
物流工业扩展 Code 39EXCODE39code39!@#
物流工业Code 93CODE93CODE-93
物流工业Code 11CODE111234567890
物流工业CodabarCODABARA12345B
物流工业ITF-14ITF141234567890123
物流工业VINVIN1HGBH41JXMN109186
零售EAN-13EANX6907031121121
零售EAN-13 + 校验码EANX_CHK690703112112
零售EAN-13 复合码EANX_CC0123456789012|[17]251231
零售UPC-AUPCA725272730706
零售UPC-A + 校验码UPCA_CHK725272730706
零售UPC-A 复合码UPCA_CC012345678905|[17]251231
零售UPC-EUPCE0123456
零售UPC-E + 校验码UPCE_CHK0123457
零售UPC-E 复合码UPCE_CC01234565|[17]251231
零售EAN-14EAN141234567890123
零售ISBNISBNX9787121123450
GS1 体系GS1-128GS1_128(01)06959385782633(10)202601012A
GS1 体系GS1-128 复合码GS1_128_CC[01]01234567890128|[17]251231
GS1 体系GS1 DataMatrixGS1_DATAMATRIX(01)06959385782633(10)202601012A(21)000001
GS1 体系GS1 DataBar 全向DBAR_OMN0123456789012
GS1 体系GS1 DataBar 有限DBAR_LTD0123456789012
GS1 体系GS1 DataBar 扩展DBAR_EXP[01]01234567890128
GS1 体系GS1 DataBar 堆叠DBAR_STK0123456789012
GS1 体系GS1 DataBar 全向堆叠DBAR_OMNSTK0123456789012
GS1 体系GS1 DataBar 扩展堆叠DBAR_EXPSTK[01]01234567890128
复合码GS1 DataBar 全向复合DBAR_OMN_CC01234567890128|[17]251231
复合码GS1 DataBar 有限复合DBAR_LTD_CC01234567890128|[17]251231
复合码GS1 DataBar 扩展复合DBAR_EXP_CC[01]01234567890128|[17]251231
复合码GS1 DataBar 堆叠复合DBAR_STK_CC01234567890128|[17]251231
复合码GS1 DataBar 全向堆叠复合DBAR_OMNSTK_CC01234567890128|[17]251231
复合码GS1 DataBar 扩展堆叠复合DBAR_EXPSTK_CC[01]01234567890128|[17]251231
二维码QR CodeQRCODEhttps://example.com
二维码Micro QRMICROQR12345
二维码Data MatrixDATAMATRIXABC123
二维码AztecAZTECHello World
二维码汉信码HANXIN汉字测试
二维码Grid MatrixGRIDMATRIXGrid Test
二维码Code OneCODEONE1234567890
二维码UPN QRUPNQR1234567890
二维码矩形 Micro QRRMQR12345
二维码UltracodeULTRA123456
二维码Aztec RunesAZRUNE12
二维码DotCodeDOTCODE12345678
堆叠码PDF417PDF417条码生成器
堆叠码紧凑 PDF417PDF417COMPCOMPACT
堆叠码Micro PDF417MICROPDF417MICRO
堆叠码MaxiCodeMAXICODE123456789012345678
堆叠码Codablock-FCODABLOCKF123456
堆叠码Code 16KCODE16K1234567890
堆叠码Code 49CODE491234567890
医疗HIBC Code 128HIBC_128+H12345678901
医疗HIBC Code 39HIBC_39+H12345
医疗HIBC Data MatrixHIBC_DM+H12345678901
医疗HIBC QR CodeHIBC_QR+H12345678901
医疗HIBC PDF417HIBC_PDF+H12345678901
医疗HIBC MicroPDF417HIBC_MICPDF+H12345678901
医疗HIBC Codablock-FHIBC_BLOCKF+H12345678901
医疗HIBC AztecHIBC_AZTEC+H12345678901
邮政USPS POSTNETPOSTNET123456789
邮政USPS PLANETPLANET12345678901
邮政英国皇家邮政 4SCCRM4SCCAB12CD
邮政日本邮政JAPANPOST123-4567
邮政韩国邮政KOREAPOST123456
邮政澳大利亚邮政AUSPOST12345678
邮政澳大利亚邮政回执AUSREPLY12345678
邮政澳大利亚邮政路由AUSROUTE12345678
邮政澳大利亚邮政重定向AUSREDIRECT12345678
邮政荷兰 KIX 邮政KIX1234AB
邮政德国邮政 LeitcodeDPLEIT1234567890123
邮政德国邮政 IdentcodeDPIDENT12345678901
邮政USPS 智能邮件USPS_IMAIL12345678901234567890
邮政Royal Mail MailmarkMAILMARK12345678
工业/其他MSI PlesseyMSI_PLESSEY1234567
工业/其他UK PlesseyPLESSEY123456
工业/其他Telepen AlphaTELEPENABC123
工业/其他Telepen NumericTELEPEN_NUM123456
工业/其他Pharmacode 单轨PHARMA12345
工业/其他PZN 德国药品码PZN1234567
工业/其他Pharmacode 双轨PHARMA_TWO123456
工业/其他FIM 码FIMA
工业/其他FlattermarkenFLAT12345
工业/其他NVE-18NVE1812345678901234567
工业/其他DAFT 码DAFTDDAFFTTD
工业/其他DPD 包裹码DPD0123456789012345678901234567
工业/其他Channel CodeCHANNEL12345
工业/其他标准 2 of 5C25STANDARD1234567890
工业/其他交叉 2 of 5C25INTER1234567890
工业/其他IATA 2 of 5C25IATA1234567890
工业/其他Data Logic 2 of 5C25LOGIC1234567890
工业/其他工业 2 of 5C25IND1234567890
工业/其他Code 32 意大利药品码CODE321234567890

使用中遇到问题请联系qiannao@qq.com。