Tencent Cloud SDK for Python

3.1.78 · active · verified Wed Apr 15

Tencent Cloud SDK for Python is the official software development kit for interacting with Tencent Cloud services. It provides a unified, Pythonic interface to TencentCloud API 3.0, enabling developers to easily integrate various cloud products like CVM, VPC, and CBS into their applications. The SDK supports Python 3.6 and above, offering consistent API usage, error codes, and response formats across different services. It is actively maintained with frequent updates to support new services and features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the client with credentials, create a request object for the CVM `DescribeInstances` API, and execute the call. Ensure `TENCENTCLOUD_SECRET_ID` and `TENCENTCLOUD_SECRET_KEY` environment variables are set for authentication.

import os
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.cvm.v20170312 import cvm_client, models

try:
    # Get credentials from environment variables (recommended for security)
    secret_id = os.environ.get('TENCENTCLOUD_SECRET_ID', 'YOUR_SECRET_ID')
    secret_key = os.environ.get('TENCENTCLOUD_SECRET_KEY', 'YOUR_SECRET_KEY')

    # Check if credentials are placeholders
    if secret_id == 'YOUR_SECRET_ID' or secret_key == 'YOUR_SECRET_KEY':
        print("Please set TENCENTCLOUD_SECRET_ID and TENCENTCLOUD_SECRET_KEY environment variables.")
        exit(1)

    cred = credential.Credential(secret_id, secret_key)

    # Instantiate the client object for a specific product (e.g., CVM)
    # The second parameter is the region, e.g., 'ap-shanghai' or 'ap-guangzhou'
    client = cvm_client.CvmClient(cred, "ap-shanghai")

    # Instantiate a request object for the API (e.g., DescribeInstancesRequest)
    req = models.DescribeInstancesRequest()

    # Call the API method through the client object
    resp = client.DescribeInstances(req)

    # Print the returned response in JSON format
    print(resp.to_json_string())

except TencentCloudSDKException as err:
    print(f"An error occurred: {err}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →