Go SDK

The Go SDK is available on GitHub and provides core functionality to interact with the Algorand blockchain using Go. The SDK includes several Go packages to work with the algod and kmd processes as well as some stand-alone functionality. The kmd and algod processes are described in the Node Overview documentation. The SDK provides HTTP clients for these two processes and stand-alone functionality for things like creating and signing transactions, message encoding and interacting with the protocol.

The GitHub repository's README contains several examples of using the SDK to perform tasks like creating a wallet, backing up and restoring a wallet, creating an account, signing and submitting a transaction, and viewing blocks on the blockchain. Full package documentation is available on Godocs.

Start the algod and kmd Processes

When using the SDK, it can interact with the algod, kmd or both processes. The algod process starts automatically when nodes are started. The kmd process automatically starts when called using the goal command line tool but with a 60-second timeout. This means it will shut down when no activity is detected for 60 seconds. To make sure the kmd is available for use with the SDK you can use the goal kmd command to start the kmd process with an indefinite timeout. Typically this will be just entering the command:

goal kmd start -d <data-dir>

Once the kmd is started, it will create  kmd.net and kmd.token files within the data directory for kmd. If you are using an RPM or DEB install these files will be in the ${HOME}/.algorand/kmd-version directory. The algod data directory also has the algod.net and algod.token files. These files contain the server and port number for the REST endpoints and API tokens that the SDK will need to communicate with their respective processes.

Install the SDK

To get the SDK just open a terminal on your machine and use the go get command.

go get -u github.com/algorand/go-algorand-sdk/...

Example

Open a new or existing Go project and use the SDK. For example, to get the status of a node use the following code.

import (
	"encoding/json"
	"fmt"

	"github.com/algorand/go-algorand-sdk/client/algod"
)
// These constants represent the algod REST endpoint and the corresponding
// API token. You can retrieve these from the `algod.net` and `algod.token`
// files in the algod data directory.
const algodAddress = "http://localhost:8080"
const algodToken = "e48a9bbe064a08f19cde9f0f1b589c1188b24e5059bc661b31bd20b4c8fa4ce7"

func main() {
	// Create an algod client
	algodClient, err := algod.MakeClient(algodAddress, algodToken)
	if err != nil {
		fmt.Printf("failed to make algod client: %s\n", err)
		return
	}
	// Get algod status
	nodeStatus, err := algodClient.Status()
	if err != nil {
		fmt.Printf("error getting algod status: %s\n", err)
		return
	}
	fmt.Printf("algod last round: %d\n", nodeStatus.LastRound)
	fmt.Printf("algod time since last round: %d\n", nodeStatus.TimeSinceLastRound)
	fmt.Printf("algod catchup: %d\n", nodeStatus.CatchupTime)
	fmt.Printf("algod latest version: %s\n", nodeStatus.LastVersion)	
}