Skip to content

getChainId

Action for getting the current Starknet chain ID.

Import

import { getChainId } from "starkweb/core";

Usage

index.ts
import { getChainId } from "starkweb/core";
import { config } from "./config";
 
const chainId = getChainId(config); 
 
// Check if on mainnet
if (chainId === mainnet.chain_id) {
  console.log("Connected to mainnet");
}

Return Type

import { type GetChainIdReturnType } from "starkweb/core";

Hex

The chain ID of the current network in hexadecimal format.

Example

example.ts
import { getChainId } from "starkweb/core";
import { mainnet, testnet } from "starkweb/chains"; 
import { config } from "./config";
 
const chainId = getChainId(config); 
 
// Check which network we're on
switch (chainId) {
  case mainnet.chain_id: 
    console.log("Connected to mainnet");
    break;
  case testnet.chain_id: 
    console.log("Connected to testnet");
    break;
  default:
    console.log("Connected to chain:", chainId);
}