Sui.

Post

Share your knowledge.

article banner.
AySports.
Aug 14, 2025
Article

Building Your First dApp with the Sui TypeScript SDK — Step-by-Step

Who this is for: Frontend and full-stack developers who want to integrate Sui into web apps, wallets, or dashboards without relying solely on the CLI.

Goal: By the end of this article, you’ll be able to:

Install and configure the Sui TypeScript SDK.

Connect to testnet and query blockchain data.

Execute transactions programmatically.

Handle gas, effects, and common SDK pitfalls.


Part 1 — What the Sui TypeScript SDK Does

The Sui TypeScript SDK is your bridge between a browser/server app and the Sui network. It:

Connects to RPC nodes to read data (objects, events, balances).

Submits transactions (transfers, Move calls).

Works in Node.js and browser environments.

Powers many wallets and dApps in the Sui ecosystem.


Part 2 — Prerequisites

Before you start, you’ll need:

Node.js ≥ 18

npm or yarn

A Sui wallet (e.g., Sui Wallet extension) for private key management.

Some testnet SUI (get from faucet via CLI or wallet).


Part 3 — Installing the SDK

  1. Create a new project:

mkdir sui-ts-demo && cd sui-ts-demo npm init -y

  1. Install the SDK:

npm install @mysten/sui.js

  1. (Optional) Install dotenv for managing environment variables:

npm install dotenv


Part 4 — Connecting to the Network

index.js (or index.ts if using TypeScript):

import 'dotenv/config'; import { JsonRpcProvider, Connection } from '@mysten/sui.js';

// Create a connection to the Sui testnet const provider = new JsonRpcProvider( new Connection({ fullnode: 'https://fullnode.testnet.sui.io/' }) );

// Test connection: Get latest block info async function getLatestBlock() { const latest = await provider.getLatestCheckpointSequenceNumber(); console.log('Latest Checkpoint:', latest); }

getLatestBlock();

Run:

node index.js

If successful, you’ll see a checkpoint number — meaning your SDK can talk to the network.


Part 5 — Querying Data

Example: Fetch your SUI balance.

const address = process.env.MY_ADDRESS; // set in .env const coins = await provider.getCoins({ owner: address, coinType: '0x2::sui::SUI' });

console.log('Your coins:', coins.data); console.log('Total balance:', coins.data.reduce((sum, c) => sum + BigInt(c.balance), 0n).toString());


Part 6 — Sending a Transaction (Transfer SUI)

1) Import Keypair

If you’re storing your key locally (dev/test only — never expose production keys):

import { Ed25519Keypair, RawSigner } from '@mysten/sui.js';

const secretKey = process.env.SECRET_KEY_BASE64; // base64-encoded private key const keypair = Ed25519Keypair.fromSecretKey(Buffer.from(secretKey, 'base64')); const signer = new RawSigner(keypair, provider);

2) Build and Send

async function sendSui(recipient, amount) { const tx = await signer.paySui({ inputCoins: (await provider.getCoins({ owner: keypair.getPublicKey().toSuiAddress() })).data.map(c => c.coinObjectId), recipients: [recipient], amounts: [amount], // in MIST (1 SUI = 1_000_000_000 MIST) });

console.log('Transfer digest:', tx.digest); }

sendSui('0xRECIPIENT_ADDRESS', 1_000_000);


Part 7 — Calling a Move Function If you’ve published a package (e.g., counter from Article 1):

async function incrementCounter(packageId, objectId, by) { const tx = await signer.executeMoveCall({ packageObjectId: packageId, module: 'counter', function: 'inc', typeArguments: [], arguments: [objectId, by], gasBudget: 20_000_000, });

console.log('Increment digest:', tx.digest); }


Part 8 — Common Problems & Solutions

ProblemCauseFix
Invalid base64 stringWrong key encodingEnsure key is raw private key in base64
Insufficient gasNo testnet SUI or small coin selectedUse faucet & merge coins via CLI before SDK call
Transaction never confirmsWrong RPC endpointUse official testnet/mainnet fullnode URLs
Type errors in TSSDK version mismatchInstall latest @mysten/sui.js and match TS config

Part 9 — Development Tips

  1. Never hardcode keys in source — use .env.

  2. Batch reads — provider supports queries for multiple objects in one call.

  3. Use events — subscribe to specific events from your Move modules.

  4. Test in localnet — Sui CLI can run a local network for integration testing.


✅ You can now:

Connect to Sui from JavaScript/TypeScript.

Query balances and objects.

Send SUI and call Move functions programmatically.

  • Sui
  • SDKs and Developer Tools
0
Share
Comments
.