Backend & Scripting
Backend services are suitable for when you need to perform actions from your wallet or simply need to read data; rather than connecting to your user's wallets. It's also a great way to perform actions on behalf of your users.
There are multiple different ways you can instantiate the SDK and use it, which vary depending on what you need to do.
To get started, install the SDK:
- npm
- Yarn
- pnpm
npm install @thirdweb-dev/sdk @metaplex-foundation/js @project-serum/anchor
yarn add @thirdweb-dev/sdk @metaplex-foundation/js @project-serum/anchor
pnpm add @thirdweb-dev/sdk @metaplex-foundation/js @project-serum/anchor
Read-only SDK
Provide the network or your own RPC URL to start reading data from any Solana program.
import { ThirdwebSDK } from "@thirdweb-dev/sdk/solana";
// Simply pass in the network you want to connect to
// can be one of "mainnet-beta", "devnet", "testnet", "localhost" or your own RPC url
const sdk = ThirdwebSDK.fromNetwork("devnet", {
secretKey: "...", // obtain from an API key from the Dashboard
});
Read-Write SDK
To execute transactions, you will need to connect a wallet to the SDK. All transactions you make will be signed by the wallet you provide.
From an existing private key
To connect the SDK with a specific wallet, you can pass in the wallet's private key.
import { ThirdwebSDK } from "@thirdweb-dev/sdk/solana";
const privateKey = "..."; // Consider fetching this from a secrets manager
const sdk = ThirdwebSDK.fromPrivateKey("devnet", privateKey, {
secretKey: "...", // obtain from an API key from the Dashboard
});
Ensure you store and access your private key securely.
- Check if you need to use a private key for your application.
- Never directly expose your private key in your source code.
- Never commit any file that may contain your private key to your source control.
- Never use a private key for a frontend (website/dapp) application.
If you are unsure how to securely store and access your private key, please do not proceed.
Using the Solana CLI Tool
If you have the Solana CLI tool installed, you can follow the setup steps to generate a keypair.
That keypair will be written to the default path, and the SDK can read from that path to initialize the SDK with the wallet generated by the Solana CLI.
This is a great choice for local scripting using NodeJS.
import { createThirdwebSDK } from "@thirdweb-dev/sdk/solana/server";
// create the SDK using the default path for the Solana CLI keypair
const sdk = createThirdwebSDK("devnet");
Note that the createThirdwebSDK
function was imported from @thirdweb-dev/sdk/solana/server
and is only supported on NodeJS environments.