SDK 3.0 is a companion tool for the TencentCloud API 3.0 platform. You can use all SMS APIs through the SDK. The new SDK version is unified and features the same SDK usage, API call methods, error codes, and returned packet formats for different programming languages.
Note:
- SMS sending APIs
One message can be sent to up to 200 numbers at a time.- Signature and body template APIs
Individual users have no permission to use signature and body template APIs and can manage SMS signatures and SMS body templates only in the SMS console. To use the APIs, change "Individual Identity" to "Organizational Identity".
SecretID
and SecretKey
on the API Key Management page in the CAM console.SecretID
is used to identify the API caller.SecretKey
is used to encrypt the string to sign that can be verified on the server. You should keep it private and avoid disclosure.sms.tencentcloudapi.com
.pip install tencentcloud-sdk-python
$ cd tencentcloud-sdk-python
$ python setup.py install
Note:All samples are for reference only and cannot be directly compiled and executed. You need to modify them based on your actual needs. You can also use API 3.0 Explorer to automatically generate the demo code as needed.
Each API has a corresponding request structure and a response structure. This document only lists the sample code of several common features.
# -*- coding: utf-8 -*-
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
# Import the client models of the SMS module
from tencentcloud.sms.v20190711 import sms_client, models
# Import the optional configuration classes
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
try:
# Required steps:
# Instantiate an authentication object. The Tencent Cloud account key pair `secretId` and `secretKey` need to be passed in as the input parameters
# This example uses the way to read from the environment variable, so you need to set these two values in the environment variable in advance
# You can also write the key pair directly into the code, but be careful not to copy, upload, or share the code to others
# Query the CAM key: https://console.tencentcloud.com/cam/capi
cred = credential.Credential("secretId", "secretKey")
# cred = credential.Credential(
# os.environ.get(""),
# os.environ.get("")
# )
# Instantiate an HTTP option (optional; skip if there are no special requirements)
httpProfile = HttpProfile()
# If you need to specify the proxy for API access, you can initialize HttpProfile as follows
# httpProfile = HttpProfile(proxy="http://username:password@proxy IP:proxy port")
httpProfile.reqMethod = "POST" # POST request (POST request by default)
httpProfile.reqTimeout = 30 # Request timeout period in seconds (60 seconds by default)
httpProfile.endpoint = "sms.tencentcloudapi.com" # Specify the access region domain name (nearby access by default)
# Optional steps:
# Instantiate a client configuration object. You can specify the timeout period and other configuration items
clientProfile = ClientProfile()
clientProfile.signMethod = "TC3-HMAC-SHA256" # Specify the signature algorithm
clientProfile.language = "en-US"
clientProfile.httpProfile = httpProfile
# Instantiate an SMS client object
# The second parameter is the region information. You can directly enter the string `ap-guangzhou` or import the preset constant
client = sms_client.SmsClient(cred, "ap-guangzhou", clientProfile)
# Instantiate a request object. You can further set the request parameters according to the API called and actual conditions
# You can directly check the SDK source code to determine which attributes of `SendSmsRequest` can be set
# An attribute may be of a basic type or import another data structure
# We recommend you use the IDE for development where you can easily redirect to and view the documentation of each API and data structure
req = models.AddSmsTemplateRequest()
# Settings of a basic parameter:
# The SDK uses the pointer style to specify parameters, so even for basic parameters, you need to use pointers to assign values to them
# The SDK provides encapsulation functions for importing the pointers of basic parameters
# Help link:
# SMS console: https://console.tencentcloud.com/smsv2
# sms helper:https://www.tencentcloud.com/document/product/382/3773?from_cn_redirect=1
# Template name
req.TemplateName = "Tencent Cloud"
# Template content
req.TemplateContent = "Your login verification code is {1}. Please enter it within {2} minutes. If the login was not initiated by you, please ignore this message."
# SMS type. 0: regular SMS; 1: marketing SMS
req.SmsType = 0;
# Whether it is Global SMS:
# 0: Mainland China SMS
# 1: Global SMS
req.International = 0
# Template remarks, such as reason for application and use case
req.Remark = "xxx"
# Initialize the request by calling the `AddSmsTemplate` method on the client object. Note: the request method name corresponds to the request object
resp = client.AddSmsTemplate(req)
# A string return packet in JSON format is output
print(resp.to_json_string(indent=2))
except TencentCloudSDKException as err:
print(err)
# -*- coding: utf-8 -*-
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
# Import the client models of the SMS module
from tencentcloud.sms.v20190711 import sms_client, models
# Import the optional configuration classes
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
try:
# Required steps:
# Instantiate an authentication object. The Tencent Cloud account key pair `secretId` and `secretKey` need to be passed in as the input parameters
# This example uses the way to read from the environment variable, so you need to set these two values in the environment variable in advance
# You can also write the key pair directly into the code, but be careful not to copy, upload, or share the code to others
# Query the CAM key: https://console.tencentcloud.com/cam/capi
cred = credential.Credential("secretId", "secretKey")
# cred = credential.Credential(
# os.environ.get(""),
# os.environ.get("")
# )
# Instantiate an HTTP option (optional; skip if there are no special requirements)
httpProfile = HttpProfile()
# If you need to specify the proxy for API access, you can initialize HttpProfile as follows
# httpProfile = HttpProfile(proxy="http://username:password@proxy IP:proxy port")
httpProfile.reqMethod = "POST" # POST request (POST request by default)
httpProfile.reqTimeout = 30 # Request timeout period in seconds (60 seconds by default)
httpProfile.endpoint = "sms.tencentcloudapi.com" # Specify the access region domain name (nearby access by default)
# Optional steps:
# Instantiate a client configuration object. You can specify the timeout period and other configuration items
clientProfile = ClientProfile()
clientProfile.signMethod = "TC3-HMAC-SHA256" # Specify the signature algorithm
clientProfile.language = "en-US"
clientProfile.httpProfile = httpProfile
# Instantiate an SMS client object
# The second parameter is the region information. You can directly enter the string `ap-guangzhou` or import the preset constant
client = sms_client.SmsClient(cred, "ap-guangzhou", clientProfile)
# Instantiate a request object. You can further set the request parameters according to the API called and actual conditions
# You can directly check the SDK source code to determine which attributes of `SendSmsRequest` can be set
# An attribute may be of a basic type or import another data structure
# We recommend you use the IDE for development where you can easily redirect to and view the documentation of each API and data structure
req = models.SendSmsRequest()
# Settings of a basic parameter:
# The SDK uses the pointer style to specify parameters, so even for basic parameters, you need to use pointers to assign values to them
# The SDK provides encapsulation functions for importing the pointers of basic parameters
# Help link:
# SMS console: https://console.tencentcloud.com/smsv2
# sms helper:https://www.tencentcloud.com/document/product/382/3773?from_cn_redirect=1
# SMS application ID, which is the `SDKAppID` generated after an application is added in the [SMS console], such as 1400006666
req.SmsSdkAppid = "1400787878"
# SMS signature content, which should be encoded in UTF-8. You must enter an approved signature, which can be viewed in the [SMS console]
req.Sign = "xxx"
# SMS code number extension, which is not activated by default. If you need to activate it, please contact [SMS Helper]
req.ExtendCode = ""
# User session content, which can carry context information such as user-side ID and will be returned as-is by the server
req.SessionContext = "xxx"
# `senderid` for Global SMS, which is not activated by default. If you need to activate it, please contact [SMS Helper] for assistance. This parameter should be left empty for Mainland China SMS
req.SenderId = ""
# Target mobile number in the e.164 standard (+[country/region code][mobile number])
# Example: +8613711112222, which has a + sign followed by 86 (country/region code) and then by 13711112222 (mobile number). Up to 200 mobile numbers are supported
req.PhoneNumberSet = ["+8613711112222", "+8613711333222", "+8613711144422"]
# Template ID. You must enter the ID of an approved template, which can be viewed in the [SMS console]
req.TemplateID = "449739"
# Template parameters. If there are no template parameters, leave it empty
req.TemplateParamSet = ["666"]
# Initialize the request by calling the `SendSms` method on the client object. Note: the request method name corresponds to the request object
resp = client.SendSms(req)
# A string return packet in JSON format is output
print(resp.to_json_string(indent=2))
except TencentCloudSDKException as err:
print(err)
# -*- coding: utf-8 -*-
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
# Import the client models of the SMS module
from tencentcloud.sms.v20190711 import sms_client, models
# Import the optional configuration classes
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
try:
# Required steps:
# Instantiate an authentication object. The Tencent Cloud account key pair `secretId` and `secretKey` need to be passed in as the input parameters
# This example uses the way to read from the environment variable, so you need to set these two values in the environment variable in advance
# You can also write the key pair directly into the code, but be careful not to copy, upload, or share the code to others
# Query the CAM key: https://console.tencentcloud.com/cam/capi
cred = credential.Credential("secretId", "secretKey")
# cred = credential.Credential(
# os.environ.get(""),
# os.environ.get("")
# )
# Instantiate an HTTP option (optional; skip if there are no special requirements)
httpProfile = HttpProfile()
# If you need to specify the proxy for API access, you can initialize HttpProfile as follows
# httpProfile = HttpProfile(proxy="http://username:password@proxy IP:proxy port")
httpProfile.reqMethod = "POST" # POST request (POST request by default)
httpProfile.reqTimeout = 30 # Request timeout period in seconds (60 seconds by default)
httpProfile.endpoint = "sms.tencentcloudapi.com" # Specify the access region domain name (nearby access by default)
# Optional steps:
# Instantiate a client configuration object. You can specify the timeout period and other configuration items
clientProfile = ClientProfile()
clientProfile.signMethod = "TC3-HMAC-SHA256" # Specify the signature algorithm
clientProfile.language = "en-US"
clientProfile.httpProfile = httpProfile
# Instantiate an SMS client object
# The second parameter is the region information. You can directly enter the string `ap-guangzhou` or import the preset constant
client = sms_client.SmsClient(cred, "ap-guangzhou", clientProfile)
# Instantiate a request object. You can further set the request parameters according to the API called and actual conditions
# You can directly check the SDK source code to determine which attributes of `SendSmsRequest` can be set
# An attribute may be of a basic type or import another data structure
# We recommend you use the IDE for development where you can easily redirect to and view the documentation of each API and data structure
req = models.PullSmsSendStatusRequest()
# Settings of a basic parameter:
# The SDK uses the pointer style to specify parameters, so even for basic parameters, you need to use pointers to assign values to them
# The SDK provides encapsulation functions for importing the pointers of basic parameters
# Help link:
# SMS console: https://console.tencentcloud.com/smsv2
# sms helper:https://www.tencentcloud.com/document/product/382/3773?from_cn_redirect=1
# SMS application ID, which is the `SDKAppID` generated after an application is added in the [SMS console], such as 1400006666
req.SmsSdkAppid = "1400787878"
# Maximum number of pulled entries. Maximum value: 100
req.Limit = 10
# Initialize the request by calling the `PullSmsSendStatus` method on the client object. Note: the request method name corresponds to the request object
resp = client.PullSmsSendStatus(req)
# A string return packet in JSON format is output
print(resp.to_json_string(indent=2))
except TencentCloudSDKException as err:
print(err)
# -*- coding: utf-8 -*-
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
# Import the client models of the SMS module
from tencentcloud.sms.v20190711 import sms_client, models
# Import the optional configuration classes
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
try:
# Required steps:
# Instantiate an authentication object. The Tencent Cloud account key pair `secretId` and `secretKey` need to be passed in as the input parameters
# This example uses the way to read from the environment variable, so you need to set these two values in the environment variable in advance
# You can also write the key pair directly into the code, but be careful not to copy, upload, or share the code to others
# Query the CAM key: https://console.tencentcloud.com/cam/capi
cred = credential.Credential("secretId", "secretKey")
# cred = credential.Credential(
# os.environ.get(""),
# os.environ.get("")
# )
# Instantiate an HTTP option (optional; skip if there are no special requirements)
httpProfile = HttpProfile()
# If you need to specify the proxy for API access, you can initialize HttpProfile as follows
# httpProfile = HttpProfile(proxy="http://username:password@proxy IP:proxy port")
httpProfile.reqMethod = "POST" # POST request (POST request by default)
httpProfile.reqTimeout = 30 # Request timeout period in seconds (60 seconds by default)
httpProfile.endpoint = "sms.tencentcloudapi.com" # Specify the access region domain name (nearby access by default)
# Optional steps:
# Instantiate a client configuration object. You can specify the timeout period and other configuration items
clientProfile = ClientProfile()
clientProfile.signMethod = "TC3-HMAC-SHA256" # Specify the signature algorithm
clientProfile.language = "en-US"
clientProfile.httpProfile = httpProfile
# Instantiate an SMS client object
# The second parameter is the region information. You can directly enter the string `ap-guangzhou` or import the preset constant
client = sms_client.SmsClient(cred, "ap-guangzhou", clientProfile)
# Instantiate a request object. You can further set the request parameters according to the API called and actual conditions
# You can directly check the SDK source code to determine which attributes of `SendSmsRequest` can be set
# An attribute may be of a basic type or import another data structure
# We recommend you use the IDE for development where you can easily redirect to and view the documentation of each API and data structure
req = models.SendStatusStatisticsRequest()
# Settings of a basic parameter:
# The SDK uses the pointer style to specify parameters, so even for basic parameters, you need to use pointers to assign values to them
# The SDK provides encapsulation functions for importing the pointers of basic parameters
# Help link:
# SMS console: https://console.tencentcloud.com/smsv2
# sms helper:https://www.tencentcloud.com/document/product/382/3773?from_cn_redirect=1
# SMS application ID, which is the `SDKAppID` generated after an application is added in the [SMS console], such as 1400006666
req.SmsSdkAppid = "1400787878"
# Maximum number of pulled entries. Maximum value: 100
req.Limit = 10
# Offset. Note: this parameter is currently fixed at 0
req.Offset = 0
# Start time of pull in the format of `yyyymmddhh` accurate to the hour
req.StartDateTime = 2019122400
# End time of pull in the format of `yyyymmddhh` accurate to the hour
# Note: `EndDataTime` must be after `StartDateTime`
req.EndDataTime = 2019122523
# Initialize the request by calling the `SendStatusStatistics` method on the client object. Note: the request method name corresponds to the request object
resp = client.SendStatusStatistics(req)
# A string return packet in JSON format is output
print(resp.to_json_string(indent=2))
except TencentCloudSDKException as err:
print(err)
When you install Python 3.6 or above on macOS, you may encounter a certificate error: Error: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1056).
.
This is because that on macOS, Python no longer uses the system's default certificate and does not provide a certificate itself. When an HTTPS request is made, the certificate provided by the certifi
library needs to be used, but the SDK does not support specifying it; therefore, you can only solve this problem by installing the certificate with the sudo "/Applications/Python 3.6/Install Certificates.command"
command.
Although this problem should not occur in Python 2, there may be similar situations in specific user environments, which can also be solved with sudo /Applications/Python 2.7/Install Certificates.command
.
If there is a proxy in your environment, you can set the proxy in the following two ways:
https_proxy
.Otherwise, it may not be called normally, and a connection timeout exception will be thrown.
Was this page helpful?