Tencent Cloud Common SDK for Python

3.1.79 · active · verified Fri Apr 17

The `tencentcloud-sdk-python-common` library provides the foundational components for interacting with Tencent Cloud services in Python. It includes core utilities for authentication, client profiles, and base models, serving as a crucial dependency for service-specific SDK modules like `tencentcloud-sdk-python-cvm`. It is part of the larger `tencentcloud-sdk-python` project, currently at version 3.1.79, with frequent updates to support new services and features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the common components (`Credential`, `ClientProfile`, `HttpProfile`) to initialize a service client (e.g., `CvmClient`) and make a basic API call. Ensure your `TENCENTCLOUD_SECRET_ID` and `TENCENTCLOUD_SECRET_KEY` environment variables are set.

import os
from tencentcloud.common.credential import Credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.cvm.v20170312.client import CvmClient
from tencentcloud.cvm.v20170312.models import DescribeInstancesRequest

try:
    # Set credentials via environment variables for security
    secret_id = os.environ.get("TENCENTCLOUD_SECRET_ID", "")
    secret_key = os.environ.get("TENCENTCLOUD_SECRET_KEY", "")
    region = "ap-guangzhou" # Replace with your desired region

    cred = Credential(secret_id, secret_key)

    http_profile = HttpProfile()
    # Specify the endpoint for the service if needed, otherwise it's auto-resolved
    http_profile.endpoint = "cvm.tencentcloudapi.com"

    client_profile = ClientProfile()
    client_profile.httpProfile = http_profile

    # Initialize a service client (e.g., CVMClient) using common components
    client = CvmClient(cred, region, client_profile)

    # Create a request object for a specific API operation
    req = DescribeInstancesRequest()
    # For a basic test, no specific filters are applied, listing all instances

    # Call the API and print the response
    resp = client.DescribeInstances(req)
    print(resp.to_json_string())

except TencentCloudSDKException as err:
    print(f"An error occurred: {err}")
    print(f"Error Code: {err.get_code()}")
    print(f"Request ID: {err.get_request_id()}")

view raw JSON →