{"id":30703198,"url":"https://github.com/dfinity/canpack","last_synced_at":"2025-09-02T16:57:16.055Z","repository":{"id":223255117,"uuid":"759192643","full_name":"dfinity/canpack","owner":"dfinity","description":"Package multiple libraries into one ICP canister.","archived":false,"fork":false,"pushed_at":"2024-12-03T12:22:34.000Z","size":1523,"stargazers_count":10,"open_issues_count":4,"forks_count":0,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-03-30T07:32:33.273Z","etag":null,"topics":["bundler","candid","canister","cross-language","dfinity","icp","language-bindings","mops","motoko","rust"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/canpack","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dfinity.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-02-17T22:37:42.000Z","updated_at":"2025-01-10T20:49:50.000Z","dependencies_parsed_at":"2024-05-30T19:10:08.833Z","dependency_job_id":null,"html_url":"https://github.com/dfinity/canpack","commit_stats":null,"previous_names":["rvanasa/canpack","dfinity/canpack"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dfinity/canpack","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dfinity%2Fcanpack","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dfinity%2Fcanpack/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dfinity%2Fcanpack/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dfinity%2Fcanpack/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dfinity","download_url":"https://codeload.github.com/dfinity/canpack/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dfinity%2Fcanpack/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273317765,"owners_count":25084037,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-02T02:00:09.530Z","response_time":77,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bundler","candid","canister","cross-language","dfinity","icp","language-bindings","mops","motoko","rust"],"created_at":"2025-09-02T16:57:14.457Z","updated_at":"2025-09-02T16:57:16.040Z","avatar_url":"https://github.com/dfinity.png","language":"TypeScript","readme":"# `canpack` \u0026nbsp; [![npm version](https://img.shields.io/npm/v/canpack.svg?logo=npm\u0026color=default)](https://www.npmjs.com/package/canpack) [![GitHub license](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)\n\nCanpack is a code generation tool which simplifies cross-language communication in [Internet Computer](https://internetcomputer.org/) canisters (such as calling a [Rust](https://www.rust-lang.org/) crate from [Motoko](https://github.com/dfinity/motoko)). This works by generating a separate canister from the host language, combining fragments defined across multiple libraries.\n\n**Note:** This project is early in development; unannounced breaking changes may occur at any time.\n\n## Installation\n\nEnsure that the following software is installed on your system:\n* [dfx](https://support.dfinity.org/hc/en-us/articles/10552713577364-How-do-I-install-dfx) (latest version)\n* [Rust](https://www.rust-lang.org/tools/install) `\u003e= 1.71`\n* [Node.js](https://nodejs.org/en) `\u003e= 18`\n\nRun the following command to install the Canpack CLI on your global system path:\n\n```\nnpm install -g canpack\n```\n\n## Quick Start (Motoko + Rust)\n\nCanpack has built-in support for the [Mops](https://mops.one/) package manager. \n\nIn your canister's `mops.toml` file, add a `rust-dependencies` section:\n\n```toml\n[rust-dependencies]\ncanpack-example-hello = \"^0.1\"\nlocal-crate = { path = \"path/to/local-crate\" }\n```\n\nYou can also specify `[rust-dependencies]` in a Motoko package's `mops.toml` file to include Rust crates in any downstream canisters.\n\nNext, run the following command in the directory with the `mops.toml` and `dfx.json` files:\n\n```bash\ncanpack\n```\n\nThis will configure and generate a `motoko_rust` canister with Candid bindings for the specified dependencies. Here is a Motoko canister which uses a function defined in the [`canpack-example-hello`](https://docs.rs/canpack-example-hello/latest/src/canpack_example_hello/lib.rs.html) crate:\n\n```motoko\nimport Rust \"canister:motoko_rust\";\n\nactor {\n    public composite query func hello(name: Text) : async Text {\n        await Rust.canpack_example_hello(name)\n    } \n}\n```\n\nAny Rust crate with Canpack compatibility can be specified as a standard [`Cargo.toml` dependency](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html). See the [Rust Crates](#rust-crates) section for more details.\n\n## Rust Crates\n\nIt's relatively simple to add Canpack support to any IC Wasm-compatible Rust crate.\n\nHere is the full implementation of the [`canpack-example-hello`](https://docs.rs/canpack-example-hello/latest/src/canpack_example_hello/lib.rs.html) package:\n\n```rust\ncanpack::export! {\n    pub fn canpack_example_hello(name: String) -\u003e String {\n        format!(\"Hello, {name}!\")\n    }\n}\n```\n\nIf needed, you can configure the generated Candid method using a `#[canpack]` attribute:\n\n```rust\ncanpack::export! {\n    #[canpack(composite_query, rename = \"canpack_example_hello\")]\n    pub fn hello(name: String) -\u003e String {\n        format!(\"Hello, {name}!\")\n    }\n}\n```\n\nNote that it is possible to reference local constants, methods, etc.\n\n```rust\nconst WELCOME: \u0026str = \"Welcome\";\n\nfn hello(salutation: \u0026str, name: String) -\u003e String {\n    format!(\"{salutation}, {name}!\")\n}\n\ncanpack::export! {\n    pub fn canpack_example_hello(name: String) -\u003e String {\n        hello(WELCOME, name)\n    }\n}\n```\n\nThe `canpack::export!` shorthand requires adding [`canpack`](https://crates.io/crates/canpack) as a dependency in your Cargo.toml file. It's also possible to manually define Candid methods by exporting a `canpack!` macro:\n\n```rust\npub fn hello(name: String) -\u003e String {\n    format!(\"Hello, {name}!\")\n}\n\n#[macro_export]\nmacro_rules! canpack {\n    () =\u003e {\n        #[ic_cdk::query]\n        #[candid::candid_method(query)]\n        fn canpack_example_hello(name: String) -\u003e String {\n            $crate::hello(name)\n        }\n    };\n}\n```\n\n## Advanced Usage\n\n### `canpack.json`\n\nPass the `-v` or `--verbose` flag to view the resolved JSON configuration for a project:\n\n```bash\ncanpack --verbose\n```\n\nBelow is a step-by-step guide for setting up a `dfx` project with a `canpack.json` config file. The goal here is to illustrate how one could use Canpack without additional tools such as Mops, which is specific to the Motoko ecosystem. \n\nRun `dfx new my_project`, selecting \"Motoko\" for the backend and \"No frontend canister\" for the frontend. Once complete, run `cd my_project` and open in your editor of choice. \n\nAdd a new file named `canpack.json` in the same directory as `dfx.json`. \n\nIn the `canpack.json` file, define a Rust canister named `my_project_backend_rust`:\n\n```json\n{\n    \"canisters\": {\n        \"my_project_backend_rust\": {\n            \"type\": \"rust\",\n            \"parts\": [{\n                \"package\": \"canpack-example-hello\",\n                \"version\": \"^0.1\"\n            }]\n        }\n    }\n}\n```\n\nNext, run the following command in this directory to generate all necessary files: \n\n```bash\ncanpack\n```\n\nIn your `dfx.json` file, configure the `\"dependencies\"` for the Motoko canister:\n\n```json\n{\n    \"canisters\": {\n        \"my_project_backend\": {\n            \"dependencies\": [\"my_project_backend_rust\"],\n            \"main\": \"src/my_project_backend/main.mo\",\n            \"type\": \"motoko\"\n        }\n    },\n}\n```\n\nNow you can call Rust functions from Motoko using a canister import:\n\n```motoko\nimport Rust \"canister:my_project_backend_rust\";\n\nactor {\n    public func hello(name: Text) : async Text {\n        await Rust.canpack_example_hello(name)\n    } \n}\n```\n\nRun the following commands to build and deploy the `dfx` project on your local machine:\n\n```\ndfx start --background\ndfx deploy\n```\n\n### Programmatic API\n\nCanpack may be used as a low-level building block for package managers and other development tools. \n\nAdd the `canpack` dependency to your Node.js project with the following command:\n\n```bash\nnpm i --save canpack\n```\n\nThe following example JavaScript code runs Canpack in the current working directory:\n\n```js\nimport { canpack } from 'canpack';\n\nconst config = {\n    verbose: true,\n    canisters: {\n        my_canister: {\n            type: 'rust',\n            parts: [{\n                package: 'canpack-example-hello',\n                version: '^0.1',\n            }]\n        }\n    }\n};\n\nawait canpack(config);\n```\n\n---\n\nThis project is early in development. Please feel free to report a bug, ask a question, or request a feature on the project's [GitHub issues](https://github.com/dfinity/canpack/issues) page.\n\nContributions are welcome! Please check out the [contributor guidelines](https://github.com/dfinity/canpack/blob/main/.github/CONTRIBUTING.md) for more information.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdfinity%2Fcanpack","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdfinity%2Fcanpack","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdfinity%2Fcanpack/lists"}