Your First Query with the TRUF.NETWORK JavaScript SDK
Connect to your local TRUF node, run a live index query, and match it to the TRUFScan Explorer UI.
From Node Setup to First Query
In Part 1, you spun up your local TRUF.Network node. Now it's time to connect to it using the JavaScript SDK and pull your first piece of decentralized economic data – one you can see live on the TRUFScan Explorer and Truflation.com.
This marks your first step into decentralized data entrepreneurship.
Step 1: Install the SDK
npm install @trufnetwork/sdk-js
# or
yarn add @trufnetwork/sdk-js
# or
pnpm install @trufnetwork/sdk-js
Optional: Use TypeScript? The SDK includes full types.
🔌 Step 2: Connect to Your Local Node
Create a file called index.js
:
import {
NodeTNClient,
StreamId,
EthereumAddress
} from "@trufnetwork/sdk-js";
import { Wallet } from "ethers";
// Create a wallet.
const wallet = Wallet.createRandom();
// Initialize client for Node.js
const client = new NodeTNClient({
// Use the mainnet gateway or your own local node endpoint
endpoint: "http://localhost:8484",
signerInfo: {
address: wallet.address,
signer: wallet, // Any object that implements signMessage
},
chainId: "tn-v2", // or use NodeTNClient.getDefaultChainId(endpoint)
});
🔎 Step 3: Query the AI Index
Let’s query the Truflation AI Index, a signal of AI’s effect on economic dynamics.
Add this:
// Create a stream locator for the AI Index
const aiIndexLocator = {
streamId: StreamId.fromString("st527bf3897aa3d6f5ae15a0af846db6").throw(),
dataProvider: EthereumAddress.fromString(
"0x4710a8d8f0d845da110086812a32de6d90d7ff5c"
).throw(),
};
// Load the action client
const streamAction = client.loadAction();
(async () => {
// Get the latest records
const records = await streamAction.getRecord({
stream: aiIndexLocator,
});
console.log(records);
})();
Run with:
node index.js
Expected output:
[ { eventTime: '1750291200', value: '91.773592818900000000' } ]
🔗 View it visually on the TRUF Explorer
🧪 Try It Live on CodeSandbox
Click below to run this code now, even if you haven’t set up a node yet (you’ll hit a 404 but the flow will be real):
👉 Open in CodeSandbox
https://codesandbox.io/p/devbox/6y9kkh
(This is AI generated if you’re wondering!)
You Just Did This
- Connected to a local node
- Queried a real-time, decentralized data index
- Verified it with a visual on-chain frontend
Next step? Build your own index.
Bonus: Explore More Indexes
Try replacing 'truf-ai-index'
with:
Index ID | Description | Explorer |
---|---|---|
us-cpi |
US inflation rate | Link |
weatherxm-temp-mexico |
Mexico weather via WeatherXM | Link |
You're not just using the network – you're helping build it.
📚 Dev Resources
- JavaScript SDK Docs
- Go SDK
- TRUF Explorer
- TRUF.NETWORK
- Truflation.com
- Node Operator Guide
Join the Community
Have questions? Want to share your first index?
Join our Discord and let us know what you're building.