# Quick Start

## Import the SDK

{% tabs %}
{% tab title="index.html" %}

```html
<script src="https://solpay.solblaze.org/sdk.js" type="text/javascript"></script>
```

{% endtab %}
{% endtabs %}

## Connect to the Solana Network

### Connect to Default RPC Endpoint:

{% tabs %}
{% tab title="index.js" %}

```javascript
(async () => {
    let network_details = await SOLPay.connectNetwork();
    console.log(network_details.network); // default network RPC URL
    console.log(network_details.commitment); // "confirmed"
})();
```

{% endtab %}
{% endtabs %}

### Connect to Custom RPC Endpoint:

{% tabs %}
{% tab title="index.js" %}

```javascript
(async () => {
    let network_details = await SOLPay.connectNetwork("https://api.mainnet-beta.solana.com", "confirmed");
    console.log(network_details.network); // "https://api.mainnet-beta.solana.com"
    console.log(network_details.commitment); // "confirmed"
})();
```

{% endtab %}
{% endtabs %}

## Connect Wallet

{% tabs %}
{% tab title="index.js" %}

```javascript
(async () => {
    let wallet = await SOLPay.connectWallet(SOLPay.adapters.PHANTOM);
    console.log(wallet.address); // the address of the connected wallet
})();
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}

#### Wallet Adapters:

SOL Pay supports six wallets:

* Phantom (`SOLPay.adapters.PHANTOM`)
* Solflare (`SOLPay.adapters.SOLFLARE`)
* Slope (`SOLPay.adapters.SLOPE`)
* Glow (`SOLPay.adapters.GLOW`)
* Exodus (`SOLPay.adapters.EXODUS`)
* Brave (`SOLPay.adapters.BRAVE`)

You can also use `SOLPay.adapters.CURRENT_ADAPTER` or leave the adapter field blank to use the current adapter.
{% endhint %}

## Send Transaction

### Send Solana Lamports

{% tabs %}
{% tab title="index.js" %}

```javascript
(async () => {
    let address = "RECIPIENT_ADDRESS_HERE" // replace with the Solana address of the recipient
    let lamports = 10000; // replace with the number of lamports to send, one billion lamports per SOL
    let payment_details = await SOLPay.sendSolanaLamports(address, lamports);
    console.log(payment_details.from); // the address of the sender wallet/connected wallet
    console.log(payment_details.to); // the address of the recipient wallet
    console.log(payment_details.lamports); // the lamports transacted
    console.log(payment_details.signature); // the signature of the transaction
})();
```

{% endtab %}
{% endtabs %}

### Send Solana

{% hint style="warning" %}

#### Important:

This method may lead to slightly inaccurate results due to decimal roundoff error. We highly recommend using the sendSolanaLamports method (shown above) instead.
{% endhint %}

{% tabs %}
{% tab title="index.js" %}

```javascript
(async () => {
    let address = "RECIPIENT_ADDRESS_HERE" // replace with the Solana address of the recipient
    let amount = 0.00001; // replace with the amount of SOL to send
    let payment_details = await SOLPay.sendSolana(address, amount);
    console.log(payment_details.from); // the address of the sender wallet/connected wallet
    console.log(payment_details.to); // the address of the recipient wallet
    console.log(payment_details.lamports); // the lamports transacted
    console.log(payment_details.signature); // the signature of the transaction
})();
```

{% endtab %}
{% endtabs %}

## Verify Transaction

{% hint style="warning" %}

#### Important:

It is vital to verify that a transaction is valid on your backend. If you only trust the payment details on the front-end, hackers can easily forge fake transactions. However, verifying the transaction on your backend resolves this issue. The backend is also where you would complete the transaction by logging the signature to a database (so that it cannot be used twice) and then providing the user with the product.

Our API does not check whether you have already verified a transaction. You need to make sure on your backend that you are storing the transaction signatures so that they cannot be used to buy the same product twice.
{% endhint %}

## Verifies the details of a transaction made through SOL Pay

<mark style="color:blue;">`GET`</mark> `https://solpay.solblaze.org/transaction.php`

The transaction endpoint takes in two parameters: to and txid. The to parameter is for the recipient address, and the txid is for the signature (which can be obtained through the payment details above). The endpoint returns the sender address and the number of lamports sent.

#### Query Parameters

| Name                                   | Type   | Description           |
| -------------------------------------- | ------ | --------------------- |
| to<mark style="color:red;">\*</mark>   | string | recipient address     |
| txid<mark style="color:red;">\*</mark> | string | transaction signature |

{% tabs %}
{% tab title="200: OK The transaction details are returned" %}

```javascript
{
    "status": "success",
    "transaction": {
        "lamports": 10000,
        "amount": 1.0e-5,
        "from": "SENDER_ADDRESS_HERE"
    }
}
```

{% endtab %}
{% endtabs %}

### Example Verification URL

```
https://solpay.solblaze.org/transaction.php?to=RECIPIENT_ADDRESS&txid=TRANSACTION_SIGNATURE
```

## Example

### Frontend

{% tabs %}
{% tab title="index.html" %}

```html
<script src="https://solpay.solblaze.org/sdk.js" type="text/javascript"></script>
<script src="/index.js" type="text/javascript"></script>
<button onclick="connectWallet();">Connect</button>
<button onclick="buyItem();">Buy Item for 0.00001 SOL</button>
```

{% endtab %}

{% tab title="index.js" %}

```javascript
async function connectWallet() {
    /* Connect to Solana network: */
    let network_url = await SOLPay.connectNetwork();
    console.log(network_url); // "https://solana-api.projectserum.com"
    
    /* Connect to user wallet: */
    let wallet = await SOLPay.connectWallet();
    console.log(wallet.address); // the address of the connected wallet
}

async function buyItem() {
    let address = "SELLER_ADDRESS_HERE" // replace with the Solana address of the seller
    let lamports = 10000;
    let payment_details = await SOLPay.sendSolanaLamports(address, lamports);
    // IMPORTANT: Send payment_details.signature to the backend for verification
    let raw_result = await fetch("/verify.php?txid=" + encodeURIComponent(payment_details.signature));
    let parsed_result = await raw_result.json();
    // Use parsed_result to either return success message or error message to user
}
```

{% endtab %}
{% endtabs %}

### Backend

{% tabs %}
{% tab title="verify.php" %}

```php
// If the transaction signature is not yet in the database
$to = "SELLER_ADDRESS_HERE";
$txid = $_GET["txid"]; // Get the txid in the URL from frontend request
$transaction_data = json_decode(file_get_contents("https://solpay.solblaze.org/transaction.php?to=" . $to . "&txid=" . $txid), true);
if($transaction_data["status"] == "success" && $transaction_data["transaction"]["lamports"] >= 10000) {
    // Add the transaction signature to the database so that it cannot be reused
    // Send the item to the user associated with $transaction_data["transaction"]["from"]
} else if($transaction_data["status"] == "error") {
    // Send $transaction_data["error"] to the frontend to display to the user
} else {
    // Send an error to the user that not enough funds were sent in the transaction
}
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}

#### Important:

The above example is for reference only and lacks a few features recommended for more robust apps, including a preconfirm handler ([sendSolanaLamports](/reference/sdk-reference/send-solana-lamports.md)) and, if the app immediately returns the purchased item back to the client directly, a user signature ([signMessage](/reference/sdk-reference/sign-message.md)).
{% endhint %}


---

# 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://solpay-docs.solblaze.org/quick-start.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.
