Skip to content

@ic-reactor/parser

The @ic-reactor/parser package is a high-performance WASM-based parser for the DFINITY Candid language. It compiles Candid interface definitions (.did files) into JavaScript and TypeScript bindings directly in the browser or Node.js environment.

While @ic-reactor/candid handles the fetching and interaction logic, this package provides the raw parsing power.

  • Fast & Local — No need to call a remote didjs canister to compile Candid.
  • Offline Capable — Parse Candid strings entirely on the client side.
  • Type Generation — Generate TypeScript interfaces on the fly.

Candid to JS

Compiles Candid strings to idlFactory and init exports for HttpAgent.

Candid to TS

Generates full TypeScript type definitions from Candid.

WASM Power

Built with Rust and compiled to WebAssembly for performance.

Terminal window
npm install @ic-reactor/parser

Import the parser and use the synchronous methods didToJs or didToTs.

import { didToJs, didToTs } from "@ic-reactor/parser"
const candid = "service:{icrc1_name:()->(text) query;}"
// Convert to JavaScript (idlFactory)
const js = didToJs(candid)
console.log(js)
// Output:
// export const idlFactory = ({ IDL }) => {
// return IDL.Service({ 'icrc1_name' : IDL.Func([], [IDL.Text], ['query']) });
// };
// export const init = ({ IDL }) => { return []; };
// Convert to TypeScript definitions
const ts = didToTs(candid)
console.log(ts)
// Output:
// import type { Principal } from '@icp-sdk/core/principal';
// ...
// export interface _SERVICE { 'icrc1_name' : ActorMethod<[], string> }
// ...

This package is designed to work seamlessly with @ic-reactor/candid. When you initialize a CandidAdapter or CandidReactor, it can automatically use this parser if it’s installed/loaded.

import { createCandidAdapter } from "@ic-reactor/core"
const adapter = createCandidAdapter({ agentManager })
// This internally loads the WASM from @ic-reactor/parser if available
await adapter.initializeParser()
const { idlFactory } = adapter.parseDidToJs("service:{}")