Send Otp For Free Using Firebase In Laravel Step By Step Guide No Sms Charges

image
image
image
image
image
image
image
image
Send OTP for Free Using Firebase in Laravel – Step-by-Step Guide (No SMS Charges)

Send OTP for Free Using Firebase in Laravel – Step-by-Step Guide (No SMS Charges)

Firebase provides a powerful and cost-effective solution for sending OTPs (One-Time Passwords) without relying on paid SMS gateways like Twilio, Msg91, or Textlocal. In this guide, you’ll learn how to integrate Firebase OTP verification in a Laravel applicationcompletely free using test numbers or real numbers after enabling billing.

🔗 GitHub Source Code: https://github.com/codehunger-team/send-otp-for-free-using-firebase


Before moving ahead you need to collect your key and other thing from the firebase.


Create a project on firebase first -

To do that you have to create a account on the firebase https://firebase.google.com/

Now see the below image to create a new project on firebase

when you click on that give a name to your project, I have given send free otp

When you click on that it have the look like something below

Now click on all products and choose authentication

When you click on that you have the image like something below click on the get started button

From the authentication you have to choose phone

Now click on the edit button to enable it see the below image

Now click on the gear icon on the left side top then project setting and come bottom, you can see the code <> like symbol as shown in the below image.

When you click on that you have something like the below one

From here you can collect all your firebase SDK detail which we have used in the below code



🚀 Why Use Firebase for OTP?

Firebase Authentication provides a secure and reliable OTP verification system with the following benefits:

  1. ✅ Free testing with predefined numbers
  2. ✅ Instant integration via JavaScript
  3. ✅ reCAPTCHA protection
  4. ✅ Google-backed security & scalability
  5. ✅ Easily extendable for mobile apps or production use

🛠️ Prerequisites

Before getting started, make sure:

  1. You have a Laravel project running
  2. You have a Firebase project created at https://console.firebase.google.com
  3. You’ve created a Web App under that Firebase project

📁 Step 1: Setup Your Laravel Blade View

Create a new Blade file named firebase-otp.blade.php inside resources/views.

<!DOCTYPE html>
<html>
<head>
<title>Firebase OTP Verification</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h2>Send OTP using Firebase in Laravel</h2>

<input type="text" id="phone" placeholder="+91XXXXXXXXXX" />
<div id="recaptcha-container"></div>
<button onclick="sendOTP()">Send OTP</button>

<br><br>
<input type="text" id="otp" placeholder="Enter OTP" />
<button onclick="verifyOTP()">Verify</button>

<!-- Firebase SDKs -->
<script src="https://www.gstatic.com/firebasejs/8.10.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.10.1/firebase-auth.js"></script>

<script>
// Laravel-based ENV values
const firebaseConfig = {
apiKey: "{{ env('FIREBASE_API_KEY') }}",
authDomain: "{{ env('FIREBASE_AUTH_DOMAIN') }}",
projectId: "{{ env('FIREBASE_PROJECT_ID') }}",
appId: "{{ env('FIREBASE_APP_ID') }}",
messagingSenderId: "{{ env('FIREBASE_SENDER_ID') }}",
measurementId: "{{ env('FIREBASE_MEASUREMENT_ID') }}"
};

firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();

window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container', {
size: 'normal',
callback: () => console.log("reCAPTCHA solved")
});

function sendOTP() {
const phoneNumber = document.getElementById("phone").value;
const appVerifier = window.recaptchaVerifier;

auth.signInWithPhoneNumber(phoneNumber, appVerifier)
.then((confirmationResult) => {
window.confirmationResult = confirmationResult;
alert("OTP sent!");
}).catch((error) => {
alert("Error sending OTP: " + error.message);
});
}

function verifyOTP() {
const otp = document.getElementById("otp").value;
window.confirmationResult.confirm(otp)
.then(() => alert("Phone number verified!"))
.catch(() => alert("Invalid OTP"));
}
</script>
</body>
</html>

🔑 Step 2: Add Firebase Config to .env

Update your .env file with values from your Firebase project settings:

FIREBASE_API_KEY=your_firebase_api_key
FIREBASE_AUTH_DOMAIN=your_project.firebaseapp.com
FIREBASE_PROJECT_ID=your_project_id
FIREBASE_APP_ID=your_app_id
FIREBASE_SENDER_ID=your_sender_id
FIREBASE_MEASUREMENT_ID=G-XXXXXXXXXX

You can find these values under:

Firebase Console > Project Settings > General > Your Apps > Firebase SDK Snippet

🧪 Step 3: Add Test Numbers for Free OTP

To test without enabling billing:

  1. Go to Authentication > Sign-in Method > Phone in Firebase.
  2. Scroll down to Phone numbers for testing.
  3. Add a test number and code, e.g.:
Phone: +911234567890
Code: 123456

When you enter this number in your form, Firebase won’t send an actual SMS, but will accept the code you provide.

💳 Step 4 (Optional): Enable Real SMS (Blaze Plan Required)

To send real OTPs via SMS:

  1. Upgrade to Blaze Plan (Pay-as-you-go)
  2. Add a billing account to your project
  3. Enable Phone Authentication in Firebase

Without this, real SMS OTPs will fail with this error:

Error sending OTP: {"error":{"code":400,"message":"BILLING_NOT_ENABLED"}}

🚦 Routes and Controller (Optional)

You can link your view in routes/web.php:

Route::view('/firebase-otp', 'firebase-otp');

Now open: http://yourdomain.com/firebase-otp

🧠 Pro Tips

  1. Always validate user input on both frontend and backend.
  2. Use Firebase's onAuthStateChanged to manage user sessions if needed.
  3. You can extend this flow with user creation, database linking, or OTP-based login.

📦 GitHub Source Code

All code used in this tutorial is available here:

🔗 GitHub Repository:

https://github.com/codehunger-team/send-otp-for-free-using-firebase