Sui TypeScript SDK
This is the Sui TypeScript SDK built on the Sui JSON RPC API. It provides utility classes and functions for applications to sign transactions and interact with the Sui network.
WARNING: Note that we are still iterating on the RPC and SDK API before TestNet, therefore please expect frequent breaking changes in the short-term. We expect the API to stabilize after the upcoming TestNet launch.
Current version: 1.37.6
Working with DevNet
The SDK will be published to npm registry with the same bi-weekly release cycle as the DevNet validators and RPC Server. To use the SDK in your project, you can do:
npm install @mysten/sui
You can also use your preferred npm client, such as yarn or pnpm.
Working with local network
Note that the latest tag for the published SDK might go out of sync with the RPC server on the main branch until the next release. If you're developing against a local network, we recommend using the experimental-tagged packages, which contain the latest changes from main.
npm install @mysten/sui@experimental
Refer to the JSON RPC topic for instructions about how to start a local network and local RPC server.
Connecting to Sui Network
The JsonRpcProvider class provides a connection to the JSON-RPC Server and should be used for all read-only operations. The default URLs to connect with the RPC server are:
- local: http://127.0.0.1:9000
- Testnet: https://api.blockeden.xyz/sui/testnet/${apiAccessKey}
- Mainnet: https://api.blockeden.xyz/sui/${apiAccessKey}
Don't have an access key? Create one at https://BlockEden.xyz/dash/.
import { SuiClient, getFullnodeUrl } from '@mysten/sui/client';
// connect to Testnet
const suiClient = new SuiClient({ url: getFullnodeUrl('testnet') });
// get tokens from the Testnet faucet server
import { getFaucetHost, requestSuiFromFaucetV0 } from '@mysten/sui/faucet';
await requestSuiFromFaucetV0({
host: getFaucetHost('testnet'),
recipient: '0xcc2bd176a478baea9a0de7a24cd927661cc6e860d5bacecb9a138ef20dbab231',
});
For local development, you can run cargo run --bin sui-test-validator to spin up a local network with a local validator, a fullnode, and a faucet server. Refer to this guide for more information.
import { SuiClient, getFullnodeUrl } from '@mysten/sui/client';
import { getFaucetHost, requestSuiFromFaucetV0 } from '@mysten/sui/faucet';
// connect to local RPC server
const suiClient = new SuiClient({ url: getFullnodeUrl('localnet') });
// get tokens from the local faucet server
await requestSuiFromFaucetV0({
host: getFaucetHost('localnet'),
recipient: '0xcc2bd176a478baea9a0de7a24cd927661cc6e860d5bacecb9a138ef20dbab231',
});
You can also construct your own in custom connections, with your own URLs to your fullnode and faucet server
import { SuiClient } from '@mysten/sui/client';
import { requestSuiFromFaucetV0 } from '@mysten/sui/faucet';
// connect to a custom RPC server
const suiClient = new SuiClient({ url: 'https://fullnode.testnet.sui.io' });
// get tokens from a custom faucet server
await requestSuiFromFaucetV0({
host: 'https://faucet.testnet.sui.io/gas',
recipient: '0xcc2bd176a478baea9a0de7a24cd927661cc6e860d5bacecb9a138ef20dbab231',
});
Writing APIs
For a primer for building transactions, refer to this guide.
Transfer Object
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
import { SuiClient, getFullnodeUrl } from '@mysten/sui/client';
import { Transaction } from '@mysten/sui/transactions';
// Generate a new Ed25519 Keypair
const keypair = new Ed25519Keypair();
const client = new SuiClient({ url: getFullnodeUrl('mainnet') });
const tx = new Transaction();
tx.transferObjects(
[tx.object('0xe19739da1a701eadc21683c5b127e62b553e833e8a15a4f292f4f48b4afea3f2')],
tx.pure.address('0x1d20dcdb2bca4f508ea9613994683eb4e76e9c4ed371169677c1be02aaf0b12a'),
);
const result = await client.signAndExecuteTransaction({
transaction: tx,
signer: keypair,
});
console.log({ result });
Transfer Sui
To transfer 1000 MIST to another address:
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
import { SuiClient, getFullnodeUrl } from '@mysten/sui/client';
import { Transaction } from '@mysten/sui/transactions';
// Generate a new Keypair
const keypair = new Ed25519Keypair();
const client = new SuiClient({ url: getFullnodeUrl('mainnet') });
const tx = new Transaction();
const [coin] = tx.splitCoins(tx.gas, [1000]);
tx.transferObjects([coin], keypair.toSuiAddress());
const result = await client.signAndExecuteTransaction({
transaction: tx,
signer: keypair,
});
console.log({ result });
Merge coins
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
import { SuiClient, getFullnodeUrl } from '@mysten/sui/client';
import { Transaction } from '@mysten/sui/transactions';
// Generate a new Keypair
const keypair = new Ed25519Keypair();
const client = new SuiClient({ url: getFullnodeUrl('mainnet') });
const tx = new Transaction();
tx.mergeCoins(tx.object('0xe19739da1a701eadc21683c5b127e62b553e833e8a15a4f292f4f48b4afea3f2'), [
tx.object('0x127a8975134a4824d9288722c4ee4fc824cd22502ab4ad9f6617f3ba19229c1b'),
]);
const result = await client.signAndExecuteTransaction({
transaction: tx,
signer: keypair,
});
console.log({ result });
Move Call
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
import { SuiClient, getFullnodeUrl } from '@mysten/sui/client';
import { Transaction } from '@mysten/sui/transactions';
// Generate a new Keypair
const keypair = new Ed25519Keypair();
const client = new SuiClient({ url: getFullnodeUrl('mainnet') });
const packageObjectId = '0x...';
const tx = new Transaction();
tx.moveCall({
target: `${packageObjectId}::nft::mint`,
arguments: [tx.pure.string('Example NFT')],
});
const result = await client.signAndExecuteTransaction({
transaction: tx,
signer: keypair,
});
console.log({ result });
Publish Modules
To publish a package:
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
import { SuiClient, getFullnodeUrl } from '@mysten/sui/client';
import { Transaction } from '@mysten/sui/transactions';
const { execSync } = require('child_process');
// Generate a new Keypair
const keypair = new Ed25519Keypair();
const client = new SuiClient({ url: getFullnodeUrl('mainnet') });
const { modules, dependencies } = JSON.parse(
execSync(`${cliPath} move build --dump-bytecode-as-base64 --path ${packagePath}`, {
encoding: 'utf-8',
}),
);
const tx = new Transaction();
const [upgradeCap] = tx.publish({
modules,
dependencies,
});
tx.transferObjects([upgradeCap], keypair.toSuiAddress());
const result = await client.signAndExecuteTransaction({
transaction: tx,
signer: keypair,
});
console.log({ result });