Quick Start

Get started with the SDK in under 5 minutes!

Import the SDK

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

Connect to the Solana Network

Connect to Default RPC Endpoint:

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

Connect to Custom RPC Endpoint:

(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"
})();

Connect Wallet

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

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.

Send Transaction

Send Solana Lamports

(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
})();

Send Solana

Important:

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

(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
})();

Verify Transaction

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.

Verifies the details of a transaction made through SOL Pay

GET https://solpay.togatech.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

NameTypeDescription

to*

string

recipient address

txid*

string

transaction signature

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

Example Verification URL

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

Example

Frontend

<script src="https://solpay.togatech.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>

Backend

// 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.togatech.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
}

Important:

The above example is for reference only and lacks a few features recommended for more robust apps, including a preconfirm handler (sendSolanaLamports) and, if the app immediately returns the purchased item back to the client directly, a user signature (signMessage).

Last updated