API Code Examples

Ready-to-run snippets for calling the HCC2D API from JavaScript, Python, Java, and PHP. Replace hcc2d_<your-key> with your actual API key.

← Back to API documentation

JavaScript (fetch)

Works in Node.js 18+ and modern browsers.

const API_KEY = "hcc2d_<your-key>";
const BASE    = "https://hcc2d.com";

async function generateCode(text, filename = "payload.txt") {
  const payloadBase64 = btoa(
    String.fromCharCode(...new TextEncoder().encode(text))
  );

  const res = await fetch(BASE + "/api/v1/generate", {
    method: "POST",
    headers: {
      "Authorization": "Bearer " + API_KEY,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      payloadBase64,
      inputName: filename,
      structured: true,
      mode: "hcc2d8",
      ec: "M",
    }),
  });

  if (!res.ok) {
    const err = await res.json();
    throw new Error(String(res.status) + " " + err.code + ": " + err.error);
  }

  const { imageUrl } = await res.json();
  const img = await fetch(BASE + imageUrl);
  const buffer = await img.arrayBuffer();
  return Buffer.from(buffer);
}

Python (requests)

Requires the requests library (pip install requests).

import base64
import requests

API_KEY = "hcc2d_<your-key>"
BASE    = "https://hcc2d.com"

def generate_code(text: str, filename: str = "payload.txt", out: str = "code.png") -> None:
    payload_b64 = base64.b64encode(text.encode()).decode()

    res = requests.post(
        f"{BASE}/api/v1/generate",
        headers={
            "Authorization": f"Bearer {API_KEY}",
            "Content-Type": "application/json",
        },
        json={
            "payloadBase64": payload_b64,
            "inputName": filename,
            "structured": True,
            "mode": "hcc2d8",
            "ec": "M",
        },
        timeout=30,
    )
    res.raise_for_status()

Java (HttpClient)

Requires Java 11+ and uses the standard java.net.http package.

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Path;
import java.util.Base64;

public class Hcc2dExample {
    static final String API_KEY = "hcc2d_<your-key>";
    static final String BASE    = "https://hcc2d.com";
}

PHP (cURL)

Works with PHP 7.4+ and the standard curl extension.

<?php
const API_KEY = 'hcc2d_<your-key>';
const BASE    = 'https://hcc2d.com';

function generateCode(string $text, string $filename = 'payload.txt', string $out = 'code.png'): void {
    $payloadBase64 = base64_encode($text);
}

Encoding binary files

For non-text payloads (PDF, ZIP, images, ...) read the raw bytes and base64-encode them directly:

JavaScript

import { readFileSync } from "fs";

const bytes = readFileSync("document.pdf");
const payloadBase64 = bytes.toString("base64");

Python

import base64

with open("document.pdf", "rb") as f:
    payload_b64 = base64.b64encode(f.read()).decode()

Java

byte[] bytes = Files.readAllBytes(Path.of("document.pdf"));
String payloadBase64 = Base64.getEncoder().encodeToString(bytes);

PHP

$payloadBase64 = base64_encode(file_get_contents('document.pdf'));

See also

API documentation — full field reference, error codes, and rate limits.

HCC2DF format reference — binary layout of the structured container.

← Back to HCC2D Encoder