Skip to content

JavaScript API Reference

Extends Magika

The main Magika object for Node use (MagikaNode).

Example usage:

import { readFile } from "fs/promises";
import { MagikaNode as Magika } from "magika/node";
const data = await readFile("some file");
const magika = await Magika.create();
const result = await magika.identifyBytes(data);
console.log(result.prediction.output.label);

For a client-side implementation, please import Magika instead.

Note that this MagikaNode class extends Magika, which means that all public Magika APIs (e.g., identifyBytes) are available for MagikaNode as well.

Demos:

  • Node: <MAGIKA_REPO>/js/magika-cli.js, which you can run with yarn run bin -h.
  • Client-side: see <MAGIKA_REPO>/website-ng/src/components/MagikaDemo.svelte

Identifies the content type from a read stream

  • stream ReadStream A read stream.
  • length number Total length of stream data.

Returns MagikaResult An object containing the result of the content type prediction.

Factory method to create a Magika instance.

  • options MagikaOptions The urls or file paths where the model and its config are stored.Parameters are optional. If not provided, the model will be loaded from GitHub.

Returns Promise<MagikaNode>

The main Magika object for client-side use.

Example usage:

const file = new File(["# Hello I am a markdown file"], "hello.md");
const fileBytes = new Uint8Array(await file.arrayBuffer());
const magika = await Magika.create();
const result = await magika.identifyBytes(fileBytes);
console.log(result.prediction.output.label);

For a Node implementation, please import MagikaNode instead.

Demos:

  • Node: <MAGIKA_REPO>/js/magika-cli.js, which you can run with yarn run bin -h.
  • Client-side: see <MAGIKA_REPO>/website-ng/src/components/MagikaDemo.svelte

Identifies the content type of a byte array.

  • fileBytes Uint8Array A fixed-length sequence of bytes.

Returns MagikaResult An object containing the result of the content type prediction.

Returns the sorted list of all possible content type labels that this JavaScript Magika instance can return in MagikaResult.prediction.output.label.

This includes the byte/stream heuristic labels (empty, txt, unknown) and the loaded ML model’s output labels after applying overwrite_map. Unlike the native Rust/Python pipeline, this excludes filesystem-only labels (directory, symlink, unsupported) and labels detected exclusively by the native Rust format resolvers (in_resolvers && !in_ml_model). To inspect all exposed KB labels or filter by Rust pipeline stage coverage, iterate Object.values(ContentTypeLabel) with Magika.getContentTypeInfo(label).

Returns Array<ContentTypeLabel> Sorted list of possible output labels.

Returns the sorted list of content type labels supported by the loaded ML model (excluding labels remapped by overwrite_map).

Most callers should use getOutputContentTypes() instead.

Returns Array<ContentTypeLabel> Sorted list of ML model content type labels.

Factory method to create a Magika instance.

  • options MagikaOptions The urls or file paths where the model and its config are stored.Parameters are optional. If not provided, the model will be loaded from GitHub.

Returns Promise<Magika>

Returns knowledge-base metadata and detection-stage coverage for an exposed Magika content type label.

Note: ContentTypeLabel covers all exposed content types across Magika’s heuristics, native Rust format resolvers, and ML model. Because the JavaScript binding runs byte/stream heuristics and the TensorFlow.js ML model without the native Rust format resolvers, labels covered only by resolvers (in_resolvers && !in_ml_model) are not emitted by identifyBytes / identifyStream; call magika.getOutputContentTypes() on a loaded instance to list the labels this runtime can emit.

  • label (ContentTypeLabel | string) An exposed ContentTypeLabel or string label (e.g., "python").

Returns ContentTypeInfo Metadata and stage coverage for label.