March 13, 2020 By Jason Zhou 3 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.

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:

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

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.

Was this article helpful?
YesNo

More from Cloud

IBM Tech Now: April 8, 2024

< 1 min read - ​Welcome IBM Tech Now, our video web series featuring the latest and greatest news and announcements in the world of technology. Make sure you subscribe to our YouTube channel to be notified every time a new IBM Tech Now video is published. IBM Tech Now: Episode 96 On this episode, we're covering the following topics: IBM Cloud Logs A collaboration with IBM watsonx.ai and Anaconda IBM offerings in the G2 Spring Reports Stay plugged in You can check out the…

The advantages and disadvantages of private cloud 

6 min read - The popularity of private cloud is growing, primarily driven by the need for greater data security. Across industries like education, retail and government, organizations are choosing private cloud settings to conduct business use cases involving workloads with sensitive information and to comply with data privacy and compliance needs. In a report from Technavio (link resides outside ibm.com), the private cloud services market size is estimated to grow at a CAGR of 26.71% between 2023 and 2028, and it is forecast to increase by…

Optimize observability with IBM Cloud Logs to help improve infrastructure and app performance

5 min read - There is a dilemma facing infrastructure and app performance—as workloads generate an expanding amount of observability data, it puts increased pressure on collection tool abilities to process it all. The resulting data stress becomes expensive to manage and makes it harder to obtain actionable insights from the data itself, making it harder to have fast, effective, and cost-efficient performance management. A recent IDC study found that 57% of large enterprises are either collecting too much or too little observability data.…

IBM Newsletters

Get our newsletters and topic updates that deliver the latest thought leadership and insights on emerging trends.
Subscribe now More newsletters