NextJS Twilio Typescript example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NextApiResponse, NextApiRequest } from "next"; | |
const twillio = require('twilio'); | |
const authToken = process.env.TWILIO_AUTH_TOKEN; | |
const accountSid = process.env.TWILIO_ACCOUNT_SID; | |
const myCellPhoneNumber = process.env.MY_NUMBER; | |
const superSecretAPIKey = 'ABCD1234'; | |
export default async (req: NextApiRequest, res: NextApiResponse) => { | |
const client = twillio(accountSid, authToken); | |
const auth = req.headers.authorization; | |
if (auth === superSecretAPIKey) { | |
await client.messages | |
.create({ | |
body: `\n | |
Create your todo list the night before with categories: | |
\n- Work | |
\n- Health | |
\n- Relationships | |
\n- Self Improvement | |
\n | |
\n Get better every day: https://jamesclear.com/continuous-improvement`, | |
from: '+12694754126', // my twilio testing number | |
to: `+1${myCellPhoneNumber}` | |
}) | |
.then((message: String) => res.json(message), (err: Error) => res.json(err)); | |
} else { | |
return res.status(401).end(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't forget, never commit your secrets to source code😄