JavaScript API Reference
Table of Contents
Section titled “Table of Contents”MagikaNode
Section titled “MagikaNode”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 withyarn run bin -h. - Client-side: see
<MAGIKA_REPO>/website-ng/src/components/MagikaDemo.svelte
identifyStream
Section titled “identifyStream”Identifies the content type from a read stream
Parameters
Section titled “Parameters”streamReadStream A read stream.lengthnumber Total length of stream data.
Returns MagikaResult An object containing the result of the content type prediction.
create
Section titled “create”Factory method to create a Magika instance.
Parameters
Section titled “Parameters”optionsMagikaOptions 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>
Magika
Section titled “Magika”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 withyarn run bin -h. - Client-side: see
<MAGIKA_REPO>/website-ng/src/components/MagikaDemo.svelte
identifyBytes
Section titled “identifyBytes”Identifies the content type of a byte array.
Parameters
Section titled “Parameters”fileBytesUint8Array A fixed-length sequence of bytes.
Returns MagikaResult An object containing the result of the content type prediction.
getOutputContentTypes
Section titled “getOutputContentTypes”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.
getModelContentTypes
Section titled “getModelContentTypes”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.
create
Section titled “create”Factory method to create a Magika instance.
Parameters
Section titled “Parameters”optionsMagikaOptions 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.
getContentTypeInfo
Section titled “getContentTypeInfo”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.
Parameters
Section titled “Parameters”label(ContentTypeLabel | string) An exposedContentTypeLabelor string label (e.g.,"python").
Returns ContentTypeInfo Metadata and stage coverage for label.