Home

Awesome

Watson Developer Cloud Go SDK v3.0.0

Build and Test Deploy and Publish Release GitHub go.mod Go version License semantic-release wdc-community.slack.com

Deprecated builds

Build Status

Go client library to quickly get started with the various Watson APIs services.

Announcements

Natural Language Classifier deprecation

On 9 August 2021, IBM announced the deprecation of the Natural Language Classifier service. The service will no longer be available from 8 August 2022. As of 9 September 2021, you will not be able to create new instances. Existing instances will be supported until 8 August 2022. Any instance that still exists on that date will be deleted.

As an alternative, we encourage you to consider migrating to the Natural Language Understanding service on IBM Cloud that uses deep learning to extract data and insights from text such as keywords, categories, sentiment, emotion, and syntax, along with advanced multi-label text classification capabilities, to provide even richer insights for your business or industry. For more information, see Migrating to Natural Language Understanding.

Updating endpoint URLs from watsonplatform.net

Watson API endpoint URLs at watsonplatform.net are changing and will not work after 26 May 2021. Update your calls to use the newer endpoint URLs. For more information, see https://cloud.ibm.com/docs/watson?topic=watson-endpoint-change.

Personality Insights deprecation

IBM Watson™ Personality Insights is discontinued. For a period of one year from 1 December 2020, you will still be able to use Watson Personality Insights. However, as of 1 December 2021, the offering will no longer be available.

As an alternative, we encourage you to consider migrating to IBM Watson™ Natural Language Understanding, a service on IBM Cloud® that uses deep learning to extract data and insights from text such as keywords, categories, sentiment, emotion, and syntax to provide insights for your business or industry. For more information, see About Natural Language Understanding.

Visual Recognition deprecation

IBM Watson™ Visual Recognition is discontinued. Existing instances are supported until 1 December 2021, but as of 7 January 2021, you can't create instances. Any instance that is provisioned on 1 December 2021 will be deleted.

Compare and Comply deprecation

IBM Watson™ Compare and Comply is discontinued. Existing instances are supported until 30 November 2021, but as of 1 December 2020, you can't create instances. Any instance that exists on 30 November 2021 will be deleted. Consider migrating to Watson Discovery Premium on IBM Cloud for your Compare and Comply use cases. To start the migration process, visit https://ibm.biz/contact-wdc-premium.

Before you begin

Installation

Get SDK package:

go get -u github.com/watson-developer-cloud/go-sdk/v2

Note: For the latest tag release, look into examples folder for basic and advanced examples.

Running in IBM Cloud

If you run your app in IBM Cloud, the SDK gets credentials from the VCAP_SERVICES environment variable.

Authentication

Watson services are migrating to token-based Identity and Access Management (IAM) authentication.

Getting credentials

To find out which authentication to use, view the service credentials. You find the service credentials for authentication the same way for all Watson services:

  1. Go to the IBM Cloud Dashboard page.
  2. Either click an existing Watson service instance in your resource list or click Create resource > AI and create a service instance.
  3. Click on the Manage item in the left nav bar of your service instance.

On this page, you should be able to see your credentials for accessing your service instance.

Supplying credentials

There are two ways to supply the credentials you found above to the SDK for authentication.

Credential file

With a credential file, you just need to put the file in the right place and the SDK will do the work of parsing and authenticating. You can get this file by clicking the Download button for the credentials in the Manage tab of your service instance.

The file downloaded will be called ibm-credentials.env. This is the name the SDK will search for and must be preserved unless you want to configure the file path (more on that later). The SDK will look for your ibm-credentials.env file in the following places (in order):

As long as you set that up correctly, you don't have to worry about setting any authentication options in your code. So, for example, if you created and downloaded the credential file for your Discovery instance, you just need to do the following:

import "github.com/watson-developer-cloud/go-sdk/v2/servicev1"

service, serviceErr := servicev1.NewServiceV1(&servicev1.ServiceV1Options{
	Version:   "2018-03-05",
})

And that's it!

If you're using more than one service at a time in your code and get two different ibm-credentials.env files, just put the contents together in one ibm-credentials.env file and the SDK will handle assigning credentials to their appropriate services.

If you would like to configure the location/name of your credential file, you can set an environment variable called IBM_CREDENTIALS_FILE. This will take precedence over the locations specified above. Here's how you can do that:

export IBM_CREDENTIALS_FILE="<path>"

where <path> is something like /home/user/Downloads/<file_name>.env.

Manually

If you'd prefer to set authentication values manually in your code, the SDK supports that as well. The way you'll do this depends on what type of credentials your service instance gives you.

IAM

IBM Cloud is migrating to token-based Identity and Access Management (IAM) authentication. IAM authentication uses a service API key to get an access token that is passed with the call. Access tokens are valid for approximately one hour and must be regenerated.

You supply either an IAM service API key or an access token:

Supplying the IAM API key

// In the constructor, letting the SDK manage the IAM token
import "github.com/watson-developer-cloud/go-sdk/v2/servicev1"
authenticator := &core.IamAuthenticator{
	ApiKey: "<apikey>",
}
service, serviceErr := servicev1.NewServiceV1(&servicev1.ServiceV1Options{
		URL:       "<service_url>",
		Authenticator: authenticator,
	})

Username and password

// In the constructor
import "github.com/watson-developer-cloud/go-sdk/v2/servicev1"

service, serviceErr := servicev1.NewServiceV1(&servicev1.ServiceV1Options{
		URL:      "<service_url>",
		Authenticator: &core.BasicAuthenticator{
			Username: "<username>",
			Password: "<password>",
		},
	})

Use

Apply these general steps for services present in various packages

  1. Import the service package
  2. Create a new service instance and pass in credentials using either of authentication methods
  3. Invoke API methods using the service instance. For a successful response, it will contain the HTTP status code, response headers and API result
  4. Handle responses and errors

package main

import (
"fmt"
"github.com/watson-developer-cloud/go-sdk/v2/discoveryv1"
)

// Creates a Discovery service instance and does a list of environments
func main() {
// Instantiate the Watson Discovery service
service, serviceErr := discoveryv1.
  NewDiscoveryV1(&discoveryv1.DiscoveryV1Options{
    Version:   "2018-03-05",
	Authenticator: &core.IamAuthenticator{
		Apikey: "YOUR APIKEY",
	},
  })
service.SetServiceURL("YOUR SERVICE URL")

// Check successful instantiation
if serviceErr != nil {
  panic(serviceErr)
}

// Create a new ListEnvironmentsOptions, these are helper methods.
listEnvironmentsOptions := service.NewListEnvironmentsOptions()

// Call the discovery ListEnvironments method
listEnvironmentResult, response, responseErr := service.ListEnvironments(listEnvironmentsOptions)
// Or you can directly pass in the model options
// listEnvironmentResult, response, responseErr := service.ListEnvironments(&discoveryv1.ListEnvironmentsOptions{})

// Check successful call
if responseErr != nil {
  panic(responseErr)
}

// This will return the `DetailedResponse`
fmt.Println(response)
// Get the HTTP status code
response.GetStatusCode()
// Get the response headers
response.GetHeaders()
// Get the API response
response.GetResult()

// Directly access the result
if listEnvironmentResult != nil {
  fmt.Println(listEnvironmentResult.Environments[0])
  }
}

Configuring the HTTP Client

To change client configs like timeout, setting proxy, etc, pass in your own client using the SetHTTPClient() method. Documentation for how to set your http client can be found in the Go net/http docs here. Below is an example to pass a proxy

package main

import (
  "fmt"
  "log"
  "net/http"
  "net/url"

  discovery "github.com/watson-developer-cloud/go-sdk/v2/discoveryv1"
)

func main() {
  // Instantiate the Watson Discovery service
  discoveryService, discoveryServiceErr := discovery.NewDiscoveryV1(&discovery.DiscoveryV1Options{
    URL:       "YOUR SERVICE URL",
    Version:   "2018-03-05",
	Authenticator: &core.IamAuthenticator{
		Apikey: "YOUR APIKEY",
	},
  })
  // Check successful instantiation
  if discoveryServiceErr != nil {
    fmt.Println(discoveryServiceErr)
    return
  }

  //creating the proxyURL
  proxyStr := "http://<proxy host>:<port>"
  proxyURL, err := url.Parse(proxyStr)
  if err != nil {
    log.Println(err)
  }

  //adding the proxy settings to the Transport object
  transport := &http.Transport{
    Proxy: http.ProxyURL(proxyURL),
  }

  //adding the Transport object to the http Client
  client := &http.Client{
    Transport: transport,
  }

  discoveryService.Service.SetHTTPClient(client)
}

Disable SSL certificate verification

Disable the SSL verification using DisableSSLVerification() method

package main

import (
  "fmt"
  discovery "github.com/watson-developer-cloud/go-sdk/v2/discoveryv1"
)

func main() {
  // Instantiate the Watson Discovery service
  discoveryService, discoveryServiceErr := discovery.NewDiscoveryV1(&discovery.DiscoveryV1Options{
    URL:       "YOUR SERVICE URL",
    Version:   "2018-03-05",
	Authenticator: &core.IamAuthenticator{
		Apikey: "YOUR APIKEY",
	},
  })
  // Check successful instantiation
  if discoveryServiceErr != nil {
    fmt.Println(discoveryServiceErr)
    return
  }

  discoveryService.Service.DisableSSLVerification()
}

Or can set it from external sources. For example, environment variables:

export <YOUR SERVICE NAME>_DISABLE_SSL=True

## Set Service URL
To set the service URL,

```go
service.SetServiceURL("my new url")

Or can set it from external sources. For example, environment variables:

export <YOUR SERVICE NAME>_URL="my new url"

Using the global transaction ID

Every SDK call returns a response with a transaction ID in the x-global-transaction-id header. This transaction ID is useful for troubleshooting and accessing relevant logs from your service instance.

result, response, responseErr := service.MyServiceCall(myServiceOptions)
fmt.Println(response.GetHeaders().Get("X-Global-Transaction-Id"))

Additionally, you can set a custom X-Global-Transaction-Id header in your request. This can make it useful if you need to provide a transaction ID to IBM support to help troubleshoot an issue.

service, err = assistantv1.NewAssistantV1(&assistantv1.AssistantV1Options{
	Version:     "2020-04-01",
	ServiceName: "assistant",
})

customHeaders := http.Header{}
customHeaders.Add("X-Global-Transaction-Id", "my-custom-value")
service.Service.SetDefaultHeaders(customHeaders)

Cloud Pak for Data(CP4D)

If your service instance is of ICP4D, below are two ways of initializing the assistant service.

  1. Supplying the Username, Password, ICP4DURL and AuthenticationType
package main

import (
  "fmt"
  discovery "github.com/watson-developer-cloud/go-sdk/v2/discoveryv1"
)

func main() {
  // Instantiate the Watson Discovery service
	authenticator := &core.CloudPakForDataAuthenticator{
		URL:                    "CP4D URL FOR AUTHENTICATION", // should be of the form https://{icp_cluster_host}
		Username:               "YOUR USERNAME", 
		Password:               "YOUR PASSWORD",
		DisableSSLVerification: true,
	}
  discoveryService, discoveryServiceErr := discovery.NewDiscoveryV1(&discovery.DiscoveryV1Options{
    URL:                "YOUR SERVICE URL", // should be of the form https://{icp_cluster_host}/{deployment}/discovery/{instance-id}/api
    Version:            "2018-03-05",
	Authenticator: authenticator,
  })

  // Check successful instantiation
  if discoveryServiceErr != nil {
    fmt.Println(discoveryServiceErr)
    return
  }

  discoveryService.DisableSSLVerification() // MAKE SURE SSL VERIFICATION IS DISABLED
}
  1. Supplying the access token
package main

import (
  "fmt"
  discovery "github.com/watson-developer-cloud/go-sdk/v2/discoveryv1"
)

func main() {
	authenticator := &core.BearerTokenAuthenticator{
		BearerToken: "YOUR BEARER TOKEN",
	}

  // Instantiate the Watson Discovery service
  discoveryService, discoveryServiceErr := discovery.NewDiscoveryV1(&discovery.DiscoveryV1Options{
    URL:              "YOUR SERVICE URL", // should be of the form https://{icp_cluster_host}/{deployment}/discovery/{instance-id}/api
    Version:          "2018-03-05",
    Authenticator: authenticator,
  })
  // Check successful instantiation
  if discoveryServiceErr != nil {
    fmt.Println(discoveryServiceErr)
    return
  }

  discoveryService.DisableSSLVerification() // MAKE SURE SSL VERIFICATION IS DISABLED
}

Examples

The examples folder has basic and advanced examples. The examples within each service assume that you already have service credentials.

Tests

Run all test suites:

go test ./...

Get code coverage for each test suite:

go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out

Run a specific test suite:

go test ./assistantv1

Questions

If you have issues with the APIs or have a question about the Watson services, see Stack Overflow.

Migrating

Contributing

See CONTRIBUTING.

Featured Projects

We'd love to highlight cool open-source projects that use this SDK! If you'd like to get your project added to the list, feel free to make an issue linking us to it.

License

This library is licensed under the Apache 2.0 license.