Ethereum JavaScript API for interacting with the Ethereum blockchain with comprehensive TypeScript support, modular architecture, and plugin extensibility.
—
The main Web3 class serves as the entry point and umbrella for all Ethereum-related functionality. It extends Web3Context to provide a unified interface for blockchain interaction with provider management, module access, and configuration.
Creates a new Web3 instance with optional provider or context configuration.
/**
* Creates a new Web3 instance
* @param providerOrContext - Provider URL, provider instance, or context options
*/
constructor(
providerOrContext?:
| string
| SupportedProviders<EthExecutionAPI>
| Web3ContextInitOptions<EthExecutionAPI, CustomRegisteredSubscription>
);Usage Examples:
import Web3 from 'web3';
// With HTTP provider URL
const web3 = new Web3('https://mainnet.infura.io/v3/YOUR-PROJECT-ID');
// With WebSocket provider
const web3 = new Web3('wss://mainnet.infura.io/ws/v3/YOUR-PROJECT-ID');
// With provider instance
import { HttpProvider } from 'web3-providers-http';
const provider = new HttpProvider('https://localhost:8545');
const web3 = new Web3(provider);
// With context options
const web3 = new Web3({
provider: 'https://mainnet.infura.io/v3/YOUR-PROJECT-ID',
config: {
defaultBlock: 'latest',
defaultGasPrice: '20000000000'
}
});
// Default (uses mainnet provider)
const web3 = new Web3();Access to utility functions, version information, and module constructors.
class Web3 {
/** Package version string */
static version: string;
/** Utility functions for data conversion, validation, and hashing */
static utils: typeof utils;
/** Module constructors for manual instantiation */
static modules: {
Web3Eth: typeof Web3Eth;
Iban: typeof Iban;
Net: typeof Net;
ENS: typeof ENS;
Personal: typeof Personal;
};
/** EIP-6963 provider discovery functions */
static requestEIP6963Providers: typeof requestEIP6963Providers;
static onNewProviderDiscovered: typeof onNewProviderDiscovered;
}Usage Examples:
// Access version
console.log(Web3.version); // "4.16.0"
// Use static utils
const weiValue = Web3.utils.toWei('1', 'ether');
const isValid = Web3.utils.isAddress('0x742C1382...');
// Manual module instantiation
const ethModule = new Web3.modules.Web3Eth(provider);
const ibanInstance = new Web3.modules.Iban('XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS');Access to Ethereum functionality and utilities through the instance.
class Web3 {
/** Ethereum interaction module with extended interface */
eth: Web3EthInterface;
/** Utility functions (same as static utils) */
utils: typeof utils;
}Methods for managing the connection provider.
/**
* Set a new provider for this Web3 instance and all sub-modules
* @param provider - New provider instance or URL
* @returns Boolean indicating success
*/
setProvider(provider: string | SupportedProviders<EthExecutionAPI>): boolean;
/**
* Get the current provider
* @returns Current provider instance or undefined
*/
get currentProvider(): SupportedProviders<EthExecutionAPI> | undefined;
/**
* Get the given provider (from browser environment)
* @returns Given provider or undefined
*/
static get givenProvider(): SupportedProviders<EthExecutionAPI> | undefined;Usage Examples:
const web3 = new Web3('https://mainnet.infura.io/v3/YOUR-PROJECT-ID');
// Change provider
web3.setProvider('wss://mainnet.infura.io/ws/v3/YOUR-PROJECT-ID');
// Check current provider
console.log(web3.currentProvider);
// Check for browser-injected provider
if (Web3.givenProvider) {
const web3 = new Web3(Web3.givenProvider);
}Create and execute multiple RPC calls in a single request.
/**
* BatchRequest constructor for creating batch RPC calls
*/
BatchRequest: typeof Web3BatchRequest;
class Web3BatchRequest {
constructor();
/** Add a request to the batch */
add(request: JsonRpcOptionalRequest): void;
/** Execute all requests in the batch */
execute(): Promise<JsonRpcResponse[]>;
}Usage Examples:
const batch = new web3.BatchRequest();
// Add multiple requests
batch.add({
method: 'eth_getBalance',
params: ['0x742C1382...', 'latest']
});
batch.add({
method: 'eth_getTransactionCount',
params: ['0x742C1382...', 'latest']
});
// Execute batch
const responses = await batch.execute();Access to underlying context and configuration options.
/**
* Get the context object containing configuration and provider
*/
getContextObject(): Web3ContextObject;
/**
* Subscribe to context events
*/
subscribeToContextEvents(context: Web3Context): void;interface Web3ContextInitOptions<API = unknown, RegisteredSubs = unknown> {
provider?: string | SupportedProviders<API>;
config?: Partial<Web3Config>;
registeredSubscriptions?: RegisteredSubs;
}
interface Web3ContextObject {
provider?: SupportedProviders;
config: Web3Config;
requestManager: Web3RequestManager;
subscriptionManager?: Web3SubscriptionManager;
}
type SupportedProviders<API = unknown> =
| EIP1193Provider<API>
| Web3BaseProvider<API>
| HttpProvider
| WebSocketProvider;Install with Tessl CLI
npx tessl i tessl/npm-web3