Skip to content

Builder API

@qrcodesdk/core is the foundation of QRCodeSDK. It turns data into a QR code matrix and lets renderers decide how that matrix becomes SVG, terminal text, PNG, DOM output, or any custom format.

Install it when you need:

  • qrcode() and the immutable builder API
  • SVG output
  • terminal text output
  • matrix output
  • custom renderers

Start with qrcode() and chain the options you need.

import {SVGQRCodeRenderer, qrcode} from '@qrcodesdk/core';
const svg = qrcode('https://qrcodesdk.dev')
.mode('octet')
.errorCorrection('M')
.version(4)
.mask(2)
.renderer(SVGQRCodeRenderer())
.render();

You can also create the builder first and provide data later.

import {QRCodeTextRenderer, qrcode} from '@qrcodesdk/core';
const terminalRenderer = QRCodeTextRenderer({size: 1, margin: 2});
const output = qrcode()
.data('HELLO WORLD')
.mode('alphanumeric')
.renderer(terminalRenderer)
.render();

QRCodeSDK accepts string and number input.

qrcode('https://qrcodesdk.dev');
qrcode(1234567890);

Use a mode when you know the shape of your data.

Mode Best for
numeric Digits only.
alphanumeric Uppercase QR alphanumeric data such as HELLO WORLD.
octet UTF-8 text, URLs, JSON, emoji, and general byte data.

If you do not provide a mode, the builder resolves one from the input.

The builder is immutable. Each method returns a new builder with the updated option.

Method Description
.data(value) Sets QR input data.
.mode(mode) Sets the mode: numeric, alphanumeric, or octet.
.errorCorrection(level) Sets the error correction level: L, M, Q, or H. Defaults to M.
.version(version) Pins a QR version from 1 to 40.
.mask(mask) Pins a mask from 0 to 7.
.matrix() Returns the generated QRCodeMatrix.
.renderer(renderer) Stores a renderer for a later .render() call.
.render(renderer?) Generates the matrix and returns renderer output.

The default error correction level is M.

qrcode('https://qrcodesdk.dev').errorCorrection('H');

Available levels are L, M, Q, and H. Higher levels can survive more damage, but they reduce capacity and can require a larger QR version.

Most applications should let the builder choose the version and mask automatically.

const matrix = qrcode('HELLO WORLD').mode('alphanumeric').version(1).mask(2).matrix();

Versions range from 1 to 40. Masks range from 0 to 7.

Use .matrix() when you want to build your own renderer or inspect the QR code directly.

import {type QRCodeMatrix, qrcode} from '@qrcodesdk/core';
const matrix: QRCodeMatrix = qrcode('custom renderer').matrix();

A matrix is a two-dimensional array of modules. 1 means dark and 0 means light.

A renderer is a function that receives a matrix and returns any output type.

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

Renderers can be passed directly to .render(renderer) or stored with .renderer(renderer).render().

@qrcodesdk/core includes runtime-neutral renderers:

For runtime-specific output, add: