# How to make IC Management API canister\_status call with @dfinity/agent?

**URL:** <https://forum.dfinity.org/t/how-to-make-ic-management-api-canister-status-call-with-dfinity-agent/7139>\
**Category:** Developers\
**Created:** [September 10, 2021, 2:35pm UTC](https://forum.dfinity.org/t/how-to-make-ic-management-api-canister-status-call-with-dfinity-agent/7139 "2021-09-10T14:35:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![heldrida](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/heldrida/32/2302_2.png) [@heldrida](https://forum.dfinity.org/u/heldrida)\
**Post date:** [September 10, 2021, 2:35pm UTC](https://forum.dfinity.org/t/how-to-make-ic-management-api-canister-status-call-with-dfinity-agent/7139/1 "2021-09-10T14:35:38Z")

</div>

Hi,

I’d like to find a working example of a client-side call to the IC Management API, canister\_status endpoint, using the @dfinity/agent library.

Currently, I get the following error:

```auto
Uncaught (in promise) Error: Call was rejected:
  Request ID: fcd619917bbae7a933d8d1333d5ad154d42cc5087f933885a7802c9f9663ce35
  Reject code: 
  Reject text: Canister xxxxx has no update method 'canister_status'

```

Used a partial idl factory for the ic management API:

```auto
      const idlFactory = ({ IDL }) => {
        // eslint-disable-next-line camelcase
        const canister_id = IDL.Principal;
        // eslint-disable-next-line camelcase
        const definite_canister_settings = IDL.Record({
          freezing_threshold: IDL.Nat,
          controllers: IDL.Vec(IDL.Principal),
          memory_allocation: IDL.Nat,
          compute_allocation: IDL.Nat,
        });
        return IDL.Service({
          canister_status: IDL.Func(
            [IDL.Record({ canister_id })],
            [
              IDL.Record({
                status: IDL.Variant({
                  stopped: IDL.Null,
                  stopping: IDL.Null,
                  running: IDL.Null,
                }),
                memory_size: IDL.Nat,
                cycles: IDL.Nat,
                settings: definite_canister_settings,
                module_hash: IDL.Opt(IDL.Vec(IDL.Nat8)),
              }),
            ],
            [],
          ),
        });
      };

```

The call is made through the @dfinity/agent, with my own identity in which controls the target (effective) canister id. Initially, used the `aaaaa-aa`, but after reading ( [The Internet Computer Interface Specification :: Internet Computer](https://sdk.dfinity.org/docs/interface-spec/index.html#http-effective-canister-id) ), used the target canister id (effective canister id).

A working example would be nice, it’s been a bit boring as I was expecting it to be a bit simpler and the dfx command (ok that the target canister id is not passed as an argument to the call).

```auto
dfx canister --network=ic --no-wallet call aaaaa-aa canister_status "(record { canister_id= principal \"xxxxx\" })"

```

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [March 31, 2022, 12:18pm UTC](https://forum.dfinity.org/t/how-to-make-ic-management-api-canister-status-call-with-dfinity-agent/7139/2 "2022-03-31T12:18:48Z")

</div>

Late to the party but since I implemented this today here’s my solution.

1. Get the interface spec - the [id.did](https://github.com/dfinity/interface-spec/blob/700213318abb873c4db891da06cb3c28ced73638/spec/ic.did) file
2. Generate `did.d.ts` and `did.js` from Candid file, for example with [Canlista](https://k7gat-daaaa-aaaae-qaahq-cai.ic0.app/docs/) UI
3. The typescript code:

```auto
import {CallConfig, Identity} from '@dfinity/agent';
import {Principal} from '@dfinity/principal';
import {_SERVICE as IcManagerActor} from '../../canisters/ic/ic.did';
import {idlFactory as IcManagerFactory} from '../../canisters/ic/ic.utils.did';
import {createActor} from '../../utils/actor.utils';
import {getIdentity} from '../auth/auth.providers';

const MANAGEMENT_CANISTER_ID = Principal.fromText('aaaaa-aa');

// Source nns-dapp - dart -> JS bridge
const transform = (_methodName: string, args: unknown[], _callConfig: CallConfig) => {
  const first = args[0] as any;
  let effectiveCanisterId = MANAGEMENT_CANISTER_ID;
  if (first && typeof first === 'object' && first.canister_id) {
    effectiveCanisterId = Principal.from(first.canister_id as unknown);
  }
  return {effectiveCanisterId};
};

const createIcManagerActor = ({identity}: {identity: Identity}): Promise<IcManagerActor> => {
  return createActor<IcManagerActor>({
    config: {
      canisterId: MANAGEMENT_CANISTER_ID,
      callTransform: transform,
      queryTransform: transform
    },
    idlFactory: IcManagerFactory,
    identity
  });
};

export const canisterStatus = async (canisterId: string) => {
  // authClient?.getIdentity();
  const identity: Identity | undefined = getIdentity();

  if (!identity) {
    throw new Error('No internet identity to get canister status');
  }

  const actor: IcManagerActor = await createIcManagerActor({identity});

  const response = await actor.canister_status({
    canister_id: Principal.fromText(canisterId)
  });

  console.log(response);

  // note: if error 403 => user is not the controller of the canister
};

```

Note: I assume it works out. At the end of the day I am not going to use following code for my app because your identity needs to be the controller for the canister to query it which is not my use case 🤪

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [September 1, 2023, 4:45am UTC](https://forum.dfinity.org/t/how-to-make-ic-management-api-canister-status-call-with-dfinity-agent/7139/3 "2023-09-01T04:45:36Z")

</div>

Update: since few months there is now a JS library - parts of ic-js - for interfacing with the IC: [https://github.com/dfinity/ic-js/tree/main/packages/ic-management](https://github.com/dfinity/ic-js/tree/main/packages/ic-management)

To get the status it’s now as easy as following:

```auto
import { ICManagementCanister } from "@dfinity/ic-management";
import { createAgent } from "@dfinity/utils";

const agent = await createAgent({
  identity,
  host: HOST,
});

const { canisterStatus } = ICManagementCanister.create({
  agent,
});

const { status, memory_size, ...rest } = await canisterStatus(YOUR_CANISTER_ID);

```

---

<div class="post-metadata">

**Author:** ![sea-snake](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/sea-snake/32/7243_2.png) [@sea-snake](https://forum.dfinity.org/u/sea-snake)\
**Post date:** [September 1, 2023, 8:39am UTC](https://forum.dfinity.org/t/how-to-make-ic-management-api-canister-status-call-with-dfinity-agent/7139/4 "2023-09-01T08:39:00Z")

</div>

Is this the same status as you can get from `CanisterStatus`?

> <https://github.com/dfinity/agent-js/blob/ac767561bd50882734c75572c7a0c36b663ffcf7/e2e/node/basic/canisterStatus.test.ts#L4>

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [September 1, 2023, 10:04am UTC](https://forum.dfinity.org/t/how-to-make-ic-management-api-canister-status-call-with-dfinity-agent/7139/5 "2023-09-01T10:04:15Z")

</div>

Not sure about the content but, at least from the response no. That agent-js function returns a Map, the ic-js library returns structured data decoded with candid.

The `@dfinity/ic-management` exposes also more features - i.e. all features available to manage a canister:

- [create](https://github.com/dfinity/ic-js/tree/main/packages/ic-management#gear-create)
- [createCanister](https://github.com/dfinity/ic-js/tree/main/packages/ic-management#gear-createcanister)
- [updateSettings](https://github.com/dfinity/ic-js/tree/main/packages/ic-management#gear-updatesettings)
- [installCode](https://github.com/dfinity/ic-js/tree/main/packages/ic-management#gear-installcode)
- [uninstallCode](https://github.com/dfinity/ic-js/tree/main/packages/ic-management#gear-uninstallcode)
- [startCanister](https://github.com/dfinity/ic-js/tree/main/packages/ic-management#gear-startcanister)
- [stopCanister](https://github.com/dfinity/ic-js/tree/main/packages/ic-management#gear-stopcanister)
- [canisterStatus](https://github.com/dfinity/ic-js/tree/main/packages/ic-management#gear-canisterstatus)
- [canisterInfo](https://github.com/dfinity/ic-js/tree/main/packages/ic-management#gear-canisterinfo)
- [deleteCanister](https://github.com/dfinity/ic-js/tree/main/packages/ic-management#gear-deletecanister)
- [provisionalCreateCanisterWithCycles](https://github.com/dfinity/ic-js/tree/main/packages/ic-management#gear-provisionalcreatecanisterwithcycles)

Shout-out to @NathanosDev for contributing to that particular lib.
