Sending SMS from NextJs 14 application using SMS Portal on Vercel

75 Views Asked by At

I'm building a NextJs 14 application that should send OTP's to users via SMS

Implemented SMS Portal, which seems to be most cost effective in my scenario.

I'm hosting on Vercel (Pro plan)

Everything works just fine locally, but when deployed SMS's are not sent Nothing in error logs, no exceptions thrown, but SMS does not arrive and my SMS Portal shows no sms's sent (unless I send them from localhost:3000)

I'm not sure there's something wrong with my code (it' works locally) or something on SMS portal's side (they say no) Or I need to whitelis SMSPortal's IP in Vercel (not sure how?)

Anyone got some insight on this?

Code is below, but like i said it works fine locally

import axios from "axios";

export const sendSms = async (message: string, recipient: string) => {
  const apiKey = process.env.SMS_PORTAL_CLIENT_ID;
  const apiSecret = process.env.SMS_PORTAL_API_SECRET;
  const accountApiCredentials = apiKey + ":" + apiSecret;

  const buff = Buffer.from(accountApiCredentials);
  const base64Credentials = buff.toString("base64");

  const requestHeaders = {
    headers: {
      Authorization: `Basic ${base64Credentials}`,
      "Content-Type": "application/json",
    },
  };

  const requestData = JSON.stringify({
    messages: [
      {
        content: message,
        destination: recipient,
      },
    ],
  });

  axios
    .post(
      "https://rest.smsportal.com/bulkmessages",
      requestData,
      requestHeaders,
    )
    .then((response) => {
      if (response.data) {
        console.log("Success:");
        console.log(response.data);
      }
    })
    .catch((error) => {
      if (error.response) {
        console.log("Failure:");
        console.log(error.response.data);
      } else {
        console.log("Something went wrong during the network request.");
      }
    });
};```
0

There are 0 best solutions below