Back to Blog
Integration Guides

How to Integrate Twilio with Firebase: Step-by-Step Guide

Connect Twilio to Firebase Cloud Functions to trigger SMS from Firestore document changes, authentication events, and real-time database updates.

DA
Danial A
Senior Twilio Consultant, Telphi Consulting
June 22, 2026
7 min read
Twilio
Integration
Database
How to Integrate Twilio with Firebase: Step-by-Step Guide

Firebase and Twilio integrate through Firebase Cloud Functions that listen to Firestore document events, Authentication triggers, and Realtime Database changes, dispatching Twilio SMS when specific data conditions are met in your Firebase project. This integration is used by mobile app developers and startup teams who run their backend on Firebase and want SMS notifications for new user registrations, order status changes, support ticket updates, and threshold alerts without deploying a separate server. The connection involves writing a Cloud Function in Node.js that imports the Twilio SDK, reads the triggering document or event data, and calls the Twilio Messages API from within the Firebase serverless environment.

What You Need Before You Start

Install the Twilio Node.js SDK in your Firebase Functions project by running npm install twilio from the functions directory inside your Firebase project, which adds the twilio package to the functions/package.json so it is bundled with each Cloud Function deployment. Store your Twilio Account SID, Auth Token, and From Number as Firebase environment configuration variables by running firebase functions:config:set twilio.account_sid='ACxxxx' twilio.auth_token='your_auth_token' twilio.from_number='+14155551234' from the Firebase CLI, and retrieve them in your function code using functions.config().twilio.account_sid to keep credentials out of source code. From Twilio, provision an SMS-capable phone number and complete A2P 10DLC registration in the Twilio Console under Messaging, then Senders, then US A2P 10DLC for US-bound messaging. Ensure your Firebase project is on the Blaze pay-as-you-go plan, as Cloud Functions that make outbound network requests to the Twilio API require the Blaze plan since the free Spark plan restricts outbound calls to Google-owned services only.

Step-by-Step Integration Guide

Write a Firestore onCreate trigger that fires when a new document is created in a specific collection, such as orders, by exporting a Cloud Function using exports.onOrderCreated = functions.firestore.document('orders/{orderId}').onCreate(async (snap, context) => { const order = snap.data(); const client = new twilio(functions.config().twilio.account_sid, functions.config().twilio.auth_token); await client.messages.create({ to: order.customerPhone, from: functions.config().twilio.from_number, body: 'Order ' + context.params.orderId + ' confirmed for ' + order.customerName + '. Total: $' + order.total }); }). Add an onUpdate trigger for status change notifications by exporting a second function using functions.firestore.document('orders/{orderId}').onUpdate(async (change, context) => { const before = change.before.data(); const after = change.after.data(); if (before.status === after.status) return null; if (!after.customerPhone) return null; const client = new twilio(functions.config().twilio.account_sid, functions.config().twilio.auth_token); return client.messages.create({ to: after.customerPhone, from: functions.config().twilio.from_number, body: 'Your order status changed to ' + after.status }); }). Add a Firebase Authentication onCreate trigger for new user SMS welcome messages by exporting exports.onUserCreated = functions.auth.user().onCreate(async (user) => { if (!user.phoneNumber) return null; const client = new twilio(functions.config().twilio.account_sid, functions.config().twilio.auth_token); return client.messages.create({ to: user.phoneNumber, from: functions.config().twilio.from_number, body: 'Welcome to our platform, ' + (user.displayName || 'friend') + '. Your account is now active.' }); }). Deploy the functions by running firebase deploy --only functions from your project root.

Common Issues and How to Fix Them

Firebase Cloud Functions deployed to the us-central1 region may experience cold start latency of several seconds when a new document is created during low-traffic periods, causing the Twilio SMS to arrive noticeably after the Firestore write that triggered it. Mitigate cold starts by setting the minInstances option on your function to 1 using functions.runWith({ minInstances: 1 }).firestore.document('orders/{orderId}').onCreate() to keep a warm instance always available, accepting the small ongoing cost of keeping one function instance alive. The Twilio SDK import in Firebase Functions fails with a cannot find module twilio error when the package was installed in the project root rather than the functions subdirectory, which is a common mistake in Firebase projects where there is both a root package.json and a functions/package.json. Always run npm install twilio from within the functions/ directory specifically, and verify the twilio entry appears in functions/package.json dependencies before deploying. Firebase Functions onUpdate triggers fire for every field change in a document including internal metadata updates, which can cause multiple SMS to be sent to the same customer for a single logical state change when multiple fields are updated in a single operation. Add a guard condition that compares only the specific field that represents state, such as status or stage, and return null immediately if that field has not changed, preventing spurious SMS from incidental field updates.

How to Get More from This Integration

Build a Firebase Realtime Database alert for threshold conditions by creating a Cloud Function triggered on functions.database.ref('metrics/{metricId}').onWrite() that reads the new value after the write, compares it against a threshold stored in the same database path or in a Firestore config collection, and dispatches a Twilio SMS to an operations phone when the metric exceeds the threshold, enabling real-time operational alerting directly from your Firebase data. Add Twilio Verify OTP for Firebase phone authentication by creating an HTTPS Cloud Function that accepts a phone number, calls the Twilio Verify API at POST https://verify.twilio.com/v2/Services/{ServiceSid}/Verifications with channel set to sms, and returns the Verify service SID to the client, then creates a second HTTPS Cloud Function that accepts the OTP code and phone and calls POST https://verify.twilio.com/v2/Services/{ServiceSid}/VerificationChecks to validate the code, using this as a custom authentication provider alongside Firebase Auth. Create a Firestore-backed SMS queue by writing a Cloud Function triggered on a Cloud Scheduler job every minute that queries Firestore for documents in a scheduled_sms collection where send_at is less than or equal to the current server timestamp and status equals pending, dispatches Twilio SMS for each qualifying document using Promise.all, and updates each document's status to sent or failed based on the Twilio API response, building a scheduled SMS system without a separate cron server. Extend the integration to Twilio WhatsApp by changing the From and To parameters in your Cloud Function's client.messages.create call to use the whatsapp: prefix, enabling Firebase event-driven WhatsApp notifications with no additional authentication setup beyond changing the sender format.

Conclusion

Firebase Cloud Functions and Twilio together create a fully serverless SMS notification system that responds to your application's data changes in real time without any server management. Contact Telphi Consulting to architect and deploy a Firebase Twilio SMS integration tailored to your application's event model.

Share this article:
0 views

Ready to Transform Your Business Communications?

Get a free consultation with our VoIP experts and discover how we can help you save costs, improve efficiency, and scale your business.

Comments (0)

Join the discussion and share your thoughts (AI-moderated for quality)

Protected by AI moderation

Be the first to comment

No comments yet. Share your thoughts below.