For the complete documentation index, see llms.txt. This page is also available as Markdown.

StateService/GetBalance - SUI

Example code for the StateService/GetBalance gRPC method. Complete guide on how to use StateService/GetBalance gRPC method in GetBlock Web3 documentation.

Returns the balance for a specific address and coin type. The result aggregates across all coin objects of that type owned by the address. Coin type is a Move type string like 0x2::sui::SUI.

Service: sui.rpc.v2.StateService Proto file: sui/rpc/v2/state_service.proto Full method path: sui.rpc.v2.StateService/GetBalance

Request Fields

Field
Type
Required
Description

owner

Address

Yes

32-byte address (hex 0x...)

coin_type

string

Yes

Move coin type — e.g. 0x2::sui::SUI for the native token

Request Example

# Clone the official proto files first (one-time setup):
#   git clone https://github.com/MystenLabs/sui-apis.git && cd sui-apis

grpcurl \
  -import-path proto \
  -proto sui/rpc/v2/state_service.proto \
  -H "x-grpc-web: 1" \
  -d '{
    "owner": "0xb871a42470b59c7184033a688f883cf24eb5e66eae1db62319bab27adb30b873",
    "coin_type": "0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI"
}' \
  go.getblock.io:443/<ACCESS-TOKEN> \
  sui.rpc.v2.StateService/GetBalance
import * as grpc from '@grpc/grpc-js';
import * as protoLoader from '@grpc/proto-loader';
import * as path from 'path';

const PROTO_PATH = path.join(__dirname, 'protos/proto/sui/rpc/v2/state_service.proto');
const ACCESS_TOKEN = '<ACCESS-TOKEN>';

const packageDef = protoLoader.loadSync(PROTO_PATH, {
    includeDirs: [path.join(__dirname, 'protos/proto')],
    keepCase: true, longs: String, enums: String, defaults: true,
});
const proto = grpc.loadPackageDefinition(packageDef) as any;
const ServiceClient = proto.sui.rpc.v2.StateService;

const metadata = new grpc.Metadata();
metadata.add('authorization', `Bearer ${ACCESS_TOKEN}`);

const client = new ServiceClient('go.getblock.io:443', grpc.credentials.createSsl());

const request = {
    "owner": "0xb871a42470b59c7184033a688f883cf24eb5e66eae1db62319bab27adb30b873",
    "coin_type": "0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI"
};

client.GetBalance(request, metadata, (err: any, response: any) => {
    if (err) {
        console.error('Error:', err);
        return;
    }
    console.log(JSON.stringify(response, null, 2));
});

Response Example

Responses are encoded in Protocol Buffers binary format on the wire. The example below shows the protobuf JSON encoding for readability.

Response Fields

Field
Type
Description

balance.coin_type

string

Echoed coin type

balance.balance

string

Total balance across all coin objects of this type (uint64 in the coin's smallest unit — MIST for SUI)

balance.coin_balance

string

Same value as balance — included for backward compatibility

Use Cases

  • Wallet balance displays for a specific coin

  • Pre-flight funds check before submitting a transaction

  • Portfolio applications that track a single asset

Error Handling

gRPC uses status codes rather than JSON-RPC numeric error codes. The most relevant for this method:

Status Code
Numeric
Cause

UNAUTHENTICATED

16

Missing or invalid <ACCESS-TOKEN> in the URL path

INVALID_ARGUMENT

3

Request fields are missing, malformed, or fail validation

UNAVAILABLE

14

Node is overloaded or temporarily unable to handle the request — retry with backoff

DEADLINE_EXCEEDED

4

Request did not complete within the timeout window

RESOURCE_EXHAUSTED

8

Rate limit exceeded for your plan

NOT_FOUND

5

The address has no coin objects of the requested type

INVALID_ARGUMENT

3

Malformed address or coin type

SDK Integration

Was this helpful?