This guide details the steps needed to install or update the SWAN SDK for Go. The SDK is a comprehensive toolkit designed to facilitate seamless interactions with the SwanChain API.
Go Version
go-swan-sdk
requires Go version 1.21 or above.
Swan API Key
To use swan-sdk
, an Swan API key is required. Steps to get an API Key:
- Go to Swan Console, switch network to Swan Chain Mainnet.
- Login with your wallet.
- Click
API Keys
->Generate API Key
Using go-swan-sdk
With Go's module support, go [build|run|test]
automatically fetches the necessary dependencies when you add import
in your project:
import "github.com/swanchain/go-swan-sdk"
To update the SDK use go get -u
to retrieve the latest version of the SDK:
go get -u github.com/swanchain/go-swan-sdk
To use go-swan-sdk
, you must first import it, and you can create and deploy instance applications quickly.
package main
import (
"github.com/swanchain/go-swan-sdk"
"log"
"time"
)
func main() {
client, err := swan.NewAPIClient("<YOUR_API_KEY>")
if err != nil {
log.Fatalf("failed to init swan client, error: %v \n", err)
}
task, err := client.CreateTask(&swan.CreateTaskReq{
PrivateKey: "<PRIVATE_KEY>",
RepoUri: "https://github.com/swanchain/awesome-swanchain/tree/main/hello_world",
Duration: 2 * time.Hour,
InstanceType: "C1ae.small",
})
taskUUID := task.Task.UUID
// Get task deployment info
resp, err := client.TaskInfo(taskUUID)
if err != nil {
log.Fatalln(err)
}
log.Printf("task info: %+v \n", resp)
//Get application instances URL
appUrls, err := client.GetRealUrl(taskUUID)
if err != nil {
log.Fatalln(err)
}
log.Printf("app urls: %v \n", appUrls)
}