The Swan SDK is a toolkit designed to simplify interactions with the Swan Chain Network Resource. It provides a streamlined interface for creating and managing computational tasks, retrieving hardware information, processing payments, and monitoring task statuses.
{% hint style="info" %} For more sample tutorial, please refer to Python Swan SDK Samples or Go Swan SDK Samples. {% endhint %}
To use swan-sdk
, a 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
{% tabs %} {% tab title="Python" %}
To use Python Swan SDK, you first need to install it and its dependencies. Before installing Swan SDK, install Python 3.8 or later and web3.py(==6.20.3).
Install the latest Swan SDK release via pip:
pip install swan-sdk
Or install via GitHub:
git clone https://github.com/swanchain/python-swan-sdk.git
cd python-swan-sdk
pip install .
To use Python Swan SDK, you must first import it and indicate which service you're going to use:
import swan
swan_orchestrator = swan.resource(api_key='<SWAN_API_KEY>', service_name='Orchestrator')
Now that you have an Orchestrator
service, you can create and deploy instance applications as an Orchestrator task with the service.
result = swan_orchestrator.create_task(
repo_uri='https://github.com/swanchain/awesome-swanchain/tree/main/hello_world', # Or your own repo URI
wallet_address='<WALLET_ADDRESS>',
private_key='<PRIVATE_KEY>',
instance_type='C1ae.medium'
)
task_uuid = result['task_uuid']
Then you can follow up task deployment information and the URL for running applications.
# Get task deployment info
task_deployment_info = swan_orchestrator.get_deployment_info(task_uuid=task_uuid)
print(task_deployment_info)
# Get application instances URL
app_urls = swan_orchestrator.get_real_url(task_uuid)
print(app_urls)
{% endtab %}
{% tab title="Go" %}
Go Version
go-swan-sdk
requires Go version 1.21 or above.
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.medium",
})
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)
}
{% endtab %} {% endtabs %}
More resources about swan SDK can be found here: