We have released the new @icp-sdk/bindgen package, which allows you to generate modern and idiomatic TypeScript bindings for Candid canister interfaces.
Today we released v0.1. There can be breaking changes even in future minor releases, before we reach stability with v1. Keep an eye on the changelog.
What’s New
@icp-sdk/bindgen is a powerful code generation tool that creates type-safe TypeScript bindings for your canisters. It offers three usage modes:
- CLI - Generate bindings from the command line
- Vite Plugin - Automatically generate bindings during development
- Library - Use programmatically in your build scripts
Key Features
- Modern TypeScript support - Generates clean, type-safe code designed to work seamlessly with
@icp-sdk/core - Great DX - Vite plugin with watch/HMR-friendly behavior for instant updates during development
- Flexible integration - Use it as a CLI, Vite plugin, or programmatic library
- Easy migration - Simple migration from
dfx generateanddidc
Quick start
Install
npm i -D @icp-sdk/bindgen
CLI
icp-bindgen --did-file ./service.did --out-dir ./src/bindings
See the CLI docs for more information.
Vite plugin
// vite.config.ts
import { defineConfig } from 'vite';
import { icpBindgen } from '@icp-sdk/bindgen/plugins/vite';
export default defineConfig({
plugins: [
icpBindgen({
didFile: './hello_world.did',
outDir: './src/bindings',
}),
],
});
See the Vite plugin docs for more information.
Library
import { generate } from '@icp-sdk/bindgen/core';
await generate({
didFile: './hello_world.did',
outDir: './src/bindings',
});
See the Library API docs for more information.
Use the generated bindings
import { createActor } from './bindings/hello_world';
const actor = createActor('your-canister-id');
const greeting = await actor.greet('World');
See the Bindings Structure docs page for more information.
Note: Generated code relies on
@icp-sdk/core. See the core docs at js.icp.build/core.
Migrating
Coming from dfx generate or didc? Follow the migration guide.
Docs
- Docs home: js.icp.build/bindgen
- CLI: js.icp.build/bindgen/latest/cli
- Vite plugin: js.icp.build/bindgen/latest/plugins/vite
- Library API: js.icp.build/bindgen/latest/core
- Migrating from dfx or didc: js.icp.build/bindgen/latest/migrating
- Changelog: js.icp.build/bindgen/latest/changelog
Future Work
- v1 release with stable API
- Additional integrations (Webpack, Rollup, etc.), depending on the community’s needs
- Different generation targets (e.g. for Azle) depending on the community’s needs
We’d love to hear your feedback! If you encounter any issues or have suggestions, please open an issue: github.com/dfinity/icp-js-bindgen/issues.