You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Node.js API向Azure Event Grid主题端点发送短信数据

使用Node.js向Azure Event Grid主题发送短信消息数据

步骤1:安装依赖

首先安装Azure Event Grid的Node.js SDK包:

npm install @azure/eventgrid

步骤2:获取主题凭证

从Azure门户的Event Grid主题页面,复制以下两个关键信息:

  • 主题Endpoint(格式类似https://<your-topic>.eventgrid.azure.net/api/events)
  • 访问密钥(Access Key)

步骤3:编写发送代码

创建Node.js脚本,构造短信事件数据并发送到主题:

const { EventGridClient, AzureKeyCredential } = require("@azure/eventgrid");

// 替换为你的实际凭证
const topicEndpoint = "https://<your-topic-name>.eventgrid.azure.net/api/events";
const accessKey = "<your-access-key>";

async function sendSmsEvent() {
  // 初始化Event Grid客户端
  const client = new EventGridClient(new AzureKeyCredential(accessKey), topicEndpoint);

  // 构造短信事件,data字段为自定义的短信内容
  const smsEvents = [
    {
      id: `${Date.now()}`, // 唯一事件ID,可用时间戳生成
      subject: "SMS/OutgoingMessage", // 事件主题,用于区分业务场景
      dataVersion: "1.0", // 数据版本,自定义即可
      eventType: "SMS.MessageInitiated", // 事件类型,订阅方会据此处理
      data: {
        recipientPhone: "+86138xxxxxxx",
        messageContent: "您的快递已送达,请及时取件",
        senderId: "AzureDemo"
      }
    }
  ];

  try {
    await client.sendEvents(smsEvents);
    console.log("短信事件已成功发送至Event Grid主题");
  } catch (err) {
    console.error("发送失败:", err.message);
  }
}

// 执行发送函数
sendSmsEvent();

关键说明

  • data字段可根据你的业务需求自定义字段,比如添加模板ID、发送时间等
  • 若需批量发送短信,直接在smsEvents数组中添加多个事件对象即可
  • 确保访问密钥和Endpoint的正确性,否则会触发认证错误
  • eventType和subject用于订阅端过滤和识别事件,建议根据业务场景命名

内容的提问来源于stack exchange,提问作者kishore pantra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 12:45:36