# 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:

  1. Connects to the 1rwn.com fire incident API
  2. Fetches recent fire and emergency incidents
  3. Filters for NY and NJ locations only
  4. Stores new incidents in the fire_incidents table
  5. 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!!

# 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:

  1. Are type "FIRE" or "SERVICES"
  2. Located in NY or NJ states
  3. Have valid date within last 24 hours
  4. Don't already exist in database (by incident_id)

# Environment Variables

Required:

# 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`);