Overview
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine and is commonly used to build scalable network applications. Follow the official installation guide to install Node.js. Verify the installation:
node --version
npm --version
This guide builds a Node.js client for the Injective Indexer gRPC API, authenticates with x-token, and calls the Explorer service's GetBlocksV2 method. The completed example requests the latest indexed Mainnet block and prints the response as JSON.
You will:
- Create a Node.js project and install the gRPC libraries.
- Download the official Explorer and Exchange proto definitions.
- Configure your Quicknode endpoint and token.
- Run an authenticated request and verify the response.
Prerequisites
This guide requires Node.js 18 or later and npm.
Step 1: Create the project
Create a project and install the gRPC libraries:
mkdir injective-indexer-grpc-nodejs
cd injective-indexer-grpc-nodejs
npm init -y
npm install @grpc/grpc-js @grpc/proto-loader
mkdir -p proto/explorer proto/exchange
Step 2: Download the proto definitions
Download the official injective_explorer_rpc.proto and injective_exchange_rpc.proto files:
curl -L \
https://raw.githubusercontent.com/InjectiveLabs/injective-proto/master/all_protos/exchange/injective_explorer_rpc.proto \
-o proto/explorer/injective_explorer_rpc.proto
curl -L \
https://raw.githubusercontent.com/InjectiveLabs/injective-proto/master/all_protos/exchange/injective_exchange_rpc.proto \
-o proto/exchange/injective_exchange_rpc.proto
Step 3: Configure the endpoint and token
Given an HTTP Provider URL in this format:
https://YOUR_ENDPOINT_NAME.injective-mainnet.quiknode.pro/YOUR_TOKEN/
set the gRPC endpoint and token as environment variables. Injective Indexer gRPC uses TLS on port 443:
export QN_GRPC_ENDPOINT="YOUR_ENDPOINT_NAME.injective-mainnet.quiknode.pro:443"
export QN_TOKEN="YOUR_TOKEN"
Injective Indexer gRPC requires x-token authentication.
x-token authentication
Attach the token to gRPC metadata and pass the metadata with every call:
const metadata = new grpc.Metadata()
metadata.add('x-token', token)
Step 4: Create the application
Create index.js. This runnable example uses x-token authentication:
const grpc = require('@grpc/grpc-js')
const protoLoader = require('@grpc/proto-loader')
const path = require('path')
const endpoint = process.env.QN_GRPC_ENDPOINT
const token = process.env.QN_TOKEN
if (!endpoint || !token) {
throw new Error('Set QN_GRPC_ENDPOINT and QN_TOKEN')
}
const definition = protoLoader.loadSync(
path.join(__dirname, 'proto/explorer/injective_explorer_rpc.proto'),
{
keepCase: true,
longs: String,
enums: String,
bytes: String,
defaults: true,
oneofs: true,
}
)
const proto = grpc.loadPackageDefinition(definition).injective_explorer_rpc
const client = new proto.InjectiveExplorerRPC(
endpoint,
grpc.credentials.createSsl()
)
const metadata = new grpc.Metadata()
metadata.add('x-token', token)
client.GetBlocksV2({ per_page: 1 }, metadata, (error, response) => {
if (error) {
console.error(error)
process.exitCode = 1
return
}
console.log(JSON.stringify(response, null, 2))
client.close()
})
Step 5: Run the application
Run the request:
node index.js
Step 6: Verify the response
The request returns the most recently indexed block. Block values change as Injective Mainnet advances, but the response should have this structure. This shortened example is from a live Quicknode response:
{
"paging": {
"next": ["MTgxMDIxMTAw"]
},
"data": [
{
"height": "181021101",
"moniker": "SCV-Security",
"block_hash": "0xd39c44f30e2e45fe5313d9b450e1421f25b88219bc611f493e885dbc58cf07ca",
"num_txs": "3",
"block_unix_timestamp": "1788186602675"
}
]
}
A successful response confirms that TLS, x-token authentication, proto loading, and the Explorer client are configured correctly.
Next steps
Your Node.js client is now ready to make authenticated requests to the Injective Indexer gRPC API. Explore the Explorer API methods and Exchange API methods to find more methods, request parameters, response fields, and runnable examples.