SDK Integration
Add Keon Wallet to your Starknet dApp with starknet-tokenkit
Keon support for dApps ships inside starknet-tokenkit, the same package as the TokenKit token picker. It has two entry points:
| Import from | Use it for |
|---|---|
starknet-tokenkit/keon-wallet | The Keon browser extension and Keon's in-app dApp browser on mobile. Both inject window.starknet_keon. |
starknet-tokenkit/keon-wallet/mobile | A dApp opened in a phone's own browser (Chrome, Safari), or on a desktop with no extension. It connects over WalletConnect. |
Install
1npm install starknet-tokenkit starknetFor the StarknetKit modal or mobile connections, also install the optional peers:
1npm install starknetkit @walletconnect/sign-clientstarknet-tokenkit 3.5 works with starknet 6, 7 or 8 and starknetkit 3.
Pick your path
| Your dApp runs in | Use | Guide |
|---|---|---|
| A desktop browser with the Keon extension | starknet-tokenkit/keon-wallet, or StarknetKit / starknet-react | This page, or StarknetKit and starknet-react |
| Keon's in-app browser on Android | The same code as the extension: Keon injects the wallet there too | Same as above |
| Chrome or another browser on an Android phone | starknet-tokenkit/keon-wallet/mobile, over WalletConnect with deep links | Connect from Android |
Connect to the extension
1import { detectKeonWallet, connect, getAccount, WalletNotFoundError } from 'starknet-tokenkit/keon-wallet'2 3try {4 // Waits up to 3 seconds for the extension to inject itself5 await detectKeonWallet()6} catch (err) {7 if (err instanceof WalletNotFoundError) {8 // Show a "Get Keon" link to https://keon.tokenkithq.io9 }10 throw err11}12 13// Opens Keon's connect prompt14const { isConnected, address, chainId } = await connect()15 16// The connected account17const account = getAccount()18 19const { transaction_hash } = await account.execute({20 contractAddress: '0x...',21 entrypoint: 'transfer',22 calldata: ['0x...', '100', '0'],23})connect() calls detectKeonWallet() for you, so you can skip the first step. Pass { autoDetect: false } to fail at once instead of waiting, or { detectTimeout: 5000 } to wait longer.
The same code runs unchanged inside Keon's in-app dApp browser on Android, because Keon injects window.starknet_keon there too.
What the account can do
getAccount() throws WalletNotConnectedError until connect() has succeeded. After that it returns an account with:
execute(calls)for invoke transactions, one call or several;signMessage(typedData)for SNIP-12 typed data;declare,deploy,deployContract,declareAndDeployanddeployAccount;- fee estimates and simulation:
estimateInvokeFee,estimateDeclareFee,estimateDeployFee,estimateAccountDeployFee,estimateFeeBulk,simulateTransaction; - reads:
callContract,getNonce,getClassAt,getClassHashAt,getTransactionReceipt,verifyMessage.
Every write opens an approval sheet in Keon. Nothing is signed without the user approving it.
Other helpers
| Function | What it does |
|---|---|
isKeonWalletAvailable() | true if window.starknet_keon exists right now |
getKeonWallet() / tryGetKeonWallet() | The injected wallet object; the first throws WalletNotFoundError, the second returns undefined |
getConnectionState() | { isConnected, address, chainId } without prompting the user |
getProvider() | A read-only provider routed through Keon |
getNetwork() | The network Keon is on: { id, name, chainId, rpcUrl } |
switchNetwork(chainId) | Asks Keon to switch, for example to '0x534e5f5345504f4c4941' (Sepolia) |
disconnect() | Clears your dApp's local state. It does not revoke the permission; the user does that in Keon under Apps > Connected apps |
Connect with StarknetKit, starknet-react or from a phone
- StarknetKit or starknet-react: register Keon as an injected wallet with the id
keon. Step by step, with transactions and signing: StarknetKit and starknet-react. - A phone's own browser: pair with a deep link and let Keon open for each request. Step by step: Connect from Android.
- Both in one StarknetKit modal:
KeonMobileConnector.initreturns the injected connector inside Keon's in-app browser and a WalletConnect connector everywhere else:
1import { connect } from 'starknetkit'2import { InjectedConnector } from 'starknetkit/injected'3import { KeonMobileConnector } from 'starknet-tokenkit/keon-wallet/mobile'4 5const { wallet, connectorData } = await connect({6 connectors: [7 new InjectedConnector({ options: { id: 'keon', name: 'Keon Wallet' } }),8 KeonMobileConnector.init({9 options: {10 dappName: 'My dApp',11 url: window.location.origin,12 icons: ['https://my-dapp.xyz/icon.png'],13 chainId: 'SN_MAIN',14 projectId: 'your-walletconnect-project-id', // from cloud.reown.com15 },16 }),17 ],18 modalMode: 'alwaysAsk',19})React to wallet events
Subscribe to one event with subscribe, or to several with subscribeAll. Both return a function that unsubscribes:
1import { subscribe, subscribeAll } from 'starknet-tokenkit/keon-wallet'2 3const stop = subscribeAll({4 accountsChanged: (accounts) => {5 // The user switched accounts in Keon6 },7 networkChanged: (network) => {8 // The user switched networks9 },10 disconnect: () => {11 // The user disconnected your dApp12 },13})14 15// Later, for example when your component unmounts16stop()The events are accountsChanged, networkChanged, chainChanged, connect, disconnect and wallet_disconnect. Subscribing before the wallet is injected does nothing, so subscribe after detectKeonWallet() or connect().
Handle errors
The SDK throws its own errors for problems on your side of the connection:
| Error | When it happens |
|---|---|
WalletNotFoundError | Keon is not installed, or did not inject within the timeout |
WalletNotConnectedError | You called getAccount() before connect() |
RequestTimeoutError | A request got no answer in time |
All three extend KeonWalletError. Errors from the wallet itself, such as the user tapping Reject, arrive as RPC errors with a numeric code. Compare it with RPC_ERROR_CODES:
1import { connect, WalletNotFoundError, RPC_ERROR_CODES } from 'starknet-tokenkit/keon-wallet'2 3try {4 await connect()5} catch (err) {6 if (err instanceof WalletNotFoundError) {7 // Show the install link8 } else if ((err as { code?: number }).code === RPC_ERROR_CODES.USER_REJECTED) {9 // The user rejected: show "Connection cancelled", not an error10 } else {11 // Something else went wrong12 }13}| Code | Name | Meaning |
|---|---|---|
4001 | USER_REJECTED | The user rejected the request |
4100 | UNAUTHORIZED | Your dApp is not connected, or lacks the permission |
4200 | UNSUPPORTED_METHOD | Keon does not support this method |
4900 | DISCONNECTED | Keon is disconnected |
4901 | CHAIN_DISCONNECTED | Keon is not connected to the requested chain |
-32602 | INVALID_PARAMS | The request parameters are invalid |
-32603 | INTERNAL_ERROR | Something failed inside Keon |
Moving from @keon-wallet/sdk
1npm uninstall @keon-wallet/sdk2npm install starknet-tokenkitThen change the imports:
| Before | After |
|---|---|
from '@keon-wallet/sdk' | from 'starknet-tokenkit/keon-wallet' |
KeonMobileConnector from '@keon-wallet/sdk' | from 'starknet-tokenkit/keon-wallet/mobile' |
InjectedConnector from '@keon-wallet/sdk' | from 'starknetkit/injected', with { options: { id: 'keon', name: 'Keon Wallet' } } |
detectKeonWallet rejects with WalletNotFoundError when Keon is missing; it does not return null. subscribeAll takes an object of handlers, as shown above.
Get listed in the dApps hub
To let users find your dApp from inside Keon, and from any other wallet that uses TokenKit's dApps directory, submit it on tokenkithq.io with a name, a logo, a description and a URL. Once it is approved, it shows up in Keon's Apps tab.
Test your integration
The Keon test dApp is built on exactly this package. It connects three ways (directly through starknet-tokenkit/keon-wallet, through the StarknetKit modal, and over WalletConnect) and exercises every method Keon supports. Open it with the Keon extension installed, or on a phone with the Keon app, to see how each request looks from the wallet's side.