Introduction to Starknet.js

Starknet.js is a JavaScript/TypeScript library that lets you interact with Starknet, the permissionless Validity-Rollup (ZK-Rollup) that scales Ethereum. Whether you're building a wallet, a dApp, or a backend service, Starknet.js provides the tools to connect to Starknet nodes, manage accounts, and call smart contracts.

This guide introduces the core concepts and walks you through the first steps of using Starknet.js in your project.

What is Starknet.js?

Starknet.js is the primary JavaScript SDK for Starknet. It abstracts away the complexities of the Starknet API and provides a developer-friendly interface for common tasks such as:

  • Connecting to Starknet networks (mainnet, testnet, or local devnet)
  • Creating and managing Starknet accounts
  • Reading from and writing to smart contracts
  • Handling transactions and fees
  • Working with Starknet-specific data types like uint256 and felt

It is designed to work in both browser and Node.js environments, making it suitable for a wide range of applications.

Prerequisites

Before you begin, ensure you have:

  • Node.js (version 18 or later recommended) and npm installed
  • Basic knowledge of JavaScript or TypeScript
  • Familiarity with Ethereum concepts such as accounts, transactions, and smart contracts

No prior Starknet experience is required, but understanding the basics of Starknet will help you follow along.

Installation

Install Starknet.js using npm or yarn:

npm install starknet

Or with yarn:

yarn add starknet

Once installed, you can import Starknet.js in your project:

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

Connecting to a Starknet Network

The first step in any Starknet.js application is to establish a connection to a Starknet node. This is done using a Provider. Starknet.js supports both JSON-RPC and Sequencer providers. For most use cases, the JSON-RPC provider is recommended.

const provider = new Provider({
  nodeUrl: 'https://your-rpc-endpoint.com'
});

You can use a public RPC endpoint or run your own node. For development, you might use a local devnet such as starknet-devnet.

Creating an Account

To send transactions, you need an account. Starknet.js provides the Account class, which requires a provider, an address, and a private key (or a signer).

const account = new Account(
  provider,
  '0x123...', // account address
  '0x456...'  // private key
);

For production use, consider using a signer that integrates with your wallet or secure key management system.

Interacting with Smart Contracts

Starknet.js makes it easy to call contract functions. You'll need the contract's ABI and address.

const contract = new Contract(abi, contractAddress, provider);

// Read from a contract
const balance = await contract.balanceOf(account.address);

// Write to a contract (requires an account)
const tx = await contract.transfer(recipient, amount);
await provider.waitForTransaction(tx.transaction_hash);

Starknet.js automatically handles the conversion between JavaScript types and Cairo types.

Next Steps

Now that you have a basic understanding, explore the following guides to deepen your knowledge:

For detailed API references, visit the API documentation.

Frequently Asked Questions

Do I need a Starknet node to use Starknet.js?

You need access to a Starknet node, but you can use a public RPC provider instead of running your own. Many providers offer free tiers for development.

Can I use Starknet.js with TypeScript?

Yes, Starknet.js is written in TypeScript and ships with type definitions. You get full type safety out of the box.

Is Starknet.js compatible with both mainnet and testnet?

Yes, you can connect to any Starknet network by specifying the appropriate RPC endpoint. Testnets like Sepolia are commonly used for development.

How do I handle errors in Starknet.js?

Starknet.js throws errors for failed transactions and RPC issues. Wrap calls in try/catch blocks and inspect the error object for details. The library also provides utility functions for common error scenarios.