Skip to content

Quickstart

This guide will walk you through setting up your project, creating a smart account directly from a local key (without the embedded wallet widget), and executing a transaction.

We will use the @0xgasless/smart-account package and viem.

Create a new directory and initialize the project:

Terminal window
mkdir my-smart-account-app
cd my-smart-account-app
npm init -y

Install the dependencies:

Terminal window
npm install viem @0xgasless/smart-account
npm install --save-dev typescript @types/node ts-node

Create a tsconfig.json:

Terminal window
npx tsc --init

Update your package.json to allow ES modules:

{
"name": "my-smart-account-app",
"version": "1.0.0",
"type": "module",
"scripts": {
"start": "ts-node src/index.ts"
}
}

Create a src directory and an index.ts file inside it. Add the following code to initialize the smart account signer using a local private key.

Note: Never commit private keys to GitHub. We use a hardcoded one here for demonstration purposes, but you should use environment variables (process.env.PRIVATE_KEY) in production.

import { createSmartAccountClient } from "@0xgasless/smart-account";
import { privateKeyToAccount } from "viem/accounts";
import { parseAbi, encodeFunctionData } from "viem";
const API_KEY = "YOUR_API_KEY"; // Replace with your actual key
const BUNDLER_URL = "https://bundler.0xgasless.com/11155111"; // Sepolia
const PAYMASTER_URL = `https://paymaster.0xgasless.com/v1/11155111/rpc/${API_KEY}`;
// Generate a random local key for testing
const EOA = privateKeyToAccount("0x...");
async function main() {
console.log("Setting up smart account...");
// Create the smart account client
const smartAccount = await createSmartAccountClient({
signer: EOA,
bundlerUrl: BUNDLER_URL,
paymasterUrl: PAYMASTER_URL,
chainId: 11155111,
});
const address = await smartAccount.getAddress();
console.log("Smart Account deployed (or counterfactual) address:", address);

Let’s mint a free NFT on Sepolia to test our gasless setup.

console.log("Preparing transaction...");
const nftContractAddress = "0xYourTestNFTAddress"; // Replace with a verifiable testnet contract
const abi = parseAbi(["function safeMint(address to)"]);
// Encode the contract call
const callData = encodeFunctionData({
abi,
functionName: "safeMint",
args: [address], // Minting the NFT to the smart account itself
});
console.log("Sending UserOperation to the Bundler...");
const userOpResponse = await smartAccount.sendTransaction({
to: nftContractAddress,
data: callData,
value: 0n,
});
const hash = await userOpResponse.waitForTxHash();
console.log("UserOperation Hash:", hash);
const receipt = await userOpResponse.wait();
console.log("Transaction mined! Receipt:", receipt.receipt.transactionHash);
}
main().catch(console.error);

Execute the script:

Terminal window
npm start

If successful, you will see the smart account address and the transaction hash logged to your console. Since we provided a paymasterUrl, the user pays zero gas — 0xGasless sponsors it through your Paymaster policy.

  • Learn how Bundlers process these UserOperations.
  • Understand how Paymasters sponsor the transactions you just sent.