# Sender ID API - Local Setup Guide

This guide provides detailed instructions for setting up and testing the Sender ID API locally. It explains how to configure the API to connect to both your local database (for API-specific tables) and the live Deywuro database (for user information).

## Prerequisites

- PHP 7.2 or higher
- MySQL/MariaDB database server
- Apache/Nginx web server with PHP support
- Access to Deywuro database credentials
- Git (optional)
- Postman or similar API testing tool (recommended)

## Step 1: Clone or Download the Repository

```bash
# Clone the repository
git clone [repository-url] sender-id-api
# Or just download and extract the ZIP file
```

Navigate to the project directory:

```bash
cd sender-id-api
```

## Step 2: Configure Database Connections

1. Create a copy of the example config file:

```bash
cp config/config.example.php config/config.php
```

2. Edit `config/database.php` to configure both database connections:

```php
<?php
// Database configuration

// API Database (Local)
define('API_DB_HOST', 'localhost');
define('API_DB_USER', 'root');
define('API_DB_PASS', 'Ebogigatt@027');
define('API_DB_NAME', 'sender_id_api');

// Primary Deywuro Database (Live)
define('PRIMARY_DB_HOST', '144.76.195.8'); // Use the actual hostname for the live DB
define('PRIMARY_DB_USER', 'eaidoo');  // Use the provided remote username
define('PRIMARY_DB_PASS', 'eaidoHe11#0');  // Use the provided remote password
define('PRIMARY_DB_NAME', 'hellio');       // Use the actual DB name

```

## Step 3: Create and Initialize the API Database

1. Create the local database:

```bash
mysql -u root -p -e "CREATE DATABASE sender_id_api CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
```

2. Import the schema:

```bash
mysql -u root -p sender_id_api < schema.sql
```

## Step 4: Configure Your Web Server

### For Apache:

1. Make sure mod_rewrite is enabled.
2. Use the provided `.htaccess` file in the project.
3. Configure a virtual host (optional):

```apache
<VirtualHost *:80>
    ServerName api.local
    DocumentRoot /path/to/sender-id-api
    
    <Directory /path/to/sender-id-api>
        AllowOverride All
        Require all granted
    </Directory>
    
    ErrorLog ${APACHE_LOG_DIR}/api-error.log
    CustomLog ${APACHE_LOG_DIR}/api-access.log combined
</VirtualHost>
```

4. Add `api.local` to your hosts file:

```
127.0.0.1 api.local
```

### For Nginx:

Create a server configuration:

```nginx
server {
    listen 80;
    server_name api.local;
    root /path/to/sender-id-api;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}
```

## Step 5: Test the Database Connections

Run the provided database test script to verify both database connections:

```bash
php db-test.php
```

This script will:
- Test the connection to your local API database
- Check for the required tables in your API database
- Test the connection to the remote Deywuro database
- Verify access to the necessary tables in the Deywuro database

If any issues are found, the script will provide helpful error messages.

## Step 6: Initialize API Keys and IP Whitelist

1. First, generate an initial API key by manually inserting it into your local database:

```sql
INSERT INTO api_keys (client_id, key_value, is_active, created_at, updated_at)
VALUES ('test_client', 'test_api_key_12345', 1, NOW(), NOW());
```

2. Then, add your IP to the whitelist:

```sql
INSERT INTO ip_whitelist (client_id, ip_address, description, is_active, created_at, updated_at)
VALUES ('test_client', '127.0.0.1', 'Local development', 1, NOW(), NOW());
```

## Step 7: Test the API

You can use the included `test-api.php` script to test the API. First, modify it to use your local URL and API key:

```php
// Configuration - Replace these with your actual values
$apiBaseUrl = 'http://api.local';  // Or http://localhost/sender-id-api
$apiKey = 'test_api_key_12345';    // The API key you created
```

Then run the script with different test modes:

```bash
# Test sender ID functionality
php test-api.php sender_id

# Test IP whitelist functionality
php test-api.php ip_whitelist

# Test API key management
php test-api.php api_key
```

Alternatively, you can use Postman or any API client to test the API endpoints directly.

## Troubleshooting

### Connection Issues

1. **Database connection fails:**
   - Check your database credentials in config/database.php
   - Ensure MySQL is running and accessible
   - For the remote database, check if your IP is allowed in their firewall

2. **API returns 401 Unauthorized:**
   - Check that you're sending the correct API key in the Authorization header
   - Verify the API key exists and is active in the database

3. **API returns 403 Forbidden:**
   - Your IP address is not whitelisted
   - Add your current IP to the whitelist using a direct database query

### API Testing

If you encounter errors while testing:

1. Check PHP error logs:
   - Apache: `/var/log/apache2/error.log`
   - Nginx: `/var/log/nginx/error.log`
   - PHP-FPM: `/var/log/php-fpm/error.log`

2. Enable debug mode by modifying `config/config.php`:
   ```php
   define('DEBUG_MODE', true);
   ```

3. Check API logs in your database:
   ```sql
   SELECT * FROM api_request_logs ORDER BY id DESC LIMIT 10;
   ```

## Next Steps

After successfully setting up and testing the API locally, you can:

1. Add more test data to your local API database
2. Configure webhook endpoints for notifications
3. Create a client application that uses the API
4. Set up a staging environment for more realistic testing

## Security Considerations

- Never expose your live database credentials or API keys in public repositories
- Always whitelist your IP addresses for production use
- Consider using a VPN or SSH tunnel when connecting to the live database
- Rotate API keys periodically 