PHP

Barcode Generator API for PHP

Generate barcodes in PHP with plain cURL - no Composer package, GD extension or bundled fonts required.

The BarGen REST API returns Code 128, EAN, UPC, GS1-128, DataMatrix and QR codes as SVG, PNG, PDF or EPS.

Works the same in Laravel, Symfony, WordPress/WooCommerce hooks and legacy PHP applications.

GS1-128 element strings are validated server-side; the free tier includes 1,000 calls per month.

PHP barcode libraries lean on GD or Imagick and ship their own font handling - workable, but another thing to keep patched across PHP upgrades, and GS1 validation is rarely included. Delegating to an API turns barcode generation into two functions and an env var, identical from PHP 7.4 to 8.4.

The examples use bare cURL so they work in any codebase. In Laravel you can swap them for Http::withHeaders(...)->get(...) one-for-one; in WooCommerce the natural place is an order hook that generates the label barcode when an order is paid.

Setup

# no package needed - PHP cURL is built in

Generate barcodes in PHP

<?php
const BARGEN_API_KEY = 'YOUR_API_KEY'; // create one at bargen.pro/dashboard
const BARGEN_BASE = 'https://api.bargen.pro/v1';

function bargen_generate(string $type, string $data, array $options = []): string
{
    $query = http_build_query(array_merge(['data' => $data, 'format' => 'svg'], $options));
    $ch = curl_init(BARGEN_BASE . "/barcode/{$type}?{$query}");
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => ['X-API-Key: ' . BARGEN_API_KEY],
        CURLOPT_TIMEOUT => 15,
    ]);
    $body = curl_exec($ch);
    $status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
    curl_close($ch);
    if ($body === false || $status !== 200) {
        throw new RuntimeException("BarGen error {$status}: {$body}");
    }
    return $body;
}

// A Code 128 barcode as SVG
file_put_contents('order.svg', bargen_generate('code128', 'ORDER-2026-0042'));

// A GS1-128 barcode - Application Identifiers validated server-side
$gs1 = bargen_generate('gs1-128', '(01)09501101530003(17)261231(10)AB123');

// A QR code as PNG with custom colour and scale
$png = bargen_generate('qrcode', 'https://bargen.pro', ['format' => 'png', 'color' => '1a1a2e', 'scale' => 4]);

Bulk generation with the batch endpoint

<?php
// Up to 50 barcodes in one request via the batch endpoint
$payload = json_encode([
    'codes' => [
        ['type' => 'code128', 'data' => 'SKU-0001'],
        ['type' => 'code128', 'data' => 'SKU-0002'],
        ['type' => 'qrcode',  'data' => 'https://example.com/p/1'],
    ],
    'options' => ['format' => 'svg'],
]);
$ch = curl_init(BARGEN_BASE . '/barcode/batch');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $payload,
    CURLOPT_HTTPHEADER => ['X-API-Key: ' . BARGEN_API_KEY, 'Content-Type: application/json'],
]);
$results = json_decode(curl_exec($ch), true)['results'] ?? [];
curl_close($ch);

Frequently asked questions

Can I use this inside WordPress or WooCommerce?

Yes - call the API from any hook (for example woocommerce_order_status_processing) using the function above or wp_remote_get(), then attach the returned image to the order email or packing slip. No plugin or GD configuration is involved.

How does this look in Laravel?

One-for-one with the HTTP client: Http::withHeaders(["X-API-Key" => config("services.bargen.key")])->get("https://api.bargen.pro/v1/barcode/code128", ["data" => $sku, "format" => "svg"])->throw()->body(). Wrap it in a small service class and cache repeated codes.

Do I need the GD or Imagick extension?

No. Rendering happens server-side at BarGen; PHP only transports bytes. That removes a whole class of "works locally, broken on the host" issues tied to image extensions and fonts.

What does it cost?

The free tier is 1,000 API calls per month with no credit card. Paid plans are flat: $29/month for 10,000 calls, $79/month for 100,000 with a 99.9% SLA, Enterprise from $249/month.

API basics

Endpoint
GET api.bargen.pro/v1/barcode/{type}
Auth
X-API-Key header
Output
SVG, PNG, PDF, EPS
Formats
25 barcode types incl. GS1 and postal
Free tier
1,000 calls/month
Full API reference

Code on GitHub

Runnable examples for every language live in the public docs repository.

bargen-pro/bargen-api-docs

Start generating barcodes from PHP

Free tier with 1,000 API calls per month. No credit card required.