Skip to content

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

  1. Locate the api folder in your project: /home/hima/Desktop/Github Repos/REward/api.
  2. Copy your service-account.json into this api folder.
  3. Duplicate config.example.php and rename it to config.php.
  4. Open config.php and 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.

Step 2: Upload to Hostinger

  1. Log in to Hostinger hPanel > File Manager.
  2. Navigate to public_html.
  3. Create a new folder named api.
  4. Upload all files and folders from your local api directory into this new public_html/api folder on Hostinger.
    • Ensure endpoints/ folder and its contents are uploaded.
    • Ensure firebase.php, index.php, config.php, and service-account.json are uploaded.

Step 3: Verify Installation

  1. Open your browser and visit: https://your-domain.com/api/
  2. 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.

  1. Open Postman.
  2. Import api/REward_API.postman_collection.json.
  3. Click on the collection name "REward API" to edit Variables:
    • baseUrl: Set to https://your-domain.com/api (the URL from Step 3 above).
    • apiKey: Set to the API_SECRET_KEY you defined in config.php.
    • testUserId: A valid user ID from your Firestore (copy one from the Firebase Console).
    • testKioskId: Any string (e.g., kiosk_01).
  4. 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.