# ic0

> An easy-to-use JavaScript API for the Internet Computer.

Latest version **0.3.2** (published 2024-11-08) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install ic0
pnpm add ic0
yarn add ic0
bun add ic0
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.3.2 |
| Published | 2024-11-08 |
| First published | 2023-01-03 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 45.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 19 |
| Author | DFINITY Foundation |
| Maintainers | rvanasa |
| Keywords | ic, icp, internet-computer, rust, motoko, dfinity, smart-contract, canister, browser, blockchain, cryptocurrency, nft, token |

## Links

- npm: https://www.npmjs.com/package/ic0
- Repository: https://github.com/dfinity/node-ic0
- Homepage: https://github.com/dfinity/node-ic0#readme
- Issues: https://github.com/dfinity/node-ic0/issues
- npm.io page: https://npm.io/package/ic0

## Dependencies (5)

- [cross-fetch](https://npm.io/package/cross-fetch.md) ^3.1.5
- [@dfinity/agent](https://npm.io/package/@dfinity/agent.md) ^2.1.3
- [@dfinity/candid](https://npm.io/package/@dfinity/candid.md) ^2.1.3
- [@dfinity/identity](https://npm.io/package/@dfinity/identity.md) ^2.1.3
- [@dfinity/principal](https://npm.io/package/@dfinity/principal.md) ^2.1.3

## Alternatives

- [@fortawesome/react-fontawesome](https://npm.io/package/@fortawesome/react-fontawesome.md) — 2.2M weekly downloads
- [roboto-fontface](https://npm.io/package/roboto-fontface.md) — 196.0K weekly downloads
- [@react-native-vector-icons/common](https://npm.io/package/@react-native-vector-icons/common.md) — 150.4K weekly downloads
- [stockfish](https://npm.io/package/stockfish.md) — 27.0K weekly downloads
- [@procore/core-icons](https://npm.io/package/@procore/core-icons.md) — 4.6K weekly downloads

## Recent versions

- 0.3.2 (latest) — 2024-11-08
- 0.3.1 — 2024-11-08
- 0.3.0 — 2024-11-07
- 0.2.8 — 2023-09-14
- 0.2.7 — 2023-02-17
- 0.2.6 — 2023-02-03
- 0.2.5 — 2023-02-03
- 0.2.4 — 2023-01-25
- 0.2.3 — 2023-01-19
- 0.2.2 — 2023-01-17
- 0.2.1 — 2023-01-10
- 0.2.0 — 2023-01-09
- 0.1.1 — 2023-01-08
- 0.1.0 — 2023-01-08
- 0.0.5 — 2023-01-06
- … 4 more at https://npm.io/package/ic0/versions

## README

# `ic0` &nbsp;[![npm version](https://img.shields.io/npm/v/ic0.svg?logo=npm)](https://www.npmjs.com/package/ic0) [![GitHub license](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/dfinity/ic0/issues)

> ### An easy-to-use JavaScript API for the [Internet Computer](https://internetcomputer.org/).

---

The `ic0` package is a simple, straightfoward way to interact with canisters running on the [Internet Computer](https://internetcomputer.org/) (IC). 

## Installation

```sh
npm i --save ic0
```

## Quick Start

Try running the following code from Node.js or a web application:

```ts
import ic from 'ic0';

const ledger = ic('ryjl3-tyaaa-aaaaa-aaaba-cai'); // Ledger canister

console.log(await ledger.call('name')); // Call the `name()` method
```

Easily call any Internet Computer canister using the following syntax:

```ts
import ic from 'ic0';

ic(canisterId).call(method, ...args); // IC mainnet

ic.local(canisterId).call(method, ...args); // Local replica
```

## Local Canisters

The [`dfx start`](https://internetcomputer.org/docs/current/references/cli-reference/dfx-start) command hosts a local execution environment for developing canister smart contracts. Here is an example of how to call a local canister:

```ts
const backend = ic.local('rrkah-fqaaa-aaaaa-aaaaq-cai'); // Access a local canister

backend.call('myFunction', 123); // Call `myFunction(123)`
```

### Basic usage:

```ts
const ledger = ic('ryjl3-tyaaa-aaaaa-aaaba-cai'); // Principal for the IC ledger

console.log(await ledger.call('name')); // => { name: 'Internet Computer' }
```

### Advanced usage:

Replica canisters use [agent-js](https://github.com/dfinity/agent-js) behind the scenes.

```ts
import { replica, HttpAgent } from 'ic0';

const ic = replica(new HttpAgent({ ... })); // Use a custom agent from `@dfinity/agent`

const ledger = ic('ryjl3-tyaaa-aaaaa-aaaba-cai');

console.log(await ledger.call('name')); // => { name: 'Internet Computer' }
```

## Mock Canisters

A **mock canister** makes it easy to [mock](https://stackoverflow.com/a/2666006) the behavior of a canister.

### Basic usage:

```ts
import { mockCanister } from 'ic0';

const mock = mockCanister({
    // Mock canister method named `echo()`
    async echo(x: number) {
        return x;
    }
});

console.log(await mock.call('echo', 123)); // => 123
```

### Advanced usage:

Provide a fallback canister and/or compose several mocks by passing a second argument to the `mockCanister()` function:

```ts
import { mockCanister, replicaCanister } from 'ic0';

const ledger = replicaCanister(principal, agent);

const mockLedger = mockCanister({
    async echo(x: number) {
        return x;
    }
}, ledger); // Fall back to the deployed `ledger` canister

const mock = mockCanister({
    async runMock() {
        return this.call('echo', 456); // Call the mocked `echo()` function
    }
}, mockLedger); // Fall back to another mock canister

console.log(await mock.call('runMock')); // => 456
```

## Related Projects

Check out the following GitHub repositories for more IC-related npm packages:

- [agent-js](https://github.com/dfinity/agent-js): a collection of npm packages for building on the Internet Computer
- [node-motoko](https://github.com/dfinity/node-motoko): run Motoko programs directly in the browser
- [mo-dev](https://npmjs.com/package/mo-dev): a live-reload server for local Motoko dapp development
- [@infu/icblast](https://github.com/infu/icblast): a community-built library for exploring the IC and writing integration tests

---
_Source: https://npm.io/package/ic0 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
