#
Fetch Fire Alerts Function
Fetches fire incident data from the 1rwn.com API for NY and NJ locations.
#
Endpoint
POST /functions/v1/fetch-fire-alerts
#
Description
This function:
- Connects to the 1rwn.com fire incident API
- Fetches recent fire and emergency incidents
- Filters for NY and NJ locations only
- Stores new incidents in the
fire_incidentstable - Runs automatically every 10 minutes via cron job
#
Request
No parameters required - typically called by cron scheduler.
POST /functions/v1/fetch-fire-alerts
Authorization: Bearer YOUR_SERVICE_ROLE_KEY
Content-Type: application/json
{}
#
Response
Success Response:
{
"success": true,
"message": "Processed 5 fire incidents",
"incidents_added": 3,
"incidents_skipped": 2
}
Error Response:
{
"success": false,
"error": "Failed to fetch incidents",
"details": "API timeout"
}
#
External API Details
#
1rwn.com API
- Endpoint:
https://www.1rwn.com/restapi/GetIncidents/ - Method: GET
- Authentication: Basic Auth
- Username:
conceptlab - Password:
#bVSmr2u!!
- Username:
#
API Response Format
{
"GetIncidentsResult": [
{
"IncidentID": "2025-001",
"IncidentType": "FIRE",
"IncidentDate": "01/22/2025",
"State": "NY",
"City": "New York",
"Address": "123 Main St",
"Latitude": 40.7128,
"Longitude": -74.0060
}
]
}
#
Database Storage
Incidents are stored in fire_incidents table:
INSERT INTO fire_incidents (
incident_id,
incident_type,
incident_date,
state,
city,
address,
latitude,
longitude,
raw_data
) VALUES (
'2025-001',
'FIRE',
'2025-01-22',
'NY',
'New York',
'123 Main St',
40.7128,
-74.0060,
'{"source": "1rwn.com", ...}'
);
#
Filtering Logic
Only processes incidents that:
- Are type "FIRE" or "SERVICES"
- Located in NY or NJ states
- Have valid date within last 24 hours
- Don't already exist in database (by incident_id)
#
Environment Variables
Required:
FIRE_ALERT_API_URL- API endpoint (default: https://www.1rwn.com/restapi/GetIncidents/)FIRE_ALERT_USERNAME- API usernameFIRE_ALERT_PASSWORD- API password
#
Cron Schedule
Configured in Supabase dashboard:
SELECT cron.schedule(
'fetch-fire-alerts',
'*/10 * * * *', -- Every 10 minutes
$$
SELECT net.http_post(
'https://edknwrcztqwhskjpoxta.supabase.co/functions/v1/fetch-fire-alerts',
headers => jsonb_build_object(
'Authorization', 'Bearer ' || current_setting('app.settings.service_role_key')
)
);
$$
);
#
Error Handling
The function handles:
- API connection failures
- Invalid response formats
- Duplicate incident IDs
- Missing required fields
- Database insertion errors
All errors are logged and don't stop processing of other incidents.
#
Usage Example
Manual trigger:
const response = await fetch(
'https://edknwrcztqwhskjpoxta.supabase.co/functions/v1/fetch-fire-alerts',
{
method: 'POST',
headers: {
'Authorization': `Bearer ${serviceRoleKey}`,
'Content-Type': 'application/json'
}
}
);
const result = await response.json();
console.log(`Added ${result.incidents_added} new incidents`);