# Find Subnet ID for a Canister

**URL:** <https://forum.dfinity.org/t/find-subnet-id-for-a-canister/36258>\
**Category:** Developers\
**Created:** [October 17, 2024, 4:28am UTC](https://forum.dfinity.org/t/find-subnet-id-for-a-canister/36258 "2024-10-17T04:28:51Z")\
**Posts on this page:** 3\
**Page:** 1

<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:** [October 17, 2024, 4:28am UTC](https://forum.dfinity.org/t/find-subnet-id-for-a-canister/36258/1 "2024-10-17T04:28:51Z")

</div>

In JavaScript, how can I discover which subnet a canister is running on? Or in other words, where can I find the subnet ID for a given canister ID?

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [October 17, 2024, 8:36am UTC](https://forum.dfinity.org/t/find-subnet-id-for-a-canister/36258/2 "2024-10-17T08:36:49Z")

</div>

This is the one part of the registry I know: You can use [`get_subnet_for_canister`](https://dashboard.internetcomputer.org/canister/rwlgt-iiaaa-aaaaa-aaaaa-cai#get_subnet_for_canister). It returns the subnet that canister id is assigned to if the id is assigned to any subnet. It doesn’t check if the canister actually exists

---

<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:** [October 17, 2024, 11:46am UTC](https://forum.dfinity.org/t/find-subnet-id-for-a-canister/36258/4 "2024-10-17T11:46:01Z")

</div>

HAHAHA, I didn’t pay attention to the thread and thought you were providing an additional answer to the one about “subnet metadata” we discussed yesterday, @Vivienne. So, I kept going with a solution using Agent-js—without the registry. That’s why I originally replied, mentioning the documentation. 🤣

Anyway, here’s what I ultimately found, and it seems to work (at least locally). Another trick seems to be using AgentJS’s `request` and providing a `paths` equals to `subnet`.

```auto
import {
	CanisterStatus as AgentCanisterStatus,
	AnonymousIdentity,
	type Identity
} from '@dfinity/agent';

export const subnetId = async ({
	canisterId
}: {
	canisterId: string;
}): Promise<string | undefined> => {
	const agent = await getAgent({ identity: new AnonymousIdentity() });

	const path = 'subnet' as const;

	const result = await AgentCanisterStatus.request({
		canisterId: Principal.from(canisterId),
		agent,
		paths: [path]
	});

	const subnet: AgentCanisterStatus.Status | undefined = result.get(path);

	const isSubnetStatus = (
		subnet: AgentCanisterStatus.Status | undefined
	): subnet is AgentCanisterStatus.SubnetStatus =>
		nonNullish(subnet) && typeof subnet === 'object' && 'subnetId' in subnet;

	return isSubnetStatus(subnet) ? subnet.subnetId : undefined;
};

```

I’ll use it in Juno.

> <https://github.com/junobuild/juno/pull/737>
>
> \# Motivation
> 
> Given that Satellites and Orbiters can be spinned in selected su…bnet, we should display the information in the overview.
