Skip to main content

Generate Code

POST /api/v1/code/generate

Generate a cryptographically signed GS1 Digital Link URL for a product.

Request

Headers

HeaderRequiredDescription
x-api-keyYesYour Optropic API key
Content-TypeYesMust be application/json

Body Parameters

ParameterTypeRequiredDescription
gtinstringYes14-digit GTIN (Global Trade Item Number)
serialNumberstringYesUnique serial number for this item
batchNumberstringNoBatch/lot number
expiryDatestringNoExpiry date in ISO 8601 format
metadataobjectNoAdditional key-value data

Example Request

curl -X POST https://api.optropic.com/api/v1/code/generate \
-H "x-api-key: optr_live_YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{
"gtin": "04260799580008",
"serialNumber": "SN-2026-001",
"batchNumber": "BATCH-A",
"metadata": {
"product": "Premium Widget",
"color": "Blue"
}
}'

Response

Success (201 Created)

{
"url": "https://id.optropic.com/01/04260799580008/21/SN-2026-001/10/BATCH-A?sig=Kx7mN2...",
"gtin": "04260799580008",
"serial": "SN-2026-001",
"batch": "BATCH-A",
"signature": "Kx7mN2vL9pQr5tYw8zAb...",
"keyId": "key_abc123",
"createdAt": "2026-02-18T10:30:00Z"
}

Response Fields

FieldTypeDescription
urlstringThe complete GS1 Digital Link URL with signature
gtinstringThe GTIN used
serialstringThe serial number
batchstringThe batch number (if provided)
signaturestringBase64url-encoded Ed25519 signature
keyIdstringID of the signing key used
createdAtstringISO 8601 timestamp

URL Structure

The generated URL follows GS1 Digital Link syntax:

https://id.optropic.com/01/{GTIN}/21/{SERIAL}/10/{BATCH}?sig={SIGNATURE}
ComponentAI (Application Identifier)Description
01GTINGlobal Trade Item Number
21SerialSerial number
10Batch/LotBatch number
sigEd25519 signature (query param)

Errors

CodeHTTP StatusDescription
INVALID_GTIN400GTIN is not a valid 14-digit number
DUPLICATE_SERIAL409This GTIN+Serial combination already exists
MISSING_REQUIRED_FIELD400Required field is missing
RATE_LIMITED429Generate rate limit exceeded

Example Error

{
"error": {
"code": "INVALID_GTIN",
"message": "GTIN must be exactly 14 digits",
"details": {
"provided": "1234567890",
"expected": "14 digits"
}
}
}

Code Examples

JavaScript

async function generateCode(gtin, serialNumber, batchNumber) {
const response = await fetch('https://api.optropic.com/api/v1/code/generate', {
method: 'POST',
headers: {
'x-api-key': process.env.OPTROPIC_API_KEY,
'Content-Type': 'application/json',
},
body: JSON.stringify({
gtin,
serialNumber,
batchNumber,
}),
});

if (!response.ok) {
const error = await response.json();
throw new Error(error.error.message);
}

return response.json();
}

// Usage
const code = await generateCode('04260799580008', 'SN-001', 'BATCH-A');
console.log(code.url);

Python

import requests
import os

def generate_code(gtin: str, serial: str, batch: str = None) -> dict:
response = requests.post(
'https://api.optropic.com/api/v1/code/generate',
headers={
'x-api-key': os.environ['OPTROPIC_API_KEY'],
'Content-Type': 'application/json',
},
json={
'gtin': gtin,
'serialNumber': serial,
'batchNumber': batch,
}
)
response.raise_for_status()
return response.json()

# Usage
code = generate_code('04260799580008', 'SN-001', 'BATCH-A')
print(code['url'])

QR Code Generation

The URL returned can be encoded into a QR code using any standard library:

import QRCode from 'qrcode';

const code = await generateCode('04260799580008', 'SN-001', 'BATCH-A');
const qrDataUrl = await QRCode.toDataURL(code.url);
// Use qrDataUrl in an <img> tag
Print-Ready QR Codes

For print production, generate QR codes at minimum 300 DPI with adequate quiet zones. We recommend SVG format for scalability.