thirdweb Go SDK
Installation
To install the SDK with the go get
command, run the following:
go get github.com/mry-am/thirdweb-go-sdk/v2/thirdweb
Getting Started
To start using this SDK, you just need to pass in a provider configuration.
Instantiating the SDK
Once you have all the necessary dependencies, you can follow the following setup steps to get started with SDK read-only functions:
package main
import (
"fmt"
"github.com/mry-am/thirdweb-go-sdk/v2/thirdweb"
)
func main() {
// Creates a new SDK instance to get read-only data for your contracts, you can pass:
// - a chain name (mainnet, rinkeby, goerli, polygon, mumbai, avalanche, fantom)
// - a custom RPC URL
sdk, err := thirdweb.NewThirdwebSDK("mumbai", nil)
if err != nil {
panic(err)
}
// Now we can interact with the SDK, like displaying the connected chain ID
chainId, err := sdk.GetChainID()
if err != nil {
panic(err)
}
fmt.Println("New SDK instance create on chain", chainId)
}
Working With Contracts
Once you instantiate the SDK, you can use it to access your thirdweb contracts. You can use SDK's contract getter functions like GetNFTCollection
, GetEdition
, and GetNFTDrop
, to get the respective SDK contract instances. To use an NFT Collection contract for example, you can do the following.
package main
import (
"fmt"
"github.com/mry-am/thirdweb-go-sdk/v2/thirdweb"
)
func main() {
sdk, err := thirdweb.NewThirdwebSDK("mumbai", nil)
if err != nil {
panic(err)
}
// Add your NFT Collection contract address here
address := "0x..."
nft, err := sdk.GetNFTCollection(address)
if err != nil {
panic(err)
}
// Now you can use any of the read-only SDK contract functions
nfts, err := nft.GetAll()
if err != nil {
panic(err)
}
fmt.Printf("%d NFTs found on this contract\n", len(nfts))
}
Signing Transactions
In order to execute transactions on your contract, the SDK needs to know the wallet that is performing those transactions. For that it needs the private key of the wallet you want to execute transactions from.
⚠ Never commit private keys to file tracking history, or your account could be compromised.
To connect your wallet to the SDK, you can use the following setup:
package main
import (
"fmt"
"encoding/json"
"github.com/mry-am/thirdweb-go-sdk/v2/thirdweb"
)
func main() {
// Get your private key securely (preferably from an environment variable)
privateKey := "..."
// Instantiate the SDK with your privateKey
sdk, err := thirdweb.NewThirdwebSDK("mumbai", &thirdweb.SDKOptions{
PrivateKey: privateKey,
})
if err != nil {
panic(err)
}
// Replace your contract address here
address := "0x..."
nft, err := sdk.GetNFTCollection(address)
if err != nil {
panic(err)
}
// Now you can execute transactions using the SDK contract functions
tx, err := nft.Mint(
&thirdweb.NFTMetadataInput{
Name: "Test NFT",
Description: "Minted with the thirdweb Go SDK",
Image: "ipfs://QmcCJC4T37rykDjR6oorM8hpB9GQWHKWbAi2YR1uTabUZu/0",
},
)
if err != nil {
panic(err)
}
result, _ := json.Marshal(&tx)
fmt.Println(string(result))
}