TypeScript Client for Sui gRPC
Connect a Node.js TypeScript client to Dwellir Sui gRPC using official v2 protobuf files, TLS, metadata authentication, and deadlines.
Sui RPC endpoint: endpoint URLs, plans and pricing.
This example runs in Node.js. It loads the official v2 service definitions and queries the node version and current checkpoint.
Install dependencies
mkdir sui-grpc-ts
cd sui-grpc-ts
npm init -y
npm install @grpc/grpc-js @grpc/proto-loader
npm install --save-dev typescript tsx @types/node
git clone --depth 1 https://github.com/MystenLabs/sui-apis.git
mkdir -p protos
cp -R sui-apis/proto/. protos/Keep the full sui/rpc/v2/ directory and its imports. The service filename is ledger_service.proto.
Query service information
Save this as client.ts:
import * as grpc from '@grpc/grpc-js';
import * as protoLoader from '@grpc/proto-loader';
type ServiceInfo = {
chain?: string;
server?: string;
checkpoint_height?: string;
};
type LedgerClient = grpc.Client & {
GetServiceInfo(
request: Record<string, never>,
metadata: grpc.Metadata,
options: grpc.CallOptions,
callback: grpc.requestCallback<ServiceInfo>,
): grpc.ClientUnaryCall;
};
type Services = {
sui: { rpc: { v2: { LedgerService: grpc.ServiceClientConstructor } } };
};
const definition = protoLoader.loadSync(
'sui/rpc/v2/ledger_service.proto',
{ keepCase: true, longs: String, includeDirs: ['./protos'] },
);
const services = grpc.loadPackageDefinition(definition) as unknown as Services;
const client = new services.sui.rpc.v2.LedgerService(
'api-sui-mainnet-full.n.dwellir.com:443',
grpc.credentials.createSsl(),
) as unknown as LedgerClient;
const apiKey = process.env.DWELLIR_API_KEY;
if (!apiKey) throw new Error('Set DWELLIR_API_KEY');
const metadata = new grpc.Metadata();
metadata.set('x-api-key', apiKey);
client.GetServiceInfo({}, metadata, { deadline: Date.now() + 10_000 }, (error, response) => {
try {
if (error) throw error;
console.log(response);
} finally {
client.close();
}
});Set DWELLIR_API_KEY in your environment, then run:
npx tsx client.tskeepCase: true preserves snake_case fields. Protobuf uint64 values remain strings, which avoids JavaScript integer precision loss.
Add other services
Load each service's actual file and constructor:
| Service | Proto file |
|---|---|
StateService | state_service.proto |
MovePackageService | move_package_service.proto |
TransactionExecutionService | transaction_execution_service.proto |
SubscriptionService | subscription_service.proto |
SignatureVerificationService | signature_verification_service.proto |
NameService | name_service.proto |
Method responses have envelopes. For example, GetObject returns response.object, and GetBalance returns response.balance.
Use the execution reference for transaction and signature encoding. Use the subscription guide for stream recovery.
Browser applications need a compatible browser transport or a server-side integration. This Node.js example uses native HTTP/2 gRPC.
Check the deployed schema
Use grpcurl reflection to inspect the endpoint before generating or updating clients. The gRPC overview lists all supported methods.