Quickstart

Learn how to transfer STX tokens via a web wallet using Stacks Connect.

In this quickstart guide, you will learn how to set up your project, authenticate users with a web wallet, and initiate a STX token transfer.

For a deeper dive into the authentication process, check out the authenticate users guide.


Install package dependencies

Add the necessary packages to your project using your preferred package manager.

npm install @stacks/connect @stacks/transactions @stacks/network

Create an AppConfig and UserSession

Add a reusable UserSession instance to your project. This will allow your app to store authentication state in localStorage.

session.ts
import { AppConfig, UserSession } from '@stacks/connect';

const appConfig = new AppConfig(['store_write', 'publish_data']);
export const userSession = new UserSession({ appConfig });

You can now import the userSession instance in the following step to continue with the authentication process.

Interacting with the user's wallet

The process of connecting to a wallet gives the web app information about the wallet account, which enables interactions with the Stacks blockchain, like calling smart contracts.

Using the showConnect function, you can prompt the user to select a wallet to connect to your app.

connect.ts
import { showConnect } from '@stacks/connect';
import { userSession } from './session';

const appDetails = {
 name: "My app",
 icon: "logo.png",
}

showConnect({
 appDetails,
 userSession,
 onFinish: () => {
   window.location.reload();
 },
 onCancel: () => {
   console.log('oops');
 },
});

This will configure the showConnect function to display a wallet connection pop-up with your app's name and icon, and handle user confirmation or cancellation actions in the onFinish and onCancel callbacks.

Initiate a STX transfer

With a connected wallet, you can now initiate a STX transfer to another account.

To prompt the user for a STX transfer call, use the openSTXTransfer function.

transfer.ts
import { openSTXTransfer } from '@stacks/connect';
import { StacksTestnet } from '@stacks/network';
import { userSession } from './session';

openSTXTransfer({
  network: new StacksTestnet(),
  recipient: 'ST39MJ145BR6S8C315AG2BD61SJ16E208P1FDK3AK',
  amount: 10000000,
  onFinish: response => {
    console.log(response.txid);
  },
  onCancel: () => {
    console.log('User cancelled');
  },
});

This will prompt the user to confirm and sign a transaction, sending 10 STX to the recipient address.

The amount field is denominated in microSTX, which is the smallest fraction of STX. 1,000,000 microSTX = 1 STX.

Next steps