> ## Documentation Index
> Fetch the complete documentation index at: https://yieldxyz.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Solana Signing

> Sign Solana transactions using @solana/web3.js or Anchor

## Overview

Solana transactions returned by the Yield.xyz API can be signed using the official Solana SDK or the Anchor framework. Below you'll find examples for both approaches.

<Tabs>
  <Tab title="@solana/web3.js">
    ### @solana/web3.js

    [@solana/web3.js](https://solana-labs.github.io/solana-web3.js/) is the official JavaScript SDK for interacting with the Solana blockchain.

    #### Installation

    ```bash theme={null}
    npm install @solana/web3.js bs58
    ```

    #### Signing a Transaction

    ```typescript theme={null}
    import {
      Connection,
      Keypair,
      Transaction,
      sendAndConfirmTransaction,
    } from "@solana/web3.js";
    import bs58 from "bs58";

    const connection = new Connection(process.env.SOLANA_RPC_URL!, "confirmed");
    const keypair = Keypair.fromSecretKey(bs58.decode(process.env.SOLANA_PRIVATE_KEY!));

    async function signAndSend(serializedTx: string) {
      const txBuffer = Buffer.from(serializedTx, "base64");
      const transaction = Transaction.from(txBuffer);

      transaction.sign(keypair);

      const signature = await sendAndConfirmTransaction(connection, transaction, [
        keypair,
      ]);

      console.log("Transaction signature:", signature);
      return signature;
    }
    ```

    <Tip>
      Use `"confirmed"` commitment for a good balance between speed and reliability. Switch to `"finalized"` when you need maximum certainty.
    </Tip>
  </Tab>

  <Tab title="Anchor">
    ### Anchor

    [Anchor](https://www.anchor-lang.com/) is a framework for Solana that provides higher-level abstractions, often used alongside `@solana/web3.js`.

    #### Installation

    ```bash theme={null}
    npm install @coral-xyz/anchor @solana/web3.js
    ```

    #### Signing a Transaction

    ```typescript theme={null}
    import { AnchorProvider, Wallet } from "@coral-xyz/anchor";
    import { Connection, Keypair, Transaction } from "@solana/web3.js";

    const connection = new Connection(process.env.SOLANA_RPC_URL!, "confirmed");
    const keypair = Keypair.fromSecretKey(/* your secret key bytes */);
    const wallet = new Wallet(keypair);

    const provider = new AnchorProvider(connection, wallet, {
      commitment: "confirmed",
    });

    async function signAndSend(serializedTx: string) {
      const txBuffer = Buffer.from(serializedTx, "base64");
      const transaction = Transaction.from(txBuffer);

      const signedTx = await provider.wallet.signTransaction(transaction);
      const signature = await connection.sendRawTransaction(signedTx.serialize());

      await connection.confirmTransaction(signature, "confirmed");
      console.log("Transaction signature:", signature);
      return signature;
    }
    ```

    <Note>
      Anchor's `Provider` can also be used with a browser wallet adapter for front-end applications.
    </Note>
  </Tab>
</Tabs>
