Skip to content

getClient

Action for getting a configured Client instance for interacting with StarkNet.

Import

import { getClient } from "starkweb/core";

Parameters

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

chainId

Hex | undefined

ID of chain to get client for. If not provided, uses the current chain.

index.tsx
import { getClient } from "starkweb/core";
import { mainnet } from "starkweb/chains"; 
 
const client = getClient(config, {
  chainId: mainnet.chain_id, 
});

Return Value

import { type GetClientReturnType } from "starkweb/core";
Client<Transport, Chain>;

A configured client instance for interacting with StarkNet.

Usage

index.tsx
import { getClient } from "starkweb/core";
import { config } from "./config";
 
const client = getClient(config);

Example

example.ts
import { getClient } from "starkweb/core";
import { config } from "./config";
import { mainnet } from "starkweb/chains";
 
// Get client for specific chain
const client = getClient(config, {
  chainId: mainnet.chain_id, 
});
 
// Use client
const blockNumber = await client.getBlockNumber(); 

Error

Throws when:

  • Chain is not configured (ChainNotConfiguredError)
  • Transport is not found (TransportNotFoundError)

TanStack Query

import {
  type GetClientData,
  type GetClientQueryFnData,
  type GetClientQueryKey,
  getClientQueryKey,
  getClientQueryOptions,
} from "starkweb/query";

Query Key

["client", { chainId: Hex }] as const;

Query Options

query.ts
import { getClientQueryOptions } from "starkweb/query";
import { config } from "./config";
 
const queryOptions = getClientQueryOptions(config);