Setting Up a Twilio Account and Sending SMS Messages with Render Hosting
We were searching for a platform that can be used for sending sms internationally, and we found Twilio, which offers many benefits. And also, it is easier to integrate with so many resources on the Internet.
After that, we are guiding through to making a custom Nodejs script to send sms and deploying the app in render.
Twilio is a cloud communications platform that makes it easy to programmatically send and receive SMS, voice calls, and other communication channels using web service APIs. With Twilio,
- You can sign up for a free trial without a credit card.(Limit of 15 USD)
- Pay only for what you use (pay-as-you-go).
- Access various channels like SMS, Voice, WhatsApp, Email, Video, etc.
Key Twilio Advantages
- 99.95% uptime SLA and automated failover.
- Scalable across global markets.
- Multiple communication channels from one platform.
- No contracts: pay as you go, with discounts available at scale.
Typically, sending sms from the US costs only 0.0079 USD. It varies from country to country.
Twilio Pricing Overview(As at 22/01/2025)
Twilio’s pricing is usage-based. For SMS, it starts at $0.0079 per text message to send or receive. Other channels and features (like WhatsApp, Voice, SendGrid Email, etc.) have their own pricing, but to focus on SMS,
- SMS
Starts at $0.0079 to send or receive a message. - WhatsApp Business API
Starts at $0.005 per message. - Voice
Starts at $0.0085/min to receive and $0.014/min to make a call. - Email (SendGrid)
Free for up to 100 emails/day; paid plans start at $19.95/month.
You can refer their pricing at: https://www.twilio.com/en-us/sms/pricing/us
Free Trial Note(Without the credit card info)
- On the free trial, you can only send SMS to verified phone numbers (including the one you used to sign up).
- You also receive a small credit to experiment. Once you upgrade (by adding a payment method), you can send to any phone number.
- Trial messages include “Sent from your Twilio trial account” by default.
Create a Twilio Account
Now, let’s create a Twilio account,
Sign Up
- Go to Twilio and click Sign up.
- You can sign up with your email or use a social provider (e.g., GitHub, Google).
Free Trial
- You do not need to add a credit card initially.
- Twilio will provide you with trial credits to test sending SMS.
- By default, you can send messages to your own verified number without additional charges.
- If you want to send SMS to unverified numbers, you must upgrade your account by adding a billing method.
Verify Your Phone Number
- During sign-up, Twilio will ask you to verify your mobile number.
- Once verified, you can send messages to this number using your Twilio trial credits.
Get a Twilio Phone Number
Navigate to the Console
- After logging in, you’ll land on your Twilio Console.
Phone Numbers
- Click on Phone Numbers in the left-hand menu.
- Select Manage → Buy a Number (for the trial, Twilio often prompts you to choose a free number).
Choose Your Number
- This number is now your Twilio number, which you can use to send and receive SMS.
Retrieve Your Account SID and Auth Token
Go to Your Console Dashboard
- In the Twilio Console, look for the Project Info section.
Copy the Credentials
- You’ll see your Account SID and Auth Token.
- Keep your Auth Token private and never commit it to a public repository.
Environment Configuration
- In your backend code or hosting environment (such as Render), you’ll reference these credentials as environment variables.
The below image shows where you can find the Auth token, SID, and twilio phone number.
Start working on your SMS sending code in Nodejs
Here’s a Node.js example using the official twilio npm package,
Initialize a Node.js Project
Open a terminal and add the following commads,
cd Documents
mkdir twilio-sms-demo
cd twilio-sms-demo
npm init -y
npm install twilio
Create an index.js File
Open a new file in visual studio code and then add the following code,
/* eslint-disable max-len */
const express = require("express");
const twilio = require("twilio");
// Initialize Express
const app = express();
// Middleware to parse JSON
app.use(express.json());
// Twilio setup
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const twilioPhoneNumber = process.env.TWILIO_PHONE_NUMBER;
const twilioClient = twilio(accountSid, authToken);
// Endpoint to send SMS
app.post("/send-sms", async (req, res) => {
const { phoneNumber, message } = req.body;
if (!phoneNumber || !message) {
return res.status(400).json({
error: "Both 'phoneNumber' and 'message' fields are required.",
});
}
try {
const sms = await twilioClient.messages.create({
body: message,
from: twilioPhoneNumber,
to: phoneNumber,
});
return res.status(200).json({
message: "SMS sent successfully",
sid: sms.sid,
});
} catch (error) {
console.error("Error sending SMS:", error.message);
return res.status(500).json({
error: "Failed to send SMS",
details: error.message,
});
}
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
Save the file as index.js in twilio-sms-demo folder.
Create a file named .env,
TWILIO_ACCOUNT_SID=<Your Account SID>
TWILIO_AUTH_TOKEN=<Your Auth Token>
TWILIO_PHONE_NUMBER=<Your Twilio Phone Number>
Change the SID, authtoken and phone number with the noted info from Twilio console.
Then run the following command,
node index.js
If your trial account and environment variables are set correctly, run the API on Postman and as given at the end of the article.
Push Your Code to GitHub
- Login to GitHub or create an account through the link,
- Create a new repo in github and copy the link
- Initialize Git
git init
git add .
git commit -m "Initial commit"
- Add Remote Repository
git remote add origin https://github.com/<your-username>/<your-repo>.git
- Push to GitHub
git push -u origin main
Deploy on Render
Render is a modern hosting platform that lets you deploy full-stack applications easily, often within the free tier constraints for small projects.
Sign in to Render
- Go to Render Dashboard and create an account if you haven’t already.
Create a New Web Service
- Click New → Web Service.
- Connect your GitHub account and select your repository with the Twilio code.
- Click “connect”
Choose a Plan
- Render has a free plan with 750 hours of server runtime per month and auto-sleep after 15 minutes of inactivity.
- When the service is inactive, it will restart within ~50 seconds on the next request.
Add Environment Variables
- On Render, find the Environment section and add the following,
TWILIO_ACCOUNT_SID=<Your Twilio Account SID>
TWILIO_AUTH_TOKEN=<Your Twilio Auth Token>
TWILIO_PHONE_NUMBER=<Your Twilio Number>
- Click “Add Variables”.
Deploy
- Click Deploy Web Service.
- For the free plan, you’ll be asked to add a credit card for verification.
- You won’t be charged unless you exceed the free plan limits.
Note: Once your service is deployed, you can trigger your code to send an SMS with the API.
Usage with Postman
Once deployed, Render provides a public URL (e.g., https://your-app.onrender.com)
- Note the URL
- Set Postman to send a test request,
HTTP Method: POST
URL: https://your-app.onrender.com/send-sms
Headers:
Content-Type: application/json
- Body,
{
"phoneNumber": "+1234567890",
"message": "Hello, this is a test message from Twilio!"
}
Expected Responses
Success
{
"message": "SMS sent successfully",
"sid": "SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}
Error
{
"error": "Failed to send SMS",
"details": "<Error info>"
}
Sending SMS Beyond Your Verified Number
- Trial Limitations: By default, you can only send messages to phone numbers you have verified in Twilio.
- Upgrade: To send SMS messages to any phone number, upgrade your account by adding your payment details in the Twilio Console.
- Costs: Twilio will charge you according to the pay-as-you-go SMS rate (starting at $0.0079/message in the US).
Best Practices
Use Environment Variables
- Always store secret credentials (Auth Token, API keys) as environment variables, never hard-code them.
Monitor Usage
- Twilio provides a usage dashboard to track your spending, so you don’t exceed budgets or run out of credits unexpectedly.
Twilio’s “pay as you go” model combined with Render’s free tier is an affordable way to build and scale your communication workflows. As you grow, Twilio offers volume discounts and additional channels to help you reach your users on their preferred messaging platform.
Ready to scale? Upgrade your Twilio and Render plans as needed, and incorporate other Twilio services (Voice, Email, WhatsApp, etc.) into your application for a richer customer experience.
Good Luck with your Project!
If you have questions or need assistance, contact support at info@protonest.co.
Ready to start your IoT journey? Visit Protonest’s IoT System Design Tool which will guide you through the complete process of IoT system development.
https://iot-system-design-tool.protonest.co/
Contact us for any consultations or projects related to IoT and embedded systems.
Email: info@protonest.co
Protonest for more details.
Protonest specializes in transforming IoT ideas into reality. We offer prototyping services from concept to completion. Our commitment ensures that your visionary IoT concepts become tangible, innovative, and advanced prototypes.
For more details: https://www.protonest.co/
Cheers!