Network
Use these methods to read the currently connected chain and switch the user to a different one.
| Network | Chain ID |
|---|---|
| Mainnet | 8 |
| Testnet | 6 |
See the official Network Information docs for the full chain ID / RPC endpoint list, including any newly added networks.
getChainId()
Returns the currently connected chain.
const network = await window.starkey.supra.getChainId()Returns:
interface ChainId {
chainId: string
}{
chainId: '8'
}Resolves to null when the wallet is not connected.
changeNetwork({ chainId })
Switches the connected network.
const result = await window.starkey.supra.changeNetwork({ chainId: '8' })
if (result === null) {
// user declined the prompt, or StarKey does not recognize this chain ID
return
}
console.log(result.chainId)Parameters:
| Name | Type | Required | Description |
|---|---|---|---|
chainId | string | yes | The chain ID to switch to. Must be a chain StarKey already knows about. |
Returns: Promise<{ chainId: string } | null> — the updated network chain ID, or null if the switch did not happen.
Does not throw. A declined prompt and an unrecognized chain ID both resolve to null.
Because both cases resolve to null, you cannot tell a declined prompt from an unknown chain ID by the return value.
Validate chainId against the table above before calling if you need to distinguish them.
Error handling
changeNetwork()resolves tonull— never rejects — when the user declines the switch or the chain ID is unrecognized. Check fornullbefore reading.chainId.getChainId()resolves tonullrather than throwing when there is no active connection — check fornullbefore reading.chainId.- Adding a network from a dApp is only available on the EVM provider. The Supra provider has no equivalent method — users add custom Supra networks from the StarKey UI instead.
Notes
- Listen for the
networkChangedevent to react when the user switches networks from the StarKey UI directly, rather than through your dApp’s ownchangeNetworkcall. - Re-fetch
accounts(),balance()after a network change — balances and available accounts are chain-scoped.
Best practices
- Always call
getChainId()after connecting to confirm the dApp and wallet agree on the active chain before building a transaction. - Check the result of
changeNetwork()fornullbefore continuing — don’t assume the wallet switched.