Step 2: Running the Python API

Now that you have the generated Python code in the src/gen folder, let’s use it to access the platform.

As a first test, you will query all data sets that exist in your account.

Step 2.1: Install dependent packages

We recommend using the package installer from your IDE (such as PyCharm), or run the following commands to install the dependencies:

pip install pydantic
pip install urllib3
pip install python-dateutil

Step 2.2: Obtaining an API username and password

The first query you should do with the xyzt.ai platform is to authenticate yourself and obtain a JWT token that will subsequently be used to securely access the platform.

For this, you need to authenticate yourself using an API username and password.

Navigate to the user management section of the platform. At the bottom of this page you can generate a new API user.

Copy the username and password for your reference.

Not all users have rights to generate API users.

If you don’t see the ability to create an API user, you probably have insufficient admin rights on the xyzt.ai platform. Or, your account does not allow for API access.

In the first case, reach out to your xyzt.ai account owner, or, as in the second case, contact us at support@xyzt.ai.

Step 2.3 Your first platform REST query

With the API user credentials, you can now use the generated Python code to authenticate with the platform and start using the REST API.

In this first example, you will query the existing data sets in the platform.

Add following main.py file to your project folder that contains our script:

main.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import openapi_client
from openapi_client.rest import ApiException
from pprint import pprint

userName = "replace with your API user name"
password = "replace with your API password"

# Enter a context with an instance of the API client
with openapi_client.ApiClient() as api_client:
    # Create an instance of the API class
    api_instance = openapi_client.AuthenticationJWTTokenApi(api_client)
    login_request = openapi_client.LoginRequest(userName=userName, password=password)

    try:
        # Retrieve a JWT token
        token_response = api_instance.obtain_token(login_request)
        print("The response of AuthenticationJWTTokenApi->obtain_token:\n")
        pprint(token_response)

        # Set the obtained token in the headers for subsequent requests
        api_client.default_headers["Authorization"] = f"Bearer {token_response.jwt_token}"

        # Query all data sets
        datasets_api = openapi_client.DataSetsApi(api_client)
        datasets_response = datasets_api.get_all_data_sets()
        pprint(datasets_response)

    except ApiException as e:
        print("Exception when calling the REST API: %s\n" % e)

Do not forget to edit the userName and password variables at the top of the file with the ones obtained after creating the API user.

The code is self-explanatory: it obtains a JWT token using the API username and password. It then uses the DataSetsApi (configured with an Authorization that holds the JWT token for authentication) to query all available data sets. Afterward, the result is printed on the console.

The JWT token has a limited lifetime, you should refresh it.

JWT tokens remain valid for 15 minutes. If your script runs longer, you will have to refresh the token.

This should print the obtained JWT token and the output of the available data sets in the platform.

The obtained datasets_response prints on the console like this:

PublicDataSetDTO(id='17cd5229-af8a-4aeb-b02d-8aae87ef117e', name='Sample Data: Spire Aviation', ...
...

The data set entry contains properties id, name, etc. The id property is particularly important as all resources (including projects, data sets, etc.) are referenced by a unique identifier and not by the name that was provided for example when creating the project or data set.

In the next step of this tutorial, you will use this id property to add an existing data set to a new project.