Skip to content

Builder and matrix

qrcode() creates an immutable builder from @qrcodesdk/core. Each options method returns a new builder; the original builder remains unchanged.

The builder accepts a string or a non-negative safe integer. Use a string for values with leading zeroes or values outside JavaScript’s safe-integer range.

import {qrcode} from '@qrcodesdk/core';
qrcode('https://qrcodesdk.dev');
qrcode(1234567890);
qrcode().payload('payload supplied later');

QRCodeSDK generates QR Code Model 2 symbols. It supports numeric, alphanumeric, and UTF-8 octet payloads, with optional automatic mixed-mode segmentation.

Mode Accepted payload Use it for
numeric Digits only Numeric identifiers and digit-only payloads
alphanumeric QR alphanumeric character set Uppercase text such as HELLO WORLD
octet UTF-8 bytes URLs, JSON, emoji, lowercase, and general text
auto or omitted Any supported payload Single-mode detection; optional mixed-mode optimization

auto is always available. Without a segmenter, the builder selects numeric for digits, alphanumeric for the QR alphanumeric character set, and octet for other text. It encodes the complete payload as one segment. A concrete mode forces the entire payload into that mode.

Import automaticSegmenter to find the shortest-bit combination of numeric, alphanumeric, and octet segments. The default generation path does not import the optimizer, so applications that omit it avoid its bundle size and processing cost.

import {qrcode} from '@qrcodesdk/core';
import {automaticSegmenter} from '@qrcodesdk/core/segmenter';
const payload = 'ABCD12345678901234567890abcd';
qrcode(payload).mode('auto').matrix(); // Single segment, version 3.
qrcode(payload).mode('auto').segmenter(automaticSegmenter).matrix(); // Mixed segments, version 2.
qrcode(payload).options({segmenter: automaticSegmenter}).matrix(); // Auto is the default.

Explicit numeric, alphanumeric, and octet modes bypass the supplied segmenter. Use .mode('auto') or .mode() to return to auto selection. Use .segmenter() or .options({segmenter: undefined}) to clear the segmenter.

Single-mode detection can produce a larger symbol than mixed-mode optimization, and a payload that fits an optimized symbol can exceed the same pinned version without optimization. To retain the previous automatic mixed-mode behavior, supply automaticSegmenter.

Framework components accept the same callback through options.matrix.segmenter. The React, Vue, Svelte, and Angular packages re-export automaticSegmenter, so applications import it from the framework package.

A custom QRCodeSegmenter receives the original payload and {version, eci}. Return a nonempty array of QRCodeSegment values with concrete modes and unencoded string payloads. Their text must concatenate to the original payload. Segments must not be empty, unless a single segment encodes an empty payload, and a boundary must not split a surrogate pair. Core validates and UTF-8 encodes each segment. With automatic version selection, the callback runs once per searched character-count group, starting at versions 1, 10, and 27. With a pinned version, it runs once for that version. The callback should be deterministic.

QRCodeMatrixOptions contains only options that affect the encoded matrix:

Option Type Default Constraint
mode 'auto' | 'numeric' | 'alphanumeric' | 'octet' 'auto' Forced mode must accept the entire payload
segmenter QRCodeSegmenter omitted Used only in auto mode
eci boolean false Only affects symbols containing octet segments
errorCorrectionLevel 'L' | 'M' | 'Q' | 'H' 'M' Higher redundancy reduces payload capacity
version integer 1–40 automatic Pinned version must fit the encoded payload
mask integer 0–7 automatic Pin only when deterministic matrix output is required

Set options individually or together:

const builder = qrcode('Grüße ✅').mode('octet').eci(true).errorCorrection('H').version(4).mask(2);
const equivalent = qrcode('Grüße ✅').options({
mode: 'octet',
eci: true,
errorCorrectionLevel: 'H',
version: 4,
mask: 2,
});

Octet payload bytes are always UTF-8. Enabling eci emits ECI assignment 26 once, immediately before the first octet segment, so scanners receive an explicit UTF-8 declaration. It adds 12 bits. With eci: false, scanners may rely on heuristics for non-ASCII text.

No ECI header is emitted when a symbol contains no octet segment, so numeric- and alphanumeric-only symbols are identical with either setting.

The levels L, M, Q, and H add increasing amounts of redundant data. The default is M. Higher levels can tolerate more damaged or obscured modules, but reduce capacity and may select a larger version. For center images, see the reliability guidance in Customize appearance.

When a pinned version cannot contain the payload, generation throws QRCode: Payload too large. Removing the override helps only if a larger version can fit the payload. If automatic selection also fails, shorten or change the payload.

Method Returns Effect
.payload(value) a builder with a payload Sets or replaces the payload
.options(options?) a configured builder Merges all supplied matrix options
.mode(mode?) a configured builder Forces or clears the encoding mode
.segmenter(segmenter?) a configured builder Sets or clears optional mixed-mode segmentation
.eci(enabled?) a configured builder Enables or clears the UTF-8 ECI declaration
.errorCorrection(level?) a configured builder Sets or clears the error correction level
.version(version?) a configured builder Pins or clears the version
.mask(mask?) a configured builder Pins or clears the mask
.matrix() QRCodeMatrix Generates the matrix; requires a payload
.renderer(renderer) a builder with a renderer Stores a renderer for a later call
.render(renderer?) renderer output Generates the matrix and invokes a supplied or stored renderer

The builder’s types prevent .matrix() or .render() before a payload is set, and prevent .render() without either a stored or directly supplied renderer.

Use QRCodeBuilder without type arguments to annotate a payload-configured builder in application helpers. Its default type accepts builders with or without a stored renderer; supply a renderer when calling .render() through that general type.

TypeScript checks matrix option names, types, and supported literal values. Payload format and capacity are checked when the matrix is first requested.

.matrix() returns a read-only QRCodeMatrix with flat, row-major module data. size is the number of modules per side. modules is a frozen array with 1 for dark and 0 for light modules, and roles is a frozen array with the role of every module, such as data, finder, or timing. The module at column x and row y is at index y * size + x in both arrays. See Module roles for every role.

import {type QRCodeMatrix, qrcode} from '@qrcodesdk/core';
const matrix: QRCodeMatrix = qrcode('custom output').matrix();
const isDark = (x: number, y: number) => matrix.modules[y * matrix.size + x] === 1;

A QRCodeRenderer<TOutput> receives that matrix and returns TOutput:

import {type QRCodeRenderer, qrcode} from '@qrcodesdk/core';
const jsonRenderer: QRCodeRenderer<string> = ({size, modules}) =>
JSON.stringify({size, modules: Array.from(modules)});
const direct = qrcode('renderer output').render(jsonRenderer);
const stored = qrcode('renderer output').renderer(jsonRenderer).render();

Built-in renderers use the same function signature. Custom renderers define their output geometry, styling, validation, and return type. See Custom renderers.

Support QRCodeSDK

If you find QRCodeSDK useful, please consider sponsoring the project!