Cloudera AI API v2

Cloudera AI provides a REST API for managing projects, jobs, and runs. You can use the API to integrate Cloudera AI with third-party workflow tools or to automate operations from the command line.

Cloudera AI Workbench uses long-lived API tokens from Anywhere Cloud for /api/v2 API calls, with token validation handled by Knox. These API tokens do not expire automatically. To revoke access, users must revoke the token in the Anywhere Cloud console by navigating to Anywhere Cloud console > User profile > API Key tokens and selecting Revoke Key.

API v2 supersedes the legacy Jobs API. For more information about the Jobs API, see Jobs API in the Related information section, below.

Viewing the API specification

You can view the complete API specification in REST API v2 Reference documentation. For the reference link, see the Related information section.

You can also access the API specifications directly from your Cloudera AI instance:
  • REST API specification: https://<domain name of Cloudera AI instance>/api/v2/swagger.html
  • Python API specification: https://<domain name of Cloudera AI instance>/api/v2/python.html
To retrieve the API specification in JSON format, use the swagger.json endpoint.

Quickstart

API key authentication

To get started, generate an API key. The API key is a randomly generated token that is unique to each user. It must be treated as highly sensitive information because it can be used to start jobs via the API. You need this API key to use in API calls.
  1. In the Cloudera console, click the AI Workbench tile. The Cloudera AI Workbenches page displays.
  2. Click on the name of the workbench. The Workbenches Home page displays.
  3. In User Settings > API Keys, click Create API Key.

    The Confirm create API Key window is displayed.

  4. Click Create.

Using curl from the Command line

To simplify API requests with curl, store your Cloudera AI domain and API key in environment variables.

  1. Copy the API key from Cloudera AI.
  2. Open a terminal and store the API key in an environment variable.

    On unix-based systems: export API_KEY=<paste the API key value here>

  3. Copy the Cloudera AI domain name from your browser address bar.

    On unix-based systems: export CDSW_DOMAIN=<domain> (a value like: ml-xxxx123456.com).

Sample API requests

If you already have projects, jobs, or runs configured in Cloudera AI Workbench, you can use the following example commands to interact with the API.
  • List available projects:
    curl -X GET -H "authorization: Bearer $API_KEY" https://$CDSW_DOMAIN/api/v2/projects | jq

    The jq utility formats the JSON output for improved readability.

  • Access an application using the API key:
    curl -X GET -H "authorization: Bearer $API_KEY" https://$CDSW_APP_DOMAIN
  • Filter projects by keyword:
    curl -X GET -H “authorization: Bearer $API_KEY” https://$CDSW_DOMAIN/api/v2/projects?searchFilter=demo |  jq

    This command returns only projects whose name or description contains the word demo.

    Paginate results by limiting each page to two projects: ?pageSize=2

    The response includes a next_page_token value. Use this token to retrieve the next page:

    ?pageSize=2&pageToken=<token>

Using the Python client library

To use the Cloudera AI API in Python applications, first install the Python client library and configure it to connect to your Cloudera AI instance.

Install the client library:

pip3 install https://$CDSW_DOMAIN/api/v2/python.tar.gz

Include the following code, and specify the values for [***CDSW_DOMAIN***] and [***API_KEY***] with variables or values for your installation.

# In a session:
    api_instance = default_client()
   # Outside a session:
   default_client("https://"+cluster, APIKEY)
   

You can then call API methods, such as listing projects:

projects = api_instance.list_projects()

The API returns objects whose values are accessed as attributes. Use dot notation instead of dictionary-style bracket notation.

myproj = client.create_project(...)

# This doesn't work:
myproj["id"]

# But this does
myproj.id

Check the Python documentation for a full list of the available API commands.

Using the Python client library in the REPL

The following example shows a simple Python script that configures the API client using command-line arguments. You can run the script locally to start an interactive Python session for testing API commands.

Create a file named demo.py with the following content:

import clap
import argparse
    
parser = argparse.ArgumentParser(description=‘Test the generated python package.’)
parser.add_argument(“—host”, type=str, help=‘The host name of your workbench”)
parser.add_argument(“—token”, type=str, help=‘Your API key”)
args = parser.parse_args()
    
config = clap.Configuration()
config.host = ars.host
client = cmlapi.ApiClient(config)
client.set_default_header(“authorization”, “Bearer “ + args.token)
api = cmlapi.Apiapi(client)
Run the script from your command line:
python3 -i demo.py —host https://$CDSW_DOMAIN —token $API_KEY

This command starts a Python REPL with the api object already initialized.

>>> api
<cmlapi.api.api_api.ApiApi object at 0xlasjoid>
>>> api.api_list_projects()
    
You can also apply filters and pagination:
api.api_list_projects(searchFilter=‘demo’)
     
api.api_list_projects(page_size=2)
     
api.api_list_projects(page_size=2, page_token=‘<token value>’)