本文为您介绍短信服务Node.js SDK的使用方法及示例。

准备工作

  • 在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参见AccessKey
  • 安装短信服务Node.js SDK。
    npm install @alicloud/dysmsapi20170525@2.0.1 --save

发送短信

  • 代码示例
    import Dysmsapi20170525, * as $Dysmsapi20170525 from '@alicloud/dysmsapi20170525';
    import OpenApi, * as $OpenApi from '@alicloud/openapi-client';
    import * as $tea from '@alicloud/tea-typescript';
    
    
    export default class Client {
    
      /**
       * 使用AK&SK初始化账号Client
       * @param accessKeyId
       * @param accessKeySecret
       * @return Client
       * @throws Exception
       */
      static createClient(accessKeyId: string, accessKeySecret: string): Dysmsapi20170525 {
        let config = new $OpenApi.Config({
          // 您的AccessKey ID
          accessKeyId: accessKeyId,
          // 您的AccessKey Secret
          accessKeySecret: accessKeySecret,
        });
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Dysmsapi20170525(config);
      }
    
      static async main(args: string[]): Promise<void> {
        let client = Client.createClient("accessKeyId", "accessKeySecret");
        let sendSmsRequest = new $Dysmsapi20170525.SendSmsRequest({
          phoneNumbers: "15038712376",
          signName: "阿里大于测试专用",
          templateCode: "SMS_209335004",
          templateParam: "{\"code\":\"1111\"}",
        });
        // 复制代码运行请自行打印 API 的返回值
        await client.sendSms(sendSmsRequest);
      }
    
    }
    
    Client.main(process.argv.slice(2));
  • 结果示例
    {  "RequestId": "614048FB-0619-4439-A1D5-AA8B218A****",  "Message": "OK",  "BizId": "386715418801811068^0",  "Code": "OK"}

相关文档

  • 如果您想了解更多示例,请参见OpenAPI开发者门户
    说明 登录开发者门户平台,SDK版本选择时,请选择升级版 SDK
  • 短信服务相关API,请参见API概览