Establish a Connection
After detecting the provider, a dApp can request accounts from StarKey via the standard EIP-1193 request method. This prompts the user to approve the connection and share their address. Once approved, the site is remembered by StarKey for future visits until the user revokes it.
Connecting
Call eth_requestAccounts to prompt the user to connect:
const provider = window.starkey?.ethereum ?? window.ethereum // see "Detecting the Provider"
try {
const accounts: string[] = await provider.request({ method: 'eth_requestAccounts' })
console.log(accounts[0])
// 0x709979...9B685
} catch (err) {
// { code: 4001, message: 'User rejected the request.' }
}Returns: Promise<string[]> — every connected address, with the active address at index 0. Rejects if the user declines the prompt (see Errors).
Checking an existing connection
To read already-approved accounts without prompting the user (e.g. on page load), call eth_accounts instead:
const accounts: string[] = await provider.request({ method: 'eth_accounts' })
if (accounts.length > 0) {
// already connected — safe to skip eth_requestAccounts
}eth_accounts resolves to an empty array [] if the site has no active connection — it never prompts the user.
Disconnecting
EIP-1193 has no disconnect method a dApp can call — the user revokes a site’s connection from the StarKey UI directly. When that happens, StarKey emits a disconnect event (see Events); clear your own local wallet state (accounts, chainId, etc.) in that handler.
Changing accounts
StarKey supports multiple accounts per extension/mobile app. When the user switches the active account from the StarKey UI, it emits accountsChanged:
provider.on('accountsChanged', (accounts: string[]) => {
if (accounts.length > 0) {
console.log(`Switched to account ${accounts[0]}`)
} else {
// all accounts disconnected — treat like a disconnect
}
})See Events for the full event catalog (accountsChanged, chainChanged, connect, disconnect).
Related pages
- Get started with EVM networks
- Network
- Events
- Errors — connection rejection handling