Starknet Snap API
When connected to the Starknet Snap, dapps can use the Starknet Snap API to interact with users' Starknet accounts (for example, to send transactions).
The examples on this page use the
wallet_invokeSnap
JSON-RPC method,
which supports all Starknet Snap API methods.
We recommend using EIP-6963 for detecting the MetaMask wallet when using the wallet_invokeSnap
approach. This ensures better interoperability and improved wallet integration.
You can also communicate with the Starknet network using the Starknet API.
Supported networks
Starknet currently supports two public networks. Use these networks' chain IDs with the Starknet Snap API methods.
Network | Chain ID (Hexadecimal) |
---|---|
Mainnet | 0x534e5f4d41494e |
Testnet (Sepolia) | 0x534e5f5345504f4c4941 |
API methods
starkNet_createAccount
Deploys an account contract.
Parameters
addressIndex
:integer
- (Optional) Specific address index of the derived key in BIP-44.deploy
:boolean
- (Optional) Indicate whether to include send the deploy transaction for the account contract. The default isfalse
.chainId
:string
- (Optional) ID of the target Starknet network. The default is the Starknet Sepolia testnet.
Returns
The address of the account and the transaction hash if the account has been created.
Example
- Request
- Result
await provider.request({ // Or window.ethereum if you don't support EIP-6963.
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_createAccount",
params: {
addressIndex: 1,
deploy: true,
chainId: "0x534e5f5345504f4c4941"
},
},
},
})
{
"transaction_hash": "0x05a56e2d52c817161883f50c441c3228cfe54d9f84b5b5b8b1c8b8e0e6f7e6d8",
"address": "0xb60e8dd61c5d32be8058bb8eb970870f07233155"
}
starkNet_declareContract
Registers a contract's class on the Starknet blockchain without deploying it.
Parameters
senderAddress
:string
- Address of the sender.chainId
:string
- (Optional) ID of the target Starknet network. The default is the Starknet Sepolia testnet.contractPayload
:object
- Transaction payload to be deployed.invocationsDetails
:object
- (Optional) Transaction details object containing:nonce
: (Optional) Nonce for the transaction.blockIdentifier
: (Optional) Block identifier for the transaction.maxFee
: (Optional) Maximum gas fee allowed for the transaction. If not specified, the fee is automatically calculated.tip
: (Optional) Additional fee to incentivize miners.paymasterData
: (Optional) Paymaster-related data for the transaction.accountDeploymentData
: (Optional) Data for account deployment.nonceDataAvailabilityMode
: (Optional) Mode for nonce data availability.feeDataAvailabilityMode
: (Optional) Mode for fee data availability.version
: (Optional) The transaction version.resourceBounds
: (Optional) The boundaries on resource consumption during the transaction.skipValidate
:boolean
- (Optional) Skip validation of the transaction.
Returns
The confirmation of sending a transaction on the Starknet contract, which contains:
transaction_hash
:string
- The transaction hash.class_hash
:string
- The computed class hash of compiled contract.
Example
- Request
- Result
await provider.request({ // Or window.ethereum if you don't support EIP-6963.
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_declareContract",
params: {
senderAddress: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
contractPayload: {
contract: {
// The compiled contract code
}
},
chainId: "0x534e5f5345504f4c4941",
},
},
},
})
{
"transaction_hash": "0x05a56e2d52c817161883f50c441c3228cfe54d9f84b5b5b8b1c8b8e0e6f7e6d8",
"class_hash": "0xb60e8dd61c5d32be8058bb8eb970870f07233155"
}
starkNet_displayPrivateKey
Extracts the private key from the deployed Starknet account and displays it in MetaMask.
Parameters
userAddress
:string
- Address of the account contract.chainId
:string
- (Optional) ID of the target Starknet network. The default is the Starknet Sepolia testnet.
Returns
Always returns null
for security reasons.
The private key is only shown in the MetaMask pop-up window.
Example
- Request
- Result
await provider.request({ // Or window.ethereum if you don't support EIP-6963.
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_displayPrivateKey",
params: {
userAddress: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
chainId: "0x534e5f5345504f4c4941"
},
},
},
})
null
starkNet_estimateAccountDeployFee
Gets the estimated gas fee for deploying an account contract.
Parameters
addressIndex
:integer
- (Optional) Specific address index of the derived key in BIP-44.chainId
:string
- (Optional) ID of the target Starknet network. The default is the Starknet Sepolia testnet.
Returns
An object with the following properties (where values are originally bigint
but converted to
base-10 string
format using .toString(10)
):
suggestedMaxFee
:string
- The maximum suggested fee for deploying the contract, in wei.overallFee
:string
- The overall fee for the deployment transaction, in wei.gasConsumed
:string
- The amount of gas consumed during the transaction. The default is0
.gasPrice
:string
- The gas price used for the transaction, in wei. The default is0
.unit
:string
- The unit of the fees and gas values, which iswei
.
Example
- Request
- Result
await provider.request({ // Or window.ethereum if you don't support EIP-6963.
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_estimateAccountDeployFee",
params: {
addressIndex: 0,
chainId: "0x534e5f5345504f4c4941"
},
},
},
})
{
"suggestedMaxFee": "1000000000000000",
"overallFee": "900000000000000",
"gasConsumed": "1000000",
"gasPrice": "1000000000",
"unit": "wei"
}
starkNet_estimateFee
Gets the estimated gas fee for calling a method on any contract.
Parameters
address
:string
- The account address from which the transaction is being made.invocations
:array
- The invocations to estimate the fee for. Each invocation represents a contract call.chainId
:string
- The chain ID of the target Starknet network. If not provided, the default is the Starknet Sepolia testnet.details
:object
- (Optional) The universal details associated with the invocations, such as nonce and version.
Returns
A promise that resolves to an EstimateFeeResponse
object, which contains the following properties:
suggestedMaxFee
:string
- The maximum suggested fee for the transaction, in wei. This value is originally abigint
and is converted to a base-10string
.overallFee
:string
- The overall fee for the transaction, in wei. This value is originally abigint
and is converted to a base-10string
.gasConsumed
:string
- The estimated amount of gas the transaction uses.gasPrice
:string
- The gas price per unit, represented as a string in wei.unit
:string
- The unit of the fees, typicallywei
.includeDeploy
:boolean
- Whether the transaction includes an account deployment step.
Example
- Request
- Result
await provider.request({ // Or window.ethereum if you don't support EIP-6963.
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_estimateFee",
params: {
address: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
invocations: [
{
entrypoint: "transfer",
calldata: [
"0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
"1000000000000000000"
]
}
],
chainId: "0x534e5f5345504f4c4941",
details: {
nonce: 1,
maxFee: "2000000000000000"
}
},
},
},
});
{
"suggestedMaxFee": "1000000000000000",
"overallFee": "900000000000000",
"gasConsumed": "1000000",
"gasPrice": "1000000000",
"unit": "wei",
"includeDeploy": false
}
starkNet_executeTxn
Signs and executes a transaction.
Parameters
address
:string
- The address of the sender.calls
: An array of call objects to be executed. Each call contains the target contract address, function name, and call data.details
:object
- (Optional) Transaction details as received bystarknet.js
, including:nonce
: (Optional) Nonce for the transaction.blockIdentifier
: (Optional) Block identifier for the transaction.maxFee
: (Optional) Maximum gas fee allowed for the transaction. If not specified, the fee is automatically calculated.tip
: (Optional) Additional fee to incentivize miners.paymasterData
: (Optional) Paymaster-related data for the transaction.accountDeploymentData
: (Optional) Data for account deployment.nonceDataAvailabilityMode
: (Optional) Mode for nonce data availability.feeDataAvailabilityMode
: (Optional) Mode for fee data availability.version
: (Optional) The transaction version.resourceBounds
: (Optional) The boundaries on resource consumption during the transaction.skipValidate
:boolean
- (Optional) Skip validation of the transaction.
abis
:any
- (Optional) Contract ABI for decoding the call data.chainId
:string
- The Starknet chain ID. Default is Starknet Sepolia testnet.
Returns
The hash of the transaction.
Example
- Request
- Result
await provider.request({ // Or window.ethereum if you don't support EIP-6963.
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_executeTxn",
params: {
address: "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
calls: [
{
entrypoint: "transfer",
calldata: [
"0x1234567890abcdef1234567890abcdef12345678",
"1000000000000000000"
]
}
],
details: {
nonce: 1,
maxFee: "2000000000000000",
},
chainId: "0x534e5f5345504f4c4941"
}
},
},
})
{
"transaction_hash": "0x05a56e2d52c817161883f50c441c3228cfe54d9f84b5b5b8b1c8b8e0e6f7e6d8"
}
starkNet_extractPublicKey
Extracts the public key from a Starknet account address.
Parameters
userAddress
:string
- Address of the account contract.chainId
:string
- (Optional) ID of the target Starknet network. The default network is the Starknet Sepolia testnet.
Returns
The public key associated with the specified account address (which may differ from the signer's public key).
Example
- Request
- Result
await provider.request({ // Or window.ethereum if you don't support EIP-6963.
method: "wallet_invokeSnap",
params: {
snapId: "npm:@consensys/starknet-snap",
request: {
method: "starkNet_extractPublicKey",
params: {
userAddress: "0xb60e8dd61c5d32be8058bb8eb970870f07233155",
chainId: "0x534e5f5345504f4c4941"
},
},
},
})
"0x04bfcab3b7ca7e8b3f3b62b2f7f77e9e4b68080bbf8f0f4a1c8f890864d2c7c1d3c45d8b2e3f5f1c27dfeea4c2f5733e90bfc7484e2a690aa9b8ac4559d2e6a8d7"