{"id":13801134,"url":"https://github.com/watson-developer-cloud/go-sdk","last_synced_at":"2025-04-24T07:32:45.854Z","repository":{"id":33958473,"uuid":"152775148","full_name":"watson-developer-cloud/go-sdk","owner":"watson-developer-cloud","description":":mouse: go SDK for the IBM Watson services.","archived":false,"fork":false,"pushed_at":"2023-05-16T18:05:38.000Z","size":19357,"stargazers_count":71,"open_issues_count":3,"forks_count":25,"subscribers_count":17,"default_branch":"master","last_synced_at":"2024-08-05T00:05:59.620Z","etag":null,"topics":["hacktoberfest"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/watson-developer-cloud.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-10-12T15:57:29.000Z","updated_at":"2024-07-10T13:06:42.000Z","dependencies_parsed_at":"2024-01-05T20:57:33.624Z","dependency_job_id":"7b032a74-5481-4946-ba59-960779759de2","html_url":"https://github.com/watson-developer-cloud/go-sdk","commit_stats":null,"previous_names":[],"tags_count":42,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watson-developer-cloud%2Fgo-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watson-developer-cloud%2Fgo-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watson-developer-cloud%2Fgo-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/watson-developer-cloud%2Fgo-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/watson-developer-cloud","download_url":"https://codeload.github.com/watson-developer-cloud/go-sdk/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223945869,"owners_count":17229725,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest"],"created_at":"2024-08-04T00:01:19.901Z","updated_at":"2025-04-24T07:32:45.847Z","avatar_url":"https://github.com/watson-developer-cloud.png","language":"Go","funding_links":[],"categories":["Data \u0026 AI"],"sub_categories":[],"readme":"# Watson Developer Cloud Go SDK v3.0.0\n[![Build and Test](https://github.com/watson-developer-cloud/go-sdk/workflows/Build%20and%20Test/badge.svg?branch=master)](https://github.com/watson-developer-cloud/go-sdk/actions?query=workflow%3A\"Build+and+Test\")\n[![Deploy and Publish](https://github.com/watson-developer-cloud/go-sdk/workflows/Deploy%20and%20Publish/badge.svg?branch=master)](https://github.com/watson-developer-cloud/go-sdk/actions?query=workflow%3A%22Deploy+and+Publish%22)\n[![Release](https://img.shields.io/github/v/release/watson-developer-cloud/go-sdk)](https://github.com/watson-developer-cloud/go-sdk/releases/latest)\n![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/watson-developer-cloud/go-sdk?filename=v2/go.mod)\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)\n[![wdc-community.slack.com](https://wdc-slack-inviter.mybluemix.net/badge.svg)](http://wdc-slack-inviter.mybluemix.net/)\n\n## Deprecation Notice\n\nThis repo is being deprecated. While this repo will no longer see active support from the IBM team, contributions by the community via PRs are still welcomed and will be reviewed.\n\n## Announcements\n### Natural Language Classifier deprecation\nOn 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.\n\nAs 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](https://cloud.ibm.com/docs/natural-language-classifier?topic=natural-language-classifier-migrating).\n\n### Updating endpoint URLs from watsonplatform.net\nWatson 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.\n\n### Personality Insights deprecation\nIBM 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.\n\nAs an alternative, we encourage you to consider migrating to IBM Watson™ [Natural Language Understanding](https://cloud.ibm.com/docs/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.\n\n### Visual Recognition deprecation\nIBM 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.\n\n### Compare and Comply deprecation\nIBM 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.\n\n## Before you begin\n\n* You need an [IBM Cloud][ibm-cloud-onboarding] account.\n\n## Installation\n\nGet SDK package:\n```bash\ngo get -u github.com/watson-developer-cloud/go-sdk/v2\n```\n\nNote: For the latest tag release, look into [examples][examples] folder for basic and advanced examples.\n\n## Running in IBM Cloud\n\nIf you run your app in IBM Cloud, the SDK gets credentials from the ```VCAP_SERVICES``` environment variable.\n\n## Authentication\n\nWatson services are migrating to token-based Identity and Access Management (IAM) authentication.\n\n* With some service instances, you authenticate to the API by using [IAM](#iam).\n* In other instances, you authenticate by providing the [username and password](#username-and-password) for the service instance.\n\n### Getting credentials\nTo find out which authentication to use, view the service credentials. You find the service credentials for authentication the same way for all Watson services:\n\n1. Go to the IBM Cloud [Dashboard](https://cloud.ibm.com/) page.\n1. Either click an existing Watson service instance in your [resource list](https://cloud.ibm.com/resources) or click [**Create resource \u003e AI**](https://cloud.ibm.com/catalog?category=ai) and create a service instance.\n1. Click on the **Manage** item in the left nav bar of your service instance.\n\nOn this page, you should be able to see your credentials for accessing your service instance.\n\n### Supplying credentials\n\nThere are two ways to supply the credentials you found above to the SDK for authentication.\n\n#### Credential file\n\nWith 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.\n\nThe 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):\n\n- The top-level directory of the project you're using the SDK in\n- Your system's home directory\n\nAs 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:\n\n```go\nimport \"github.com/watson-developer-cloud/go-sdk/v2/servicev1\"\n\nservice, serviceErr := servicev1.NewServiceV1(\u0026servicev1.ServiceV1Options{\n\tVersion:   \"2018-03-05\",\n})\n```\n\nAnd that's it!\n\nIf 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.\n\nIf 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:\n\n```bash\nexport IBM_CREDENTIALS_FILE=\"\u003cpath\u003e\"\n```\n\nwhere `\u003cpath\u003e` is something like `/home/user/Downloads/\u003cfile_name\u003e.env`.\n\n#### Manually\nIf 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.\n\n### IAM\nIBM 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.\n\nYou supply either an IAM service **API key** or an **access token**:\n\n* Use the API key to have the SDK manage the lifecycle of the access token. The SDK requests an access token, ensures that the access token is valid, and refreshes it if necessary.\n\n* Use the access token if you want to manage the lifecycle yourself. For details, see [Authenticating with IAM tokens](https://cloud.ibm.com/docs/watson?topic=watson-iam#iam).\n\n**Supplying the IAM API key**\n\n```go\n// In the constructor, letting the SDK manage the IAM token\nimport \"github.com/watson-developer-cloud/go-sdk/v2/servicev1\"\nauthenticator := \u0026core.IamAuthenticator{\n\tApiKey: \"\u003capikey\u003e\",\n}\nservice, serviceErr := servicev1.NewServiceV1(\u0026servicev1.ServiceV1Options{\n\t\tURL:       \"\u003cservice_url\u003e\",\n\t\tAuthenticator: authenticator,\n\t})\n```\n\n### Username and password\n\n```go\n// In the constructor\nimport \"github.com/watson-developer-cloud/go-sdk/v2/servicev1\"\n\nservice, serviceErr := servicev1.NewServiceV1(\u0026servicev1.ServiceV1Options{\n\t\tURL:      \"\u003cservice_url\u003e\",\n\t\tAuthenticator: \u0026core.BasicAuthenticator{\n\t\t\tUsername: \"\u003cusername\u003e\",\n\t\t\tPassword: \"\u003cpassword\u003e\",\n\t\t},\n\t})\n```\n\n## Use\nApply these general steps for services present in various packages\n1. Import the service package\n2. Create a new service instance and pass in credentials using either of [authentication](#authentication) methods\n3. Invoke API methods using the service instance. For a successful response, it will contain the HTTP status code, response headers and API result\n4. Handle responses and errors\n\n```go\n\npackage main\n\nimport (\n\"fmt\"\n\"github.com/watson-developer-cloud/go-sdk/v2/discoveryv1\"\n)\n\n// Creates a Discovery service instance and does a list of environments\nfunc main() {\n// Instantiate the Watson Discovery service\nservice, serviceErr := discoveryv1.\n  NewDiscoveryV1(\u0026discoveryv1.DiscoveryV1Options{\n    Version:   \"2018-03-05\",\n\tAuthenticator: \u0026core.IamAuthenticator{\n\t\tApikey: \"YOUR APIKEY\",\n\t},\n  })\nservice.SetServiceURL(\"YOUR SERVICE URL\")\n\n// Check successful instantiation\nif serviceErr != nil {\n  panic(serviceErr)\n}\n\n// Create a new ListEnvironmentsOptions, these are helper methods.\nlistEnvironmentsOptions := service.NewListEnvironmentsOptions()\n\n// Call the discovery ListEnvironments method\nlistEnvironmentResult, response, responseErr := service.ListEnvironments(listEnvironmentsOptions)\n// Or you can directly pass in the model options\n// listEnvironmentResult, response, responseErr := service.ListEnvironments(\u0026discoveryv1.ListEnvironmentsOptions{})\n\n// Check successful call\nif responseErr != nil {\n  panic(responseErr)\n}\n\n// This will return the `DetailedResponse`\nfmt.Println(response)\n// Get the HTTP status code\nresponse.GetStatusCode()\n// Get the response headers\nresponse.GetHeaders()\n// Get the API response\nresponse.GetResult()\n\n// Directly access the result\nif listEnvironmentResult != nil {\n  fmt.Println(listEnvironmentResult.Environments[0])\n  }\n}\n```\n\n## Configuring the HTTP Client\n\nTo 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](https://pkg.go.dev/net/http). Below is an example to pass a proxy\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"log\"\n  \"net/http\"\n  \"net/url\"\n\n  discovery \"github.com/watson-developer-cloud/go-sdk/v2/discoveryv1\"\n)\n\nfunc main() {\n  // Instantiate the Watson Discovery service\n  discoveryService, discoveryServiceErr := discovery.NewDiscoveryV1(\u0026discovery.DiscoveryV1Options{\n    URL:       \"YOUR SERVICE URL\",\n    Version:   \"2018-03-05\",\n\tAuthenticator: \u0026core.IamAuthenticator{\n\t\tApikey: \"YOUR APIKEY\",\n\t},\n  })\n  // Check successful instantiation\n  if discoveryServiceErr != nil {\n    fmt.Println(discoveryServiceErr)\n    return\n  }\n\n  //creating the proxyURL\n  proxyStr := \"http://\u003cproxy host\u003e:\u003cport\u003e\"\n  proxyURL, err := url.Parse(proxyStr)\n  if err != nil {\n    log.Println(err)\n  }\n\n  //adding the proxy settings to the Transport object\n  transport := \u0026http.Transport{\n    Proxy: http.ProxyURL(proxyURL),\n  }\n\n  //adding the Transport object to the http Client\n  client := \u0026http.Client{\n    Transport: transport,\n  }\n\n  discoveryService.Service.SetHTTPClient(client)\n}\n```\n\n## Disable SSL certificate verification\nDisable the SSL verification using `DisableSSLVerification()` method\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  discovery \"github.com/watson-developer-cloud/go-sdk/v2/discoveryv1\"\n)\n\nfunc main() {\n  // Instantiate the Watson Discovery service\n  discoveryService, discoveryServiceErr := discovery.NewDiscoveryV1(\u0026discovery.DiscoveryV1Options{\n    URL:       \"YOUR SERVICE URL\",\n    Version:   \"2018-03-05\",\n\tAuthenticator: \u0026core.IamAuthenticator{\n\t\tApikey: \"YOUR APIKEY\",\n\t},\n  })\n  // Check successful instantiation\n  if discoveryServiceErr != nil {\n    fmt.Println(discoveryServiceErr)\n    return\n  }\n\n  discoveryService.Service.DisableSSLVerification()\n}\n```\n\nOr can set it from external sources. For example, environment variables:\n\n```bash\nexport \u003cYOUR SERVICE NAME\u003e_DISABLE_SSL=True\n\n## Set Service URL\nTo set the service URL,\n\n```go\nservice.SetServiceURL(\"my new url\")\n```\n\nOr can set it from external sources. For example, environment variables:\n\n```bash\nexport \u003cYOUR SERVICE NAME\u003e_URL=\"my new url\"\n```\n\n## Using the global transaction ID\nEvery 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.\n\n```go\nresult, response, responseErr := service.MyServiceCall(myServiceOptions)\nfmt.Println(response.GetHeaders().Get(\"X-Global-Transaction-Id\"))\n```\n\nAdditionally, 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.\n\n```go\nservice, err = assistantv1.NewAssistantV1(\u0026assistantv1.AssistantV1Options{\n\tVersion:     \"2020-04-01\",\n\tServiceName: \"assistant\",\n})\n\ncustomHeaders := http.Header{}\ncustomHeaders.Add(\"X-Global-Transaction-Id\", \"my-custom-value\")\nservice.Service.SetDefaultHeaders(customHeaders)\n```\n\n## Cloud Pak for Data(CP4D)\nIf your service instance is of ICP4D, below are two ways of initializing the assistant service.\n\n1) Supplying the `Username`, `Password`, `ICP4DURL` and `AuthenticationType`\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  discovery \"github.com/watson-developer-cloud/go-sdk/v2/discoveryv1\"\n)\n\nfunc main() {\n  // Instantiate the Watson Discovery service\n\tauthenticator := \u0026core.CloudPakForDataAuthenticator{\n\t\tURL:                    \"CP4D URL FOR AUTHENTICATION\", // should be of the form https://{icp_cluster_host}\n\t\tUsername:               \"YOUR USERNAME\", \n\t\tPassword:               \"YOUR PASSWORD\",\n\t\tDisableSSLVerification: true,\n\t}\n  discoveryService, discoveryServiceErr := discovery.NewDiscoveryV1(\u0026discovery.DiscoveryV1Options{\n    URL:                \"YOUR SERVICE URL\", // should be of the form https://{icp_cluster_host}/{deployment}/discovery/{instance-id}/api\n    Version:            \"2018-03-05\",\n\tAuthenticator: authenticator,\n  })\n\n  // Check successful instantiation\n  if discoveryServiceErr != nil {\n    fmt.Println(discoveryServiceErr)\n    return\n  }\n\n  discoveryService.DisableSSLVerification() // MAKE SURE SSL VERIFICATION IS DISABLED\n}\n```\n\n2) Supplying the access token\n```go\npackage main\n\nimport (\n  \"fmt\"\n  discovery \"github.com/watson-developer-cloud/go-sdk/v2/discoveryv1\"\n)\n\nfunc main() {\n\tauthenticator := \u0026core.BearerTokenAuthenticator{\n\t\tBearerToken: \"YOUR BEARER TOKEN\",\n\t}\n\n  // Instantiate the Watson Discovery service\n  discoveryService, discoveryServiceErr := discovery.NewDiscoveryV1(\u0026discovery.DiscoveryV1Options{\n    URL:              \"YOUR SERVICE URL\", // should be of the form https://{icp_cluster_host}/{deployment}/discovery/{instance-id}/api\n    Version:          \"2018-03-05\",\n    Authenticator: authenticator,\n  })\n  // Check successful instantiation\n  if discoveryServiceErr != nil {\n    fmt.Println(discoveryServiceErr)\n    return\n  }\n\n  discoveryService.DisableSSLVerification() // MAKE SURE SSL VERIFICATION IS DISABLED\n}\n```\n## Examples\n\nThe [examples][examples] folder has basic and advanced examples. The examples within each service assume that you already have [service credentials](#getting-credentials).\n\n## Tests\n\nRun all test suites:\n```bash\ngo test ./...\n```\n\nGet code coverage for each test suite:\n```bash\ngo test -coverprofile=coverage.out ./...\ngo tool cover -html=coverage.out\n```\n\nRun a specific test suite:\n```bash\ngo test ./assistantv1\n```\n\n## Questions\n\nIf you have issues with the APIs or have a question about the Watson services, see [Stack Overflow](https://stackoverflow.com/questions/tagged/ibm-watson+go).\n\n## Migrating\n* To move from `v0.11.0` to `v1.0.0`, refer to the [MIGRATION-V1](https://github.com/watson-developer-cloud/go-sdk/blob/master/MIGRATION-V1.md)\n\n## Contributing\n\nSee [CONTRIBUTING][CONTRIBUTING].\n\n## Featured Projects\n\nWe'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.\n\n## License\n\nThis library is licensed under the [Apache 2.0 license][license].\n\n[ibm-cloud-onboarding]: https://cloud.ibm.com/registration?target=%2Fdeveloper%2Fwatson\u0026cm_sp=WatsonPlatform-WatsonServices-_-OnPageNavLink-IBMWatson_SDKs-_-Go\n[examples]: https://github.com/watson-developer-cloud/go-sdk/tree/master/examples\n[CONTRIBUTING]: https://github.com/watson-developer-cloud/go-sdk/blob/master/.github/CONTRIBUTING.md\n[license]: http://www.apache.org/licenses/LICENSE-2.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwatson-developer-cloud%2Fgo-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwatson-developer-cloud%2Fgo-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwatson-developer-cloud%2Fgo-sdk/lists"}