The .NET Standard SDK uses Watson services, a collection of REST APIs that use cognitive computing to solve complex problems.
- Before you begin
- Installing the Watson .NET Standard SDK
- Documentation
- Questions
- Open Source @ IBM
- License
- Contributing
Ensure you have the following prerequisites:
- You need an IBM Cloud account.
- Install Visual Studio for Windows or Visual Studio Code for OSX or Linux.
- Install .NET Core.
This SDK provides classes and methods to access the following Watson services:
- Assistant
- Compare Comply
- Discovery
- Language Translator
- Natural Language Understanding
- Natural Language Classifier
- Personality Insights
- Speech to Text
- Text to Speech
- Tone Analyzer
- Visual Recognition
You can get the latest SDK packages through NuGet or manually here.
Watson services are migrating to token-based Identity and Access Management (IAM) authentication.
- With some service instances, you authenticate to the API by using IAM.
- In other instances, you authenticate by providing the username and password for the service instance.
- If you're using a Watson service on ICP, you'll need to authenticate in a specific way.
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:
- Go to the IBM Cloud Dashboard page.
- Either click an existing Watson service instance or click Create resource > AI and create a service instance.
- Copy the
url
and eitherapikey
orusername
andpassword
. Click Show if the credentials are masked.
In your code, you can use these values in the service constructor or with a method call after instantiating your service.
Some services use 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:
- 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.
- Use the access token if you want to manage the lifecycle yourself. For details, see Authenticating with IAM tokens. If you want to switch to API key override your stored IAM credentials with an IAM API key.
void Example()
{
IamAuthenticator authenticator = new IamAuthenticator(
apikey: "{apikey}");
var service = new AssistantService("{versionDate}", authenticator);
service.SetServiceUrl("{serviceUrl}");
}
void Example()
{
BearerTokenAuthenticator authenticator = new BearerTokenAuthenticator(
bearerToken: "{bearerToken}");
var service = new AssistantService("{versionDate}", authenticator);
service.SetServiceUrl("{serviceUrl}");
}
void Example()
{
BasicAuthenticator authenticator = new BasicAuthenticator(
username: "{username}",
password: "{password}");
var service = new AssistantService("{versionDate}", authenticator);
service.SetServiceUrl("{serviceUrl}");
}
Like IAM, you can pass in credentials to let the SDK manage an access token for you or directly supply an access token to do it yourself.
void Example()
{
CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator(
url: "https://{cp4d_cluster_host}{:port}",
username: "{username}",
password: "{password}");
var service = new AssistantService("{version-date}", authenticator);
service.SetServiceUrl("{serviceUrl}");
var results = service.Message("{workspace-id}", "{message-request}");
}
void Example()
{
BearerTokenAuthenticator authenticator = new BearerTokenAuthenticator(
bearerToken: "{bearerToken}");
var service = new AssistantService("{version-date}", authenticator);
service.SetServiceUrl("{serviceUrl}");
var results = service.Message("{workspace-id}", "{message-request}");
}
Be sure to both disable SSL verification when authenticating and set the endpoint explicitly to the URL given in ICP.
There are two ways to supply the credentials you found above to the SDK for authentication.
With a credential file, you just need to put the file in the right place and the SDK will do the work of parsing it 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):
- The top-level directory of the project you're using the SDK in
- Your system's home directory
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:
AssistantService service = new AssistantService("{version-date}");
var listWorkspacesResult = service.ListWorkspaces();
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
.
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.
You can send custom request headers by adding them to the service using .WithHeader({key}, {value})
.
void Example()
{
IamAuthenticator authenticator = new IamAuthenticator(
apikey: "{apikey}");
var service = new AssistantService("{version-date}", authenticator);
service.SetServiceUrl("{serviceUrl}");
service.WithHeader("X-Watson-Metadata", "customer_id=some-assistant-customer-id");
var results = service.Message("{workspace-id}", "{message-request}");
}
You can get the response headers, status code and the raw json response in the result object.
void Example()
{
IamAuthenticator authenticator = new IamAuthenticator(
apikey: "{apikey}");
var service = new AssistantService("{version-date}", authenticator);
service.SetServiceUrl("{serviceUrl}");
var results = service.Message("{workspace-id}", "{message-request}");
var responseHeaders = results.Headers; // The response headers
var responseJson = results.Response; // The raw response json
var statusCode = results.StatusCode; // The response status code
}
You can disable SSL verification on calls to Watson (only do this if you really mean to!).
void Example()
{
CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator(
url: "https://{cp4d_cluster_host}{:port}",
username: "{username}",
password: "{password}",
disableSslVerification: true);
var service = new AssistantService("{version-date}", authenticator);
service.SetServiceUrl("{serviceUrl}");
var results = service.Message("{workspace-id}", "{message-request}");
}
Discovery v2 is only available on Cloud Pak for Data.
Click here for documentation by release and branch.
If you are having difficulties using the APIs or have a question about the IBM Watson Services, please ask a question on dW Answers or Stack Overflow.
Find more open source projects on the IBM Github Page.
This library is licensed under Apache 2.0. Full license text is available in LICENSE.
See CONTRIBUTING.md.
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.