How to Access Watson Studio Assets by API

2 min read

IBM Watson Studio provides you with the environment and tools to solve your business problems by collaboratively working with data.

You can choose the tools you need to analyze and visualize data, to cleanse and shape data, to ingest streaming data, or to create and train machine learning models.

Users can manage assets under the IBM Watson Studio project via the web interface, but you can also use a collection of Watson Data REST APIs associated with Watson Studio and Watson Knowledge Catalog to manage data-related assets and the people who need to use these assets. 

This post will outline how to manage the project assets through the API command line interface. Users who are interested in automating the process may benefit from this content.  

1. Create the user token 

The Watson Data API documentation provides instructions on creating an IAM bearer token.

To create a token in the IBM Cloud, log in to IBM Cloud and select Manage > Access (IAM) > IBM Cloud API Keys.

Create an API key for your own personal identity, copy the key value, and save it in a secure place. After you leave the page, you will no longer be able to access this value.

Create an API key for your own personal identity, copy the key value, and save it in a secure place.

Use this curl command to retrieve a token:

curl "https://iam.ng.bluemix.net/identity/token" \       
-d "apikey=YOUR_API_KEY_HERE&grant_type=urn%3Aibm%3Aparams%3Aoauth%3Agrant-type%3Aapikey" \       
-H "Content-Type: application/x-www-form-urlencoded" \       
-H "Authorization: Basic Yng6Yng="

Sample response:

{
        "access_token": "eyJraWQiOiIyMDE3MDgwOS0wMDowMDowMCIsImFsZyI6...",
        "refresh_token": "zmRTQFKhASUdF76Av6IUzi9dtB7ip8F2XV5fNgoRQ0mbQgD5XCeWkQhjlJ1dZi8K...",
        "token_type": "Bearer",
        "expires_in": 3600,
        "expiration": 1505865282
        }

Since, by default, the access token expires after an hour, it's easier to use a script to get the IAM access token and refresh it when you need to. 

A sample shell script:

api_key=<your API key>

curl -k -X POST \--header "Authorization: Basic Yng6Yng=" \
--header "Content-Type: application/x-www-form-urlencoded" \
--header "Accept: application/json" \
--data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey" \
--data-urlencode "${api_key}" \
-o token_resp.txt \
"https://iam.cloud.ibm.com/identity/token" 

token=`cat token_resp.txt | jq '. "access_token"'`
token=`echo "$token" | tr -d '"'`
echo "Authorization: Bearer $token" > wml_token.txt

Note: The script was created on MacOS—you might need to modify it to run correctly in other OS. 'jq' is a utility to format the output in JSON format. 

2. Get the data asset

Once you have retrieved the access token, you can use it to access the data asset. For example, you may get the data asset by using something similar to the following code:

project_id="<project id>"
data_asset_id="<data asset id>"
end_point="https://api.dataplatform.cloud.ibm.com"

curl -H 'accept: application/json' -H 'Content-Type: application/json' -H "$(cat wml_token.txt)" -X GET "${end_point}/v2/data_assets/${data_asset_id}?project_id=${project_id}"

The access token is read from the stored text file in Step 1 wml_token.txt.

Tip: You can get the data_asset_id and project_id from the URL of the Watson Studio interface:

Tip: You can get the data_asset_id and project_id from the URL of the Watson Studio interface:

Click on the data file GoSales.csv and you can see the URL in the following screenshot:

Click on the data file 'GoSales.csv' and you can see the URL in the following screenshot:

Sample output:

Sample output:

3. (Optional) Delete the asset

Run the following curl command to delete the asset if you wish:

curl -H 'accept: application/json' -H 'Content-Type: application/json' -H "$(cat wml_token.txt)" -X DELETE "${end_point}/v2/data_assets/${data_asset_id}?project_id=${project_id}"

Summary

The Watson Data API provides a comprehensive list of functions to manage the assets, and most GUI interface based functions can also be implemented with the API command line.

You can also follow these guidelines to access other various assets in Watson Studio, such as projects, catalogs, buckets, etc. For further information on the API, please refer to the online documentation at IBM Watson Data API.

Be the first to hear about news, product updates, and innovation from IBM Cloud