Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active April 19, 2018 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelmota/fcd2464cbb4f50264c2b4cfd62616413 to your computer and use it in GitHub Desktop.
Save miguelmota/fcd2464cbb4f50264c2b4cfd62616413 to your computer and use it in GitHub Desktop.
Node.js twilio send SMS
const twilio = require('twilio')
// Twilio Credentials
const accountSid = 'abc123';
const authToken = 'abc123';
const client = new twilio(accountSid, authToken);
const fromNumber = '12225558888' // account phone number
export function normalizePhone(phone) {
let digits = phone.replace(/[^\d]+/g, '')
if (digits.length === 10) {
digits = `1${digits}` // US country code required for SNS
}
return `+${digits}`
}
export async function send(phone, message) {
const response = await client.messages.create({
to: normalizePhone(phone),
from: fromNumber,
body: message,
})
return response.id
// send(17774449999, 'hello')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment