Starknet.js Modules

Starknet.js is a JavaScript library for interacting with Starknet, the Layer 2 network that scales Ethereum with STARK proofs. The library is organized into modules, each responsible for a distinct area of functionality—from account management and contract interaction to provider communication and cryptographic utilities. This page explains how those modules are structured, how to import them, and how to use them effectively in your projects.

Why Starknet.js Is Modular

Starknet’s design emphasizes scalability, integrity, and composability. Starknet.js mirrors those principles by keeping its codebase modular. Instead of a single monolithic bundle, functionality is split into focused modules that you can import individually. This approach offers several benefits:

  • Smaller bundles: Import only what you need, reducing the footprint of your frontend or backend application.
  • Clear separation of concerns: Each module has a well-defined responsibility, making the library easier to learn and reason about.
  • Composability: Modules can be combined to build complex workflows—such as deploying an account, calling a contract, and verifying a transaction—without unnecessary coupling.
  • Easier upgrades: When migrating between major versions, you can update module imports incrementally.

Core Modules at a Glance

While the exact module list evolves with each release, the following categories cover the primary areas you will work with when using Starknet.js:

  • Provider: Communicates with Starknet nodes to read chain state, estimate fees, and retrieve transaction receipts.
  • Account: Manages key pairs, signs transactions, and sends them to the network. Account abstraction is a first-class concept in Starknet, and this module reflects that.
  • Contract: Provides a typed interface for calling and invoking smart contract functions using ABI definitions.
  • Signer: Handles signing operations, including support for different signature schemes and external signers.
  • Utils: Offers helper functions for common tasks such as formatting values, computing hashes, and encoding data.
  • Constants and Types: Exposes network-specific constants and TypeScript types to keep your code consistent and type-safe.

For the most current and detailed list of modules, refer to the API reference section of the documentation, which is generated from the source code and updated with each release.

Importing Modules

Starknet.js supports both CommonJS and ES module imports. The recommended approach depends on your project setup.

ES Modules

import { Provider, Account, Contract } from "starknet";

CommonJS

const { Provider, Account, Contract } = require("starknet");

If you are working in a browser environment without a bundler, you can load Starknet.js from a CDN such as jsDelivr. The package is published on npm as starknet, and CDN URLs follow the standard npm package pattern. Always pin a specific version in production to avoid unexpected breaking changes.

Practical Guidance for Using Modules

When building with Starknet.js, keep the following practices in mind:

  • Start with a provider: Most workflows begin by creating a provider instance connected to the network you target (mainnet or a testnet).
  • Separate read and write operations: Use the provider for read-only calls and an account instance for transactions that modify state.
  • Leverage TypeScript types: The library ships with type definitions. Importing types from the relevant modules helps catch errors early.
  • Handle version migrations carefully: Major versions may reorganize modules or change import paths. Review the migration guide when upgrading.
  • Compose modules for complex flows: For example, combine the account, contract, and utils modules to deploy a contract, invoke a function, and format the resulting transaction hash.

Frequently Asked Questions

What is the difference between a module and a class in Starknet.js?

Modules are organizational units that group related exports. Classes such as Provider and Account are exported from those modules. You import the class from the module, then instantiate it in your code.

Can I import everything from the root package?

Yes. The root package re-exports the public API, so you can import multiple classes and functions in a single statement. For tree-shaking benefits, import from specific subpaths when your bundler supports it.

Where can I find the full module list?

The API reference in the documentation is generated from the source and lists every module, class, and function available in the current release. It is the authoritative source for module names and signatures.

How do I know which version of the modules I am using?

Check the version number in your package.json or the release notes on GitHub. The documentation includes a version selector so you can view the module structure for the release you depend on.

Are modules compatible across Starknet.js versions?

Module organization can change between major versions. Always consult the migration guide when moving to a new major release, and test your imports in a staging environment before deploying.