Skip to main content

Starcoin Provider API

Recommended Reading

We recommend that all Web3 site developers read the Basic Usage section.

StarMask injects a global API into websites visited by its users at window.starcoin. This API allows websites to request users' Starcoin accounts, read data from blockchains the user is connected to, and suggest that the user sign messages and transactions. The presence of the provider object indicates an Starcoin user.

We recommend using @starcoin/starmask-onboarding to detect our provider injected at window.starcoin, on any platform or browser.

// This function detects most providers injected at window.starcoin
import StarMaskOnboarding from "@starcoin/starmask-onboarding";

const { isStarMaskInstalled } = StarMaskOnboarding;

if (!isStarMaskInstalled()) {
console.log("Please install StarMask!");

The Starcoin JavaScript provider API is specified by EIP-1193.

Basic Usage

For any non-trivial Starcoin web application — a.k.a. DApp, Web3 site etc. — to work, you will have to:

  • Detect the Starcoin provider (window.starcoin)
  • Detect which Starcoin network the user is connected to
  • Get the user's Starcoin account(s)

The snippet at the top of this page is sufficient for detecting the provider. You can learn how to accomplish the other two by reviewing the snippet in the Using the Provider section.

The provider API is all you need to create a full-featured Web3 application.

Chain IDs

These are the IDs of the Starcoin chains that StarMask supports by default.

0x11Starcoin Main Network (Mainnet)
0xfb251Barnard Test Network
0xfc252Proxima Test Network
0xfd253Halley Test Network
0xfe254Starcoin Dev Network




This property is non-standard. Non-StarMask providers may also set this property to true.

true if the user has StarMask installed.




Note that this method has nothing to do with the user's accounts.

You may often encounter the word "connected" in reference to whether a Web3 site can access the user's accounts. In the provider interface, however, "connected" and "disconnected" refer to whether the provider can make RPC requests to the current chain.

starcoin.isConnected(): boolean;

Returns true if the provider is connected to the current chain, and false otherwise.

If the provider is not connected, the page will have to be reloaded in order for connection to be re-established. Please see the connect and disconnect events for more information.


interface RequestArguments {
method: string;
params?: unknown[] | object;

starcoin.request(args: RequestArguments): Promise<unknown>;

Use request to submit RPC requests to Starcoin via StarMask. It returns a Promise that resolves to the result of the RPC method call.

The params and return value will vary by RPC method. In practice, if a method has any params, they are almost always of type Array<any>.

If the request fails for any reason, the Promise will reject with an Starcoin RPC Error.

StarMask supports most standardized Starcoin RPC methods, in addition to a number of methods that may not be supported by other wallets. See the StarMask RPC API documentation for details.


params: [

method: "txpool.submit_hex_transaction",
.then((result) => {
// The result varies by RPC method.
// For example, this method will return a transaction hash hexadecimal string on success.
.catch((error) => {
// If the request fails, the Promise will reject with an error.


The StarMask provider implements the Node.js EventEmitter API. This sections details the events emitted via that API. There are innumerable EventEmitter guides elsewhere, but you can listen for events like this:

starcoin.on("accountsChanged", (accounts) => {
// Handle the new accounts, or lack thereof.
// "accounts" will always be an array, but it can be empty.

starcoin.on("chainChanged", (chainId) => {
// Handle the new chain.
// Correctly handling chain changes can be complicated.
// We recommend reloading the page unless you have good reason not to.

Also, don't forget to remove listeners once you are done listening to them (for example on component unmount in React):

function handleAccountsChanged(accounts) {
// ...

starcoin.on("accountsChanged", handleAccountsChanged);

// Later

starcoin.removeListener("accountsChanged", handleAccountsChanged);

The first argument of the starcoin.removeListener is the event name and the second argument is the reference to the same function which has passed to starcoin.on for the event name mentioned in the first argument.


interface ConnectInfo {
chainId: string;

starcoin.on('connect', handler: (connectInfo: ConnectInfo) => void);

The StarMask provider emits this event when it first becomes able to submit RPC requests to a chain. We recommend using a connect event handler and the starcoin.isConnected() method in order to determine when/if the provider is connected.


starcoin.on('disconnect', handler: (error: ProviderRpcError) => void);

The StarMask provider emits this event if it becomes unable to submit RPC requests to any chain. In general, this will only happen due to network connectivity issues or some unforeseen error.

Once disconnect has been emitted, the provider will not accept any new requests until the connection to the chain has been re-established, which requires reloading the page. You can also use the starcoin.isConnected() method to determine if the provider is disconnected.


starcoin.on('accountsChanged', handler: (accounts: Array<string>) => void);

The StarMask provider emits this event whenever the return value of the stc_accounts RPC method changes. stc_accounts returns an array that is either empty or contains a single account address. The returned address, if any, is the address of the most recently used account that the caller is permitted to access. Callers are identified by their URL origin, which means that all sites with the same origin share the same permissions.

This means that accountsChanged will be emitted whenever the user's exposed account address changes.



See the Chain IDs section for StarMask's default chains and their chain IDs.

starcoin.on('chainChanged', handler: (chainId: string) => void);

The StarMask provider emits this event when the currently connected chain changes.

All RPC requests are submitted to the currently connected chain. Therefore, it's critical to keep track of the current chain ID by listening for this event.

We strongly recommend reloading the page on chain changes, unless you have good reason not to.

starcoin.on("chainChanged", (_chainId) => window.location.reload());


All errors thrown or returned by the StarMask provider follow this interface:

interface ProviderRpcError extends Error {
message: string;
code: number;
data?: unknown;

The starcoin.request(args) method throws errors eagerly. You can often use the error code property to determine why the request failed.

Using the Provider

This snippet explains how to accomplish the three most common requirements for Web3 sites:

  • Detect the Starcoin provider (window.starcoin)
  • Detect which Starcoin network the user is connected to
  • Get the user's Starcoin account(s)