REward API Setup Guide (Hostinger)
This guide details how to set up the PHP backend API on your Hostinger Premium Hosting account. This API is required for Phase 2 (ESP32 Integration) and for sending real emails (OTPs/Notifications).
1. Prerequisites
- Hostinger Account (Premium Hosting).
- Access to File Manager or FTP.
- Firebase Service Account Key (JSON file).
- Go to Firebase Console > Project Settings > Service Accounts.
- Click Generate new private key.
- Rename the downloaded file to
service-account.json.
2. Installation Steps
Step 1: Prepare the Files
- Locate the
apifolder in your project:/home/hima/Desktop/Github Repos/REward/api. - Copy your
service-account.jsoninto thisapifolder. - Duplicate
config.example.phpand rename it toconfig.php. - Open
config.phpand update the values:- FIREBASE_PROJECT_ID: Your Firebase project ID (e.g.,
reward-app-123). - API_SECRET_KEY: Generate a strong random string (e.g.,
my_super_secret_esp32_key). Save this, you will need it for the ESP32 code later.
- FIREBASE_PROJECT_ID: Your Firebase project ID (e.g.,
Step 2: Upload to Hostinger
- Log in to Hostinger hPanel > File Manager.
- Navigate to
public_html. - Create a new folder named
api. - Upload all files and folders from your local
apidirectory into this newpublic_html/apifolder on Hostinger.- Ensure
endpoints/folder and its contents are uploaded. - Ensure
firebase.php,index.php,config.php, andservice-account.jsonare uploaded.
- Ensure
Step 3: Verify Installation
- Open your browser and visit:
https://your-domain.com/api/ - You should see a JSON response:
json { "status": "ok", "message": "REward API v1.0" }If you see a 404 or 500 error, check that the files are in the correct folder.
3. Testing with Postman
I have updated the Postman Collection in your project to include the new Email Endpoint.
- Open Postman.
- Import
api/REward_API.postman_collection.json. - Click on the collection name "REward API" to edit Variables:
baseUrl: Set tohttps://your-domain.com/api(the URL from Step 3 above).apiKey: Set to the API_SECRET_KEY you defined inconfig.php.testUserId: A valid user ID from your Firestore (copy one from the Firebase Console).testKioskId: Any string (e.g.,kiosk_01).
- Test Email (OTP):
- Go to Notifications > Send OTP Email.
- Update the Body with your actual email address.
- Click Send.
- Check your inbox!
4. Phase 2: ESP32 Integration
Now that the API is running, your ESP32 can send data to it.
Endpoint: POST https://your-domain.com/api/kiosk/transaction
Headers:
- Content-Type: application/json
- X-API-Key: <YOUR_SECRET_KEY>
JSON Body:
{
"kioskId": "esp32_01",
"userId": "user_id_scanned_from_qr",
"plasticCount": 1,
"metalCount": 0
}
This request will: 1. Verify the API Key. 2. Update the User's points in Firestore. 3. Record the transaction. 4. Send a Push Notification to the user's phone.
Next Steps: - Flash your ESP32 with code that performs this HTTP POST request when an item is recycled.