The official Go SDK for the Harness Platform. This SDK provides programmatic access to Harness services including CI/CD pipelines, feature flags, cloud cost management, chaos engineering, and more.
This product is not supported by the Harness Customer support team. If you have any questions please open a new issue or join our slack channel.
go get github.com/harness/harness-go-sdkTo update to the latest version:
go get -u github.com/harness/harness-go-sdk@latestThe SDK is organized into the following packages:
| Package | Description | Documentation |
|---|---|---|
harness/nextgen |
NextGen Platform APIs (pipelines, connectors, secrets, GitOps, etc.) | API Docs |
harness/cd |
FirstGen Continuous Delivery APIs | FirstGen Docs |
harness/chaos |
Chaos Engineering APIs | Chaos Docs |
harness/code |
Code Repository APIs | Code Docs |
harness/helpers |
Common helper utilities | - |
harness/utils |
Utility functions | - |
logging |
Logging utilities | - |
Most users should use the nextgen package for interacting with the Harness Platform:
package main
import (
"context"
"fmt"
"os"
"github.com/harness/harness-go-sdk/harness/nextgen"
)
func main() {
// Create configuration
cfg := nextgen.NewConfiguration()
cfg.BasePath = "https://app.harness.io/gateway"
cfg.AddDefaultHeader("x-api-key", os.Getenv("HARNESS_API_KEY"))
// Create client
client := nextgen.NewAPIClient(cfg)
// List organizations
accountId := os.Getenv("HARNESS_ACCOUNT_ID")
resp, _, err := client.OrganizationApi.GetOrganizationList(
context.Background(),
accountId,
&nextgen.OrganizationApiGetOrganizationListOpts{},
)
if err != nil {
panic(err)
}
for _, org := range resp.Data.Content {
fmt.Println(org.Organization.Name)
}
}For FirstGen Harness, use the cd package:
package main
import (
"fmt"
"os"
"github.com/harness/harness-go-sdk/harness/cd"
)
func main() {
// Create client
client, err := cd.NewClient(&cd.Config{
AccountId: os.Getenv("HARNESS_ACCOUNT_ID"),
APIKey: os.Getenv("HARNESS_API_KEY"),
Endpoint: "https://app.harness.io",
})
if err != nil {
panic(err)
}
// Get application by name
app, err := client.ApplicationClient.GetApplicationByName("my-app")
if err != nil {
panic(err)
}
fmt.Println(app.Id, app.Name)
}| Variable | Required | Description |
|---|---|---|
HARNESS_ACCOUNT_ID |
Yes | Your Harness account identifier |
HARNESS_API_KEY |
Yes | API key for authentication |
HARNESS_ENDPOINT |
No | API endpoint (default: https://app.harness.io) |
HARNESS_BEARER_TOKEN |
No | Bearer token for certain FirstGen APIs (deprecated) |
- Go to the Harness Platform
- Navigate to Account Settings → Access Control → API Keys
- Click + API Key and create a new key
- Save the token securely - it won't be shown again
connector := nextgen.Connector{
Name: "my-github-connector",
Identifier: "my_github_connector",
Type_: "Github",
Spec: map[string]interface{}{
"url": "https://github.com/myorg",
"authentication": map[string]interface{}{
"type": "Http",
"spec": map[string]interface{}{
"type": "UsernameToken",
"spec": map[string]interface{}{
"username": "myuser",
"tokenRef": "account.github_token",
},
},
},
},
}
resp, _, err := client.ConnectorsApi.CreateConnector(
context.Background(),
connector,
accountIdentifier,
nil,
)secret := nextgen.Secret{
Name: "my-secret",
Identifier: "my_secret",
Type_: "SecretText",
Spec: map[string]interface{}{
"valueType": "Inline",
"value": "my-secret-value",
"secretManagerIdentifier": "harnessSecretManager",
},
}
resp, _, err := client.SecretsApi.CreateSecret(
context.Background(),
secret,
accountIdentifier,
nil,
)resp, _, err := client.PipelinesApi.GetPipelineList(
context.Background(),
accountIdentifier,
orgIdentifier,
projectIdentifier,
&nextgen.PipelinesApiGetPipelineListOpts{
PageIndex: optional.NewInt32(0),
PageSize: optional.NewInt32(20),
},
)API errors include the HTTP response for additional context:
resp, httpResp, err := client.OrganizationApi.GetOrganization(
context.Background(),
orgIdentifier,
accountIdentifier,
nil,
)
if err != nil {
if httpResp != nil {
fmt.Printf("HTTP Status: %d\n", httpResp.StatusCode)
body, _ := io.ReadAll(httpResp.Body)
fmt.Printf("Response: %s\n", string(body))
}
return err
}List endpoints support pagination:
opts := &nextgen.OrganizationApiGetOrganizationListOpts{
PageIndex: optional.NewInt32(0),
PageSize: optional.NewInt32(50),
}
resp, _, err := client.OrganizationApi.GetOrganizationList(ctx, accountId, opts)
// Check for more pages
if resp.Data.TotalPages > 1 {
// Fetch additional pages...
}import (
"net/http"
"time"
retryablehttp "github.com/hashicorp/go-retryablehttp"
)
// Create a retryable HTTP client
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = 3
retryClient.RetryWaitMin = 1 * time.Second
retryClient.RetryWaitMax = 5 * time.Second
cfg := nextgen.NewConfiguration()
cfg.HTTPClient = retryClient.StandardClient()Enable debug logging for troubleshooting:
import "github.com/harness/harness-go-sdk/logging"
logger := logging.NewLogger()
logger.SetLevel(log.DebugLevel)
// Use with FirstGen client
client, _ := cd.NewClient(&cd.Config{
AccountId: accountId,
APIKey: apiKey,
DebugLogging: true,
Logger: logger,
})This SDK uses Go modules. To get a specific version:
go get github.com/harness/[email protected]Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
This SDK is licensed under the Apache 2.0 License. See LICENSE.md for details.