# Sui TypeScript SDK

> TypeScript SDK for building on the Sui blockchain

The Sui TypeScript SDK is a modular library of tools for interacting with the Sui blockchain. Use it
to send queries to RPC nodes, build and sign transactions, and interact with a Sui or local network.

## Installation

```npm
npm i @mysten/sui
```

The SDK is published as an ESM only package. Make sure your `package.json` includes
`"type": "module"`:

```json
{
	"type": "module"
}
```

If you are using TypeScript, your `tsconfig.json` should use a compatible `moduleResolution` setting
such as `"NodeNext"`, `"Node16"`, or `"Bundler"`.

## Module packages

The SDK contains a set of modular packages that you can use independently or together. Import just
what you need to keep your code light and compact.

- [`@mysten/sui/client`](/sui/clients): A client for interacting with Sui RPC nodes.
- [`@mysten/sui/bcs`](/sui/bcs): A BCS builder with pre-defined types for Sui.
- [`@mysten/sui/transactions`](/sui/transactions/basics): Utilities for building and interacting
  with transactions.
- [`@mysten/sui/keypairs/*`](/sui/cryptography/keypairs): Modular exports for specific KeyPair
  implementations.
- [`@mysten/sui/verify`](/sui/cryptography#verifying-signatures): Methods for verifying transactions
  and messages.
- [`@mysten/sui/cryptography`](/sui/cryptography): Shared types and classes for cryptography.
- [`@mysten/sui/multisig`](/sui/cryptography/signers/multisig): Utilities for working with multisig
  signatures.
- [`@mysten/sui/utils`](/sui/utils): Utilities for formatting and parsing various Sui types.
- [`@mysten/sui/faucet`](#faucet): Methods for requesting SUI from a faucet.
- [`@mysten/sui/zklogin`](/sui/zklogin): Utilities for working with zkLogin.

## Network locations

The following table lists the locations for Sui networks.

| Network | Full node                             | faucet                                   |
| ------- | ------------------------------------- | ---------------------------------------- |
| local   | `http://127.0.0.1:9000` (default)     | `http://127.0.0.1:9123/v2/gas` (default) |
| Devnet  | `https://fullnode.devnet.sui.io:443`  | `https://faucet.devnet.sui.io/v2/gas`    |
| Testnet | `https://fullnode.testnet.sui.io:443` | `https://faucet.testnet.sui.io/v2/gas`   |
| Mainnet | `https://fullnode.mainnet.sui.io:443` | `null`                                   |

> **Warning:** Use dedicated nodes/shared services rather than public endpoints for production apps. The public
> endpoints maintained by Mysten Labs (`fullnode.<NETWORK>.sui.io:443`) are rate-limited, and support
> only 100 requests per 30 seconds or so. Do not use public endpoints in production applications with
> high traffic volume.
> 
> You can either run your own Full nodes, or outsource this to a professional infrastructure provider
> (preferred for apps that have high traffic). You can find a list of reliable RPC endpoint providers
> for Sui on the [Sui Dev Portal](https://sui.io/developers#dev-tools) using the **Node Service** tab.

## Quick start

Get started in a few minutes. This guide walks you through creating a keypair, funding it from a
faucet, and checking your balance.

### Create a project

```sh
mkdir hello-sui
cd hello-sui
npm init -y
npm pkg set type=module
npm i @mysten/sui
```

### Step 1: Create a keypair and get SUI

Create a `setup.ts` file that generates a new keypair, requests SUI from the faucet, and prints your
secret key for later use:

```typescript

// Generate a new keypair
const keypair = new Ed25519Keypair();

console.log('Address:', keypair.toSuiAddress());
console.log('Secret key:', keypair.getSecretKey());

// Request SUI from the devnet faucet
await requestSuiFromFaucetV2({
	host: getFaucetHost('devnet'),
	recipient: keypair.toSuiAddress(),
});

console.log('Faucet request sent! Save the secret key above for the next step.');
```

Run it:

```sh
node setup.ts
```

Save the secret key that gets printed; you'll use it in the next step.

> **Warning:** Logging secret keys to the console is only appropriate for quick demos like this. In real
> applications, never log or expose secret keys. Store them securely using environment variables,
> encrypted keystores, or a secrets manager.

### Step 2: Check your balance

Create a `balance.ts` file that imports your keypair from the secret key and checks the balance:

```typescript

// Import the keypair using the secret key from step 1
const keypair = Ed25519Keypair.fromSecretKey('suiprivkey1...'); // paste your secret key here

const grpcClient = new SuiGrpcClient({
	network: 'devnet',
	baseUrl: 'https://fullnode.devnet.sui.io:443',
});

const { balance } = await grpcClient.core.getBalance({
	owner: keypair.toSuiAddress(),
});

const sui = Number(balance.balance) / Number(MIST_PER_SUI);
console.log(`Address: ${keypair.toSuiAddress()}`);
console.log(`Balance: ${sui} SUI`);
```

Run it:

```sh
node balance.ts
```

### Step 3: Transfer SUI

Create a `transfer.ts` file that sends SUI to another address and logs the transaction effects:

```typescript

const keypair = Ed25519Keypair.fromSecretKey('suiprivkey1...'); // paste your secret key here
const grpcClient = new SuiGrpcClient({
	network: 'devnet',
	baseUrl: 'https://fullnode.devnet.sui.io:443',
});

const tx = new Transaction();

tx.transferObjects(
	[coinWithBalance({ balance: BigInt(0.1 * Number(MIST_PER_SUI)) })],
	'0xRecipientAddress', // replace with the recipient's address
);

const result = await keypair.signAndExecuteTransaction({
	transaction: tx,
	client: grpcClient,
	include: { effects: true, balanceChanges: true },
});

if (result.$kind === 'FailedTransaction') {
	console.error('Transaction failed:', result.FailedTransaction.status.error?.message);
} else {
	console.log('Transaction digest:', result.Transaction.digest);
	console.log('Effects:', JSON.stringify(result.Transaction.effects, null, 2));
	console.log('Balance changes:', JSON.stringify(result.Transaction.balanceChanges, null, 2));
}
```

Run it:

```sh
node transfer.ts
```

### Faucet

Devnet, Testnet, and local networks include faucets that mint SUI. Use `requestSuiFromFaucetV2` to
request SUI programmatically:

```typescript

await requestSuiFromFaucetV2({
	host: getFaucetHost('testnet'),
	recipient: '0xYourAddress',
});
```

> **Note:** Faucets on Devnet and Testnet are rate limited. If you hit the limit, wait before trying again.
> For Testnet, you can also get SUI through the web UI at `faucet.sui.io` or through the [Sui
> Discord](https://discord.gg/sui) faucet channels.

## Next steps

- [Building Transactions](/sui/transactions/basics): create and compose transactions
- [Signing and Execution](/sui/transactions/signing-and-execution): sign and submit transactions
- [Coins and Balances](/sui/transactions/coins-and-balances): work with tokens
- [Client Setup](/sui/clients): configure clients for different networks