Skip to content

Instantly share code, notes, and snippets.

Created January 12, 2020 00:22
What would you like to do?
NextJS Twilio Typescript example
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
body: `\n
Create your todo list the night before with categories:
\n- Work
\n- Health
\n- Relationships
\n- Self Improvement
\n Get better every day:`,
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();
Copy link

Don't forget, never commit your secrets to source code 😄

Copy link

Update: I use tryslater for cron jobs in Next.js now instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment