Getting Started
This guide walks you through creating your first Starknet.go application. You'll build a simple program that connects to Starknet and retrieves blockchain data.
Prerequisites
- Go 1.23 or higher installed
- Starknet.go installed in your project
- Basic understanding of Go programming
- A Starknet RPC endpoint (you can get one from Alchemy, Infura, or run your own Juno node)
Your First Application
We'll create an application that connects to Starknet and retrieves the latest block information.
Step 1: Project Setup
Create a new directory and initialize a Go module:
mkdir my-starknet-app
cd my-starknet-app
go mod init my-starknet-appAdd Starknet.go and godotenv dependencies:
go get github.com/NethermindEth/starknet.go
go get github.com/joho/godotenv
go mod tidyStep 2: Configure Environment
Create a .env file in your project root:
STARKNET_RPC_URL=<your-rpc-endpoint-url>Step 3: Create the Application
Create main.go with the following code:
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/joho/godotenv"
)
func main() {
// Load environment variables from .env file
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
rpcURL := os.Getenv("STARKNET_RPC_URL")
if rpcURL == "" {
log.Fatal("STARKNET_RPC_URL environment variable is not set")
}
ctx := context.Background()
// Connect to Starknet
client, err := rpc.NewProvider(ctx, rpcURL)
if err != nil {
log.Fatal("Failed to create client:", err)
}
// Get chain information
chainID, err := client.ChainID(ctx)
if err != nil {
log.Fatal("Failed to get chain ID:", err)
}
blockNumber, err := client.BlockNumber(ctx)
if err != nil {
log.Fatal("Failed to get block number:", err)
}
fmt.Printf("Connected to Starknet\n")
fmt.Printf("Chain ID: %s\n", chainID)
fmt.Printf("Latest block: %d\n", blockNumber)
}Step 4: Run the Application
go run main.goExpected output:
Connected to Starknet
Chain ID: SN_SEPOLIA
Latest block: 1506321Understanding the Code
Environment Variables: Using .env files keeps sensitive configuration like RPC URLs out of your source code. The godotenv package loads these variables at runtime.
RPC Client: The rpc.NewProvider() function creates a client that communicates with Starknet nodes via JSON-RPC. It requires a context and the RPC endpoint URL.
Context: Go's context package manages request timeouts and cancellation. Use context.Background() for simple operations.
Error Handling: Always check for errors when making RPC calls, as network operations can fail.
Next Steps
Now that you have a basic application working, you can explore more features:
- Client Overview - Learn about the Starknet client
- Client Methods - Explore available methods for interacting with Starknet
- RPC Methods - See all available RPC methods
Troubleshooting
If you encounter any issues:
- Make sure your Starknet node URL is correct and accessible
- Check that you have the latest version of Starknet.go installed
- Verify your Go version meets the minimum requirement (1.20 or higher)
- Avoid naming your module after Go standard library packages

