Skip to main content

Manage Starknet networks

You can detect a user's Starknet network and prompt them to switch Starknet networks in MetaMask, using the get-starknet library or the wallet_invokeSnap JSON-RPC method.

Prerequisites

Connect to Starknet from your dapp.

Detect a user's network

Detect the Starknet network a user is currently connected to using the following:

const checkCurrentNetwork = (wallet) => {
try {
if(wallet?.isConnected !== true){
throw("Wallet not connected");
}
const currentNetwork = wallet?.chainId
console.log("Currently connected to:", currentNetwork);
return currentNetwork;
} catch (error) {
console.error("Error of detect current connected network:", error);
}
};

Switch networks

Starknet currently supports two public networks, Mainnet and Sepolia testnet. Prompt users to switch between networks by setting the chain ID of the target network:

const switchChain = async (wallet, chainId) => {
try {
if(wallet?.isConnected !== true){
throw("Wallet not connected");
}

await wallet?.request({
type: "wallet_switchStarknetChain",
params: { chainId: chainId },
});
console.log(`Switched to chainId: ${chainId}`);
} catch (e) {
console.error("Failed to switch chain:", e);
}
};