Skip to content

API Reference

The package exports two main functions for converting Candid definitions.

didToJs(candid: string): string

Compiles a Candid interface string into a JavaScript module string.

Parameters:

  • candid: The Candid interface definition string.

Returns:

  • A string containing the JavaScript module source.

Output Exports: The generated JavaScript module exports:

  • idlFactory: An IDL.InterfaceFactory function.
  • init: A function that returns initialization arguments.
const js = didToJs("service:{}")
// export const idlFactory ...

didToTs(candid: string): string

Compiles a Candid interface string into a TypeScript declaration string.

Parameters:

  • candid: The Candid interface definition string.

Returns:

  • A string containing the TypeScript declaration source.

Output Includes:

  • Imports from @icp-sdk/core (or legacy @dfinity/agent).
  • _SERVICE interface defining the actor type.
  • idlFactory and init type declarations.
const ts = didToTs("service:{}")
// export interface _SERVICE ...

The package builds for multiple targets to ensure compatibility across environments.

FolderFormatEnvironment
dist/webES ModulesBrowsers
dist/nodejsCommonJSNode.js
dist/bundlerES ModulesBundlers (Webpack, Vite)

Most modern bundlers (Vite, Next.js) will automatically pick up the correct version via package.json exports.