sns nodejs publish to a specific phone

44 Views Asked by At

I recently developed a project that needs to send a text message, Amazon's sns, with the old version I could send it smoothly, but with the new one I'm not able to send it to a PhoneNumber (I can still send it with v2, but it will be discontinued and I cannot run the risk of the application stopping), how can I send it? I tried it this way:

const { SNSClient, PublishCommand } = require("@aws-sdk/client-sns");
const { fromEnv } = require("@aws-sdk/credential-providers");

async function sendMessage(message, phoneNumber) {
    const snsClient = new SNSClient({
        credentials: fromEnv(),
        region: process.env.AWS_REGION,
    });
    const params = {
        Message: message,
        PhoneNumber: phoneNumber, // Use this to send directly to a phone number
    };

    try {
        const command = new PublishCommand(params);
        const data = await snsClient.send(command);
        console.log("SMS SEND SUCCESS", data);
    } catch (err) {
        console.log("Error", err);
    }
}

module.exports = { sendMessage };

But dont work, just return:

Error: {
    Type: 'Sender',
    Code: 'InvalidParameter',
    Message: 'Invalid parameter: TopicArn or TargetArn Reason: no value for required parameter',
    message: 'Invalid parameter: TopicArn or TargetArn Reason: no value for required parameter'
  }
1

There are 1 best solutions below

0
ramsey On

The above issue can occur in any of the following scenario:

  1. The SDK version doesn’t match(if that’s the case then we have to re-install the SDK)
  2. Incorrect access key or secret key
  3. Insufficient permission for access key or secret key
  4. If the service is not available in the region where they try to access from.

For this case re-installing the SDK worked.