├── .env.example ├── .gitignore ├── LICENSE ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html └── manifest.json ├── server └── index.js └── src ├── App.css ├── App.js ├── App.test.js ├── SMSForm.css ├── SMSForm.js ├── index.css ├── index.js ├── logo.svg └── serviceWorker.js /.env.example: -------------------------------------------------------------------------------- 1 | TWILIO_ACCOUNT_SID= 2 | TWILIO_AUTH_TOKEN= 3 | TWILIO_PHONE_NUMBER= 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | 6 | # testing 7 | /coverage 8 | 9 | # production 10 | /build 11 | 12 | # misc 13 | .DS_Store 14 | .env 15 | .env.local 16 | .env.development.local 17 | .env.test.local 18 | .env.production.local 19 | 20 | npm-debug.log* 21 | yarn-debug.log* 22 | yarn-error.log* 23 | 24 | .eslintcache 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2018 Phil Nash 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Send SMS with React and Twilio 2 | 3 | This is an example of sending SMS using React and Twilio. It consists of a `SMSForm` component that communicates with a server endpoint to [send SMS messages via the Twilio REST API](https://www.twilio.com/docs/sms/send-messages). 4 | 5 | To see how to build the project yourself, check out the blog post [How to send an SMS from React with Twilio](https://www.twilio.com/blog/send-an-sms-react-twilio). 6 | 7 | This project was created from the [react-express-starter project](https://github.com/philnash/react-express-starter) and includes a React front end and an Express server. 8 | 9 | ## Running the project 10 | 11 | To run the project you will need a Twilio account and a Twilio phone number that can send SMS messages. Gather your Twilio Account Sid and Auth Token from the [Twilio console](https://www.twilio.com/console) and the phone number. 12 | 13 | Then, clone the project, change into the directory and install the dependencies. 14 | 15 | ```bash 16 | git clone https://github.com/philnash/send-sms-react-twilio.git 17 | cd send-sms-react-twilio 18 | npm install 19 | ``` 20 | 21 | Copy the `.env.example` file to `.env` and fill in your Twilio credentials and phone number. 22 | 23 | Start the application on its own with the command: 24 | 25 | ```bash 26 | npm run dev 27 | ``` 28 | 29 | Open the app at [localhost:3000](http://localhost:3000). You can now use the form to send SMS messages via your Twilio number. 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-express-starter", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react": "^17.0.1", 7 | "react-dom": "^17.0.1", 8 | "react-scripts": "^5.0.1" 9 | }, 10 | "scripts": { 11 | "start": "react-scripts start", 12 | "build": "react-scripts build", 13 | "test": "react-scripts test", 14 | "eject": "react-scripts eject", 15 | "server": "node-env-run server --exec nodemon | pino-colada", 16 | "dev": "run-p server start" 17 | }, 18 | "proxy": "http://localhost:3001", 19 | "eslintConfig": { 20 | "extends": "react-app" 21 | }, 22 | "browserslist": [ 23 | ">0.2%", 24 | "not dead", 25 | "not ie <= 11", 26 | "not op_mini all" 27 | ], 28 | "devDependencies": { 29 | "body-parser": "^1.20.1", 30 | "express": "^4.18.2", 31 | "express-pino-logger": "^5.0.0", 32 | "node-env-run": "^4.0.2", 33 | "nodemon": "^2.0.20", 34 | "npm-run-all": "^4.1.5", 35 | "pino-colada": "^2.1.0", 36 | "twilio": "^3.83.3" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philnash/send-sms-react-twilio/23e81b9d3788607f5a375c874d1e59810ec8214e/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 13 | 14 | 23 | React App 24 | 25 | 26 | 27 | 30 |
31 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": ".", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /server/index.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const bodyParser = require('body-parser'); 3 | const pino = require('express-pino-logger')(); 4 | const client = require('twilio')( 5 | process.env.TWILIO_ACCOUNT_SID, 6 | process.env.TWILIO_AUTH_TOKEN 7 | ); 8 | 9 | const app = express(); 10 | app.use(bodyParser.urlencoded({ extended: false })); 11 | app.use(bodyParser.json()); 12 | app.use(pino); 13 | 14 | app.get('/api/greeting', (req, res) => { 15 | const name = req.query.name || 'World'; 16 | res.setHeader('Content-Type', 'application/json'); 17 | res.send(JSON.stringify({ greeting: `Hello ${name}!` })); 18 | }); 19 | 20 | app.post('/api/messages', (req, res) => { 21 | res.header('Content-Type', 'application/json'); 22 | client.messages 23 | .create({ 24 | from: process.env.TWILIO_PHONE_NUMBER, 25 | to: req.body.to, 26 | body: req.body.body 27 | }) 28 | .then(() => { 29 | res.send(JSON.stringify({ success: true })); 30 | }) 31 | .catch(err => { 32 | console.log(err); 33 | res.send(JSON.stringify({ success: false })); 34 | }); 35 | }); 36 | 37 | app.listen(3001, () => 38 | console.log('Express server is running on localhost:3001') 39 | ); 40 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | animation: App-logo-spin infinite 20s linear; 7 | height: 40vmin; 8 | } 9 | 10 | .App-header { 11 | background-color: #282c34; 12 | min-height: 100vh; 13 | display: flex; 14 | flex-direction: column; 15 | align-items: center; 16 | justify-content: center; 17 | font-size: calc(10px + 2vmin); 18 | color: white; 19 | } 20 | 21 | .App-link { 22 | color: #61dafb; 23 | } 24 | 25 | @keyframes App-logo-spin { 26 | from { 27 | transform: rotate(0deg); 28 | } 29 | to { 30 | transform: rotate(360deg); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import logo from './logo.svg'; 3 | import './App.css'; 4 | import SMSForm from './SMSForm'; 5 | 6 | class App extends Component { 7 | render() { 8 | return ( 9 |
10 |
11 | logo 12 | 13 | 14 |
15 |
16 | ); 17 | } 18 | } 19 | 20 | export default App; 21 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | ReactDOM.unmountComponentAtNode(div); 9 | }); 10 | -------------------------------------------------------------------------------- /src/SMSForm.css: -------------------------------------------------------------------------------- 1 | .sms-form { 2 | text-align: left; 3 | padding: 1em; 4 | } 5 | .sms-form label { 6 | display: block; 7 | } 8 | .sms-form input, 9 | .sms-form textarea { 10 | font-size: 1em; 11 | width: 100%; 12 | box-sizing: border-box; 13 | } 14 | .sms-form div { 15 | margin-bottom: 0.5em; 16 | } 17 | .sms-form button { 18 | font-size: 1em; 19 | width: 100%; 20 | } 21 | .sms-form.error { 22 | outline: 2px solid #f00; 23 | } 24 | -------------------------------------------------------------------------------- /src/SMSForm.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import './SMSForm.css'; 3 | 4 | class SMSForm extends Component { 5 | constructor(props) { 6 | super(props); 7 | this.state = { 8 | message: { 9 | to: '', 10 | body: '' 11 | }, 12 | submitting: false, 13 | error: false 14 | }; 15 | this.onSubmit = this.onSubmit.bind(this); 16 | this.onHandleChange = this.onHandleChange.bind(this); 17 | } 18 | 19 | onSubmit(event) { 20 | event.preventDefault(); 21 | this.setState({ submitting: true }); 22 | fetch('/api/messages', { 23 | method: 'POST', 24 | headers: { 25 | 'Content-Type': 'application/json' 26 | }, 27 | body: JSON.stringify(this.state.message) 28 | }) 29 | .then(res => res.json()) 30 | .then(data => { 31 | if (data.success) { 32 | this.setState({ 33 | error: false, 34 | submitting: false, 35 | message: { 36 | to: '', 37 | body: '' 38 | } 39 | }); 40 | } else { 41 | this.setState({ 42 | error: true, 43 | submitting: false 44 | }); 45 | } 46 | }); 47 | } 48 | 49 | onHandleChange(event) { 50 | const name = event.target.getAttribute('name'); 51 | this.setState({ 52 | message: { ...this.state.message, [name]: event.target.value } 53 | }); 54 | } 55 | 56 | render() { 57 | return ( 58 |
62 |
63 | 64 | 71 |
72 |
73 | 74 |