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 fromUse it for
starknet-tokenkit/keon-walletThe Keon browser extension and Keon's in-app dApp browser on mobile. Both inject window.starknet_keon.
starknet-tokenkit/keon-wallet/mobileA dApp opened in a phone's own browser (Chrome, Safari), or on a desktop with no extension. It connects over WalletConnect.
Info
`@keon-wallet/sdk` was the old standalone package. It was merged into `starknet-tokenkit` in 3.3.0 and gets no more updates. The functions keep their names, so moving over means changing the install and the import path. The last section of this page shows how.

Install

Code
bash
1npm install starknet-tokenkit starknet

For the StarknetKit modal or mobile connections, also install the optional peers:

Code
bash
1npm install starknetkit @walletconnect/sign-client

starknet-tokenkit 3.5 works with starknet 6, 7 or 8 and starknetkit 3.

Pick your path

Your dApp runs inUseGuide
A desktop browser with the Keon extensionstarknet-tokenkit/keon-wallet, or StarknetKit / starknet-reactThis page, or StarknetKit and starknet-react
Keon's in-app browser on AndroidThe same code as the extension: Keon injects the wallet there tooSame as above
Chrome or another browser on an Android phonestarknet-tokenkit/keon-wallet/mobile, over WalletConnect with deep linksConnect from Android

Connect to the extension

Code
ts
1import { detectKeonWallet, connect, getAccount, WalletNotFoundError } from 'starknet-tokenkit/keon-wallet'
2 
3try {
4 // Waits up to 3 seconds for the extension to inject itself
5 await detectKeonWallet()
6} catch (err) {
7 if (err instanceof WalletNotFoundError) {
8 // Show a "Get Keon" link to https://keon.tokenkithq.io
9 }
10 throw err
11}
12 
13// Opens Keon's connect prompt
14const { isConnected, address, chainId } = await connect()
15 
16// The connected account
17const 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, declareAndDeploy and deployAccount;
  • 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

FunctionWhat 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.init returns the injected connector inside Keon's in-app browser and a WalletConnect connector everywhere else:
Code
ts
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.com
15 },
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:

Code
ts
1import { subscribe, subscribeAll } from 'starknet-tokenkit/keon-wallet'
2 
3const stop = subscribeAll({
4 accountsChanged: (accounts) => {
5 // The user switched accounts in Keon
6 },
7 networkChanged: (network) => {
8 // The user switched networks
9 },
10 disconnect: () => {
11 // The user disconnected your dApp
12 },
13})
14 
15// Later, for example when your component unmounts
16stop()

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:

ErrorWhen it happens
WalletNotFoundErrorKeon is not installed, or did not inject within the timeout
WalletNotConnectedErrorYou called getAccount() before connect()
RequestTimeoutErrorA 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:

Code
ts
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 link
8 } else if ((err as { code?: number }).code === RPC_ERROR_CODES.USER_REJECTED) {
9 // The user rejected: show "Connection cancelled", not an error
10 } else {
11 // Something else went wrong
12 }
13}
CodeNameMeaning
4001USER_REJECTEDThe user rejected the request
4100UNAUTHORIZEDYour dApp is not connected, or lacks the permission
4200UNSUPPORTED_METHODKeon does not support this method
4900DISCONNECTEDKeon is disconnected
4901CHAIN_DISCONNECTEDKeon is not connected to the requested chain
-32602INVALID_PARAMSThe request parameters are invalid
-32603INTERNAL_ERRORSomething failed inside Keon

Moving from @keon-wallet/sdk

Code
bash
1npm uninstall @keon-wallet/sdk
2npm install starknet-tokenkit

Then change the imports:

BeforeAfter
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.