# Send Solana via javascript functions

### **Step 1: Init JavaScript Project** <a href="#id-4de5" id="id-4de5"></a>

```
npm init
```

### **Step 2: Install modules** <a href="#id-5270" id="id-5270"></a>

```
npm install -S @solana/web3.js bs58
```

@solana/web3.js — Solana JavaScript SDK\
bs58 — encoding library

### **Step 3: Create JavaScript script file** <a href="#id-8e9a" id="id-8e9a"></a>

You can use any text editor. Create an empty file with .js extension.\
\&#xNAN;*For e.g: **test.js***

### Step 4: Include installed modules <a href="#id-9de3" id="id-9de3"></a>

At the beginning of created file we will include required modules:

```
const web3 = require('@solana/web3.js');
const bs58 = require('bs58');
```

### Step 5: Init connection and create Keypair object <a href="#id-4bcf" id="id-4bcf"></a>

```
const connection = new web3.Connection('https://api.mainnet-beta.solana.com', 'confirmed');

const privateKey = bs58.decode('619G6W.....XaWz4');
const from = web3.Keypair.fromSecretKey(privateKey)
```

As an argument for **web3.Connection** method we will use any Solana RPC.\
\&#xNAN;*For e.g default mainnet RPC:* [*https://api.mainnet-beta.solana.com*](https://api.mainnet-beta.solana.com/)

In constant **privateKey** we will load wallet private key (you can find it in the settings of your wallet)

We will save Keypair object to constant **from**.

### Step 6: Create Trasnaction <a href="#c2b4" id="c2b4"></a>

```
(async () => {
    const transaction = new web3.Transaction().add(
        web3.SystemProgram.transfer({
          fromPubkey: from.publicKey,
          toPubkey: '9aq15...NzYHSJdxMa5t',
          lamports: 900000
        }),
      );
    
      const signature = await web3.sendAndConfirmTransaction(
        connection,
        transaction,
        [from],
      );
      
      console.log('SIGNATURE', signature);
})()
```

At the second string we create **transaction** constant. We will pass to **web3.Transaction().add()** method result of calling **web3.SystemProgram.transfer()** method with following arguments:

* fromPubkey — publicKey property of Keypair object
* toPubkey — string with SOL address to where we want to send SOL (lamports)
* lamports — amount of lamports that we want to send
* **fromPubkey** also may be a string\
  \* **toPubkey** also may be a Keypair object (in this case we will use **publicKey** property)\
  \* lamports is a piece of SOL. We can use this [converter](https://www.solconverter.com/) for better understanding

**signature** constant contains a result of execution **web3.sendAndConfirmTransaction()** method where we passed **connection** constant, **transaction** data and sender **Keypair** object.

Expected output of following code is a transaction hash:

```
SIGNATURE MifLinkmkV.....wEjnDxi25Gcb4HfoU9DM7dfLoCyw3U6tmpznczMivTj
```

<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://8bit-1.gitbook.io/solibrary/send-solana-via-javascript-functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
