This project demonstrates how to implement a user verification system using WhatsApp for sending One-Time Passwords (OTPs). It's built with PHP, uses MongoDB for storing data, and integrates with the official Meta WhatsApp Business API.
- Secure OTP Flow: Sends OTP via WhatsApp API and only saves it to the database upon successful transmission.
- Dynamic Country Codes: Populates a searchable dropdown with country codes from a JSON file.
- Phone Number Validation: Validates phone number length based on the selected country.
- AJAX-driven: Smooth user experience without page reloads.
- User-Friendly Interface: Clean UI with SweetAlert2 for notifications.
- Environment-based Configuration: Keeps your sensitive credentials out of the code using a
.envfile.
- PHP: Version 7.4 or higher.
- Web Server: Apache or Nginx (XAMPP, WAMP, or MAMP are fine for local development).
- Composer: For managing PHP dependencies.
- MongoDB: A running MongoDB instance (local or a cloud service like MongoDB Atlas).
- A Meta (Facebook) Developer Account.
- A WhatsApp Business Account.
- A registered phone number that can make and receive WhatsApp calls/messages.
-
Clone the Repository:
git clone <your-repository-url> cd <your-repository-folder>
-
Install Dependencies: Run Composer to install the required PHP libraries (MongoDB driver, Dotenv).
composer install
-
Configure Environment Variables: Create a
.envfile in the root of the project by copying the example file.copy .env.example .env
Now, open the
.envfile and fill in your specific credentials.# .env # --- Meta WhatsApp API Credentials --- # Your permanent or temporary access token from the Meta App Dashboard WHATSAPP_TOKEN="YOUR_WHATSAPP_API_TOKEN" # The Phone Number ID for your sending number, found in the Meta App Dashboard PHONE_NUMBER_ID="YOUR_SENDER_PHONE_NUMBER_ID" # --- MongoDB Configuration --- # Your MongoDB connection string MONGODB_URI="mongodb://localhost:27017" # The name of the database you want to use MONGODB_DATABASE="whatsapp_verification"
Setting up the WhatsApp API involves several steps across two different websites: the Meta for Developers site (developers.facebook.com) for app configuration and the Meta Business Portfolio (business.facebook.com) for managing business assets like phone numbers and message templates.
You must have a personal Facebook account to create a Meta Developer account and a Meta Business Portfolio.
The Business Portfolio (formerly Business Manager) is where you manage all your business assets.
- Go to the Meta Business Suite and log in with your Facebook account.
- If you don't have a Business Account, you will be prompted to create one. Follow the on-screen instructions to create your business portfolio. You'll need to provide a business name, your name, and a business email.
The app connects your code to Meta's services.
- Go to Meta for Developers and click My Apps.
- Click Create App.
- Select Other as the use case, then select Business as the app type.
- Provide an app name and your contact email.
- Crucially, when prompted, select the Business Portfolio you created in the previous step to link the app to it.
- From your app's dashboard on the developer site, find the "WhatsApp" product and click Set up.
- In the sidebar, under "WhatsApp", click API Setup.
- On this page, you will find your initial test credentials:
- A temporary Access Token (expires in 24 hours).
- A Phone Number ID associated with a test phone number provided by Meta.
- These are great for initial testing but are not for production.
For a live application, you need a token that doesn't expire.
- Navigate to your Meta Business Portfolio:
business.facebook.com. - Go to Settings > Business Settings.
- Under Users, click on System Users.
- Add a new System User. Give it a name (e.g., "WebAppSystemUser") and set the role to Admin.
- Assign Assets:
- Select the System User you just created.
- Click Add Assets.
- In the "Select asset type" column, choose Apps.
- Select your app from the list.
- Enable the Manage App permission.
- Generate Token:
- Click the Generate new token button for that System User.
- Select your app from the dropdown.
- Under "Available permissions", select
whatsapp_business_managementandwhatsapp_business_messaging. - Click Generate Token.
- Important: Copy and save this token immediately. This is your permanent
WHATSAPP_TOKENfor your.envfile. You will not be able to see it again.
WhatsApp requires pre-approved templates for business-initiated messages like OTPs. This is done on the Meta Business Portfolio website.
- From your app's dashboard on the developer site, go to WhatsApp -> API Setup. You will see a link that says "To create your own message templates, click here." This is a shortcut that will take you to the correct page on
business.facebook.com. - Click Create Template.
- Configure the template as follows:
- Category: Authentication.
- Name:
otp(this name must match the one used in the PHP code). - Languages: Select your desired language (e.g.,
English (US)).
- Configure the Template Content:
- Important Note: For
Authenticationtemplates, Meta provides a standardized, uneditable message body to prevent phishing. You cannot change the text like "Your verification code is...". - You will only be able to configure the OTP length and expiry time. The PHP code sends a 6-digit code, so ensure your template reflects that.
- You can add a "Copy Code" button, which is highly recommended for a better user experience.
- Important Note: For
- Submit for Approval:
- Click Submit. Meta's review is usually very fast for authentication templates.
Before sending messages to any number, you must add it to a verified list.
- Go back to the API Setup page on the Meta for Developers site (
developers.facebook.com). - Scroll down to the "Send and receive messages" section.
- Click the "To" field and select Manage phone number list.
- Add a personal WhatsApp number. Meta will send a verification code to that number on WhatsApp to confirm you own it.
- Once verified, this number can receive test messages from your app.
- Place the project folder inside your web server's root directory (e.g.,
C:/xampp/htdocs/). - Start your Apache and MongoDB services.
- Open your web browser and navigate to
http://localhost/your-project-folder/index.php.
You should now see the verification form, ready to send OTPs!