Python SDK

安装

使用 pip 是安装适用于 Python 的 Unimatrix SDK 的推荐方法,可在 PyPI 上获得。

运行以下命令将 uni-sdk 添加为项目依赖:

pip install uni-sdk

使用示例

以下示例展示如何使用 Unimatrix Python SDK 快速调用 Unimatrix 服务。

发送短信

向单个收件人发送短信


from uni.client import UniClient
from uni.exception import UniException

# 初始化
client = UniClient("your access key id", "your access key secret") # 若使用简易验签模式仅传入第一个参数即可
client.endpoint = "https://api-cn.unimtx.com" # 设置接入点到中国大陆, 若使用全球节点请移除此行代码

# 发送短信
try:
  res = client.messages.send({
    "to": "1865800xxxx", # 国际号码以 E.164 格式传入手机号, 如 +861865800xxxx
    "signature": "合一矩阵", # 请替换为您的短信签名
    "content": "您的验证码是123456,5分钟内有效。"
  })
  print(res.data)
except UniException as e:
  print(e)

使用模版和模版变量发送短信

client.messages.send({
  "to": "1865800xxxx",
  "signature": "合一矩阵",
  "templateId": "pub_verif_ttl2",
  "templateData": {
    "code": "123456",
    "ttl": "5"
  }
})