Skip to content

Getting Started

In this guide, we’ll build a command-line AI assistant that can read its own wallet balance and transfer tokens using OpenAI, LangChain, and Agentkit.

  • Node.js installed
  • An OpenAI API Key (OPENAI_API_KEY)
  • A 0xGasless API Key (0XGASLESS_API_KEY) from the Dashboard

Create a new directory and install the necessary packages.

Terminal window
mkdir my-agent
cd my-agent
npm init -y
npm install @0xgasless/agent-sdk @langchain/openai @langchain/core
npm install dotenv

Create a .env file in the root of your project:

OPENAI_API_KEY="sk-..."
0XGASLESS_API_KEY="your-0xgasless-api-key"
AGENT_PRIVATE_KEY="0x..." # Optional: Provide an existing private key, or let the SDK generate one

Create an index.ts file. We will initialize the core AgentKit instance.

import { AgentKit } from "@0xgasless/agent-sdk";
import 'dotenv/config';
async function main() {
// 1. Initialize the core AgentKit
// This creates (or loads) the agent's wallet and connects to the paymaster/bundler.
const agentKit = new AgentKit({
apiKey: process.env['0XGASLESS_API_KEY']!,
privateKey: process.env.AGENT_PRIVATE_KEY, // Uses a random key if omitted
chainId: 11155111, // Sepolia testnet
});
const walletAuth = await agentKit.getWalletAuth();
console.log("Agent's Wallet Address:", await walletAuth.getAddress());

Now we inject the 0xGasless tools into a LangChain agent. We use a pre-built prompt that tells the LLM it has access to a crypto wallet.

import { getLangChainTools } from "@0xgasless/agent-sdk/langchain";
import { ChatOpenAI } from "@langchain/openai";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { MemorySaver } from "@langchain/langgraph";
// 2. Wrap AgentKit tools into LangChain format
const tools = getLangChainTools(agentKit);
// 3. Initialize the LLM
const model = new ChatOpenAI({
modelName: "gpt-4o-mini",
temperature: 0,
});
// 4. Create the ReAct agent
const agent = createReactAgent({
llm: model,
tools: tools,
checkpointSaver: new MemorySaver(),
});

Finally, let’s send a natural language prompt to the agent.

console.log("Sending prompt to agent...");
const config = { configurable: { thread_id: "Session1" } };
const stream = await agent.stream(
{ messages: [["user", "What is your wallet address and native balance?"]] },
config
);
for await (const chunk of stream) {
if ("agent" in chunk) {
console.log("Agent:", chunk.agent.messages[0].content);
} else if ("tools" in chunk) {
console.log("[Tool Execution]:", chunk.tools.messages[0].name);
}
}
}
main().catch(console.error);

Run the script. The agent will read your prompt, realize it needs to use the GetAddressAction and GetBalanceAction tools, execute them on-chain, and return the result to you in simple English.

Terminal window
npx ts-node index.ts

Output:

Agent's Wallet Address: 0x...
Sending prompt to agent...
[Tool Execution]: get_address
[Tool Execution]: get_balance
Agent: My wallet address is 0x... and my native balance is currently 0 ETH.