Skip to content

Get started with Starknet.go

StarkNet.go is a comprehensive Go library that facilitates interaction with the StarkNet blockchain network. StarkNet is a permissionless, decentralized ZK-Rollup operating as a Layer 2 scaling solution for Ethereum.

Prerequisites

Before you begin, make sure you have:

Overview

Starknet.go enables Go backend applications and WASM frontends to seamlessly connect with Starknet by providing abstractions for the Starknet RPC, account managements and wallet operations.

Creating Your First Starknet.go Application

Let's create a simple application that connects to Starknet and retrieves the latest block number.

Step 1: Set up your project

Create a new directory for your project and initialize a Go module:

Terminal
mkdir my-starknet-app
cd my-starknet-app
go mod init my-starknet-app

Step 2: Install dependencies

Add Starknet.go to your project:

Terminal
go get github.com/NethermindEth/starknet.go

Step 3: Create the main application

Create a new file main.go with the following code:

main.go
package main
 
import (
    "context"
    "fmt"
    "log"
 
    "github.com/NethermindEth/starknet.go/rpc"
)
 
func main() {
    // Initialize connection to RPC provider
    rpcUrl := "https://free-rpc.nethermind.io/mainnet-juno/"
    client, err := rpc.NewProvider(rpcUrl)
    if err != nil {
        log.Fatal(err)
    }
 
    fmt.Println("Established connection with the client")
 
    // Get the latest block number
    blockNumber, err := client.BlockNumber(context.Background())
    if err != nil {
        log.Fatal(err)
    }
 
    fmt.Printf("Latest block number: %d\n", blockNumber)
}

Step 4: Run the application

Run your application:

Terminal
go run main.go

You should see output similar to:

Established connection with the client
Latest block number: 123456

Next Steps

Now that you have a basic application working, you can explore more features:

  1. Client Configuration - Learn how to configure your Starknet client
  2. Client Methods - Explore available methods for interacting with Starknet
  3. Examples - See more examples of using Starknet.go

Troubleshooting

If you encounter any issues:

  1. Make sure your Starknet node URL is correct and accessible
  2. Check that you have the latest version of Starknet.go installed
  3. Verify your Go version meets the minimum requirement (1.20 or higher)
  4. Check the documentation for more detailed setup instructions