TM1py

2.2.4 · active · verified Thu Apr 16

TM1py is a free and open-source Python package that wraps the IBM Planning Analytics (TM1) REST API into a simple-to-use library, facilitating Python developments for TM1. It enables programmatic interaction with TM1 for tasks like reading/writing data, executing processes, and managing metadata. The library is actively maintained by Cubewise and a community of contributors, with the current version being 2.2.4, and receives regular updates and minor releases. [3, 5, 15]

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to an on-premise TM1 instance and an IBM Planning Analytics Cloud (PAaaS) instance using TM1py. It retrieves the server product version to confirm connectivity. Environment variables are used for sensitive credentials. For PAaaS, a `base_url`, `api_key`, `iam_url`, and `tenant` are typically required for API Key authentication. [5, 18]

import os
from TM1py.Services import TM1Service

# --- On-premise TM1 Connection ---
TM1_ADDRESS_ONPREM = os.environ.get('TM1_ADDRESS_ONPREM', 'localhost')
TM1_PORT_ONPREM = int(os.environ.get('TM1_PORT_ONPREM', '8001'))
TM1_USER_ONPREM = os.environ.get('TM1_USER_ONPREM', 'admin')
TM1_PASSWORD_ONPREM = os.environ.get('TM1_PASSWORD_ONPREM', 'apple')
TM1_SSL_ONPREM = os.environ.get('TM1_SSL_ONPREM', 'True').lower() == 'true'

try:
    with TM1Service(
        address=TM1_ADDRESS_ONPREM,
        port=TM1_PORT_ONPREM,
        user=TM1_USER_ONPREM,
        password=TM1_PASSWORD_ONPREM,
        ssl=TM1_SSL_ONPREM
    ) as tm1_onprem:
        version_onprem = tm1_onprem.server.get_product_version()
        print(f"Successfully connected to on-premise TM1: {version_onprem}")
except Exception as e:
    print(f"Failed to connect to on-premise TM1: {e}")

# --- IBM Planning Analytics Cloud (PAaaS / TM1 v12) Connection ---
# Note: For PAaaS, typically an API Key is used with 'user="apikey"'
TM1_BASE_URL_CLOUD = os.environ.get('TM1_BASE_URL_CLOUD', 'https://us-east-1.planninganalytics.saas.ibm.com/api/<TenantId>/v0/tm1/<DatabaseName>/')
TM1_API_KEY_CLOUD = os.environ.get('TM1_API_KEY_CLOUD', '')
TM1_IAM_URL_CLOUD = os.environ.get('TM1_IAM_URL_CLOUD', 'https://iam.cloud.ibm.com')
TM1_TENANT_CLOUD = os.environ.get('TM1_TENANT_CLOUD', '') # Required for API Key authentication

if TM1_API_KEY_CLOUD:
    try:
        with TM1Service(
            base_url=TM1_BASE_URL_CLOUD,
            user="apikey",
            password=TM1_API_KEY_CLOUD,
            iam_url=TM1_IAM_URL_CLOUD,
            tenant=TM1_TENANT_CLOUD,
            ssl=True,
            verify=True,
            async_requests_mode=True
        ) as tm1_cloud:
            version_cloud = tm1_cloud.server.get_product_version()
            print(f"Successfully connected to PA Cloud: {version_cloud}")
    except Exception as e:
        print(f"Failed to connect to PA Cloud: {e}")
else:
    print("Skipping PA Cloud connection: TM1_API_KEY_CLOUD not set.")

view raw JSON →