Logo

TrafficLight Systems API

Introduction

The TrafficLight API provides programmatic access to traffic light data including status, timing, and location information. This API allows developers to integrate real-time traffic light information into their applications.

Base URL: https://imators.systems/traffic/api.php

Authentication

All API requests require authentication using an API key. You can obtain an API key from the admin panel.

Include your API key as a query parameter in all requests:

GET https://imators.systems/traffic/api.php?endpoint=lights&api_key=YOUR_API_KEY

Rate Limits

The API currently has the following rate limits:

  • 100 requests per minute
  • 5,000 requests per day

Exceeding these limits will result in a 429 Too Many Requests response.

Error Handling

The API uses standard HTTP status codes to indicate the success or failure of a request.

Status Code Description
200 OK - The request was successful
400 Bad Request - The request is invalid or missing required parameters
401 Unauthorized - Invalid or missing API key
404 Not Found - The requested resource was not found
429 Too Many Requests - Rate limit exceeded
500 Internal Server Error - Something went wrong on the server

Error responses include a JSON body with details:

{
  "status": "error",
  "message": "Description of the error"
}

Get All Lights

Retrieve a list of all traffic lights in the system.

Request

GET https://imators.systems/traffic/api.php?endpoint=lights&api_key=YOUR_API_KEY

Response

{
  "status": "success",
  "data": [
    {
      "id": "1",
      "name": "Main Street & First Avenue",
      "latitude": "51.509865",
      "longitude": "-0.118092",
      "direction": "north",
      "red_duration": "60",
      "green_duration": "30",
      "status": {
        "current": "RED",
        "next_change": 25,
        "next_state": "GREEN",
        "cycle_position": 58.33
      }
    },
    {
      "id": "2",
      "name": "Broadway & Park Place",
      "latitude": "51.507652",
      "longitude": "-0.127758",
      "direction": "east",
      "red_duration": "45",
      "green_duration": "30",
      "status": {
        "current": "GREEN",
        "next_change": 12,
        "next_state": "RED",
        "cycle_position": 70.0
      }
    }
  ]
}

Get Light by ID

Retrieve detailed information about a specific traffic light.

Request

GET https://imators.systems/traffic/api.php?endpoint=light&id=1&api_key=YOUR_API_KEY

Parameters

Parameter Type Description
id integer The ID of the traffic light (required)

Response

{
  "status": "success",
  "data": {
    "id": "1",
    "name": "Main Street & First Avenue",
    "latitude": "51.509865",
    "longitude": "-0.118092",
    "direction": "north",
    "red_duration": "60",
    "green_duration": "30",
    "status": {
      "current": "RED",
      "next_change": 25,
      "next_state": "GREEN",
      "cycle_position": 58.33
    }
  }
}

Get Nearby Lights

Retrieve traffic lights within a specified radius of a location.

Request

GET https://imators.systems/traffic/api.php?endpoint=nearby&lat=51.509865&lng=-0.118092&radius=1&api_key=YOUR_API_KEY

Parameters

Parameter Type Description
lat float Latitude of the center point (required)
lng float Longitude of the center point (required)
radius float Radius in kilometers (default: 1)

Response

{
  "status": "success",
  "data": [
    {
      "id": "1",
      "name": "Main Street & First Avenue",
      "latitude": "51.509865",
      "longitude": "-0.118092",
      "direction": "north",
      "red_duration": "60",
      "green_duration": "30",
      "distance": 0.0,
      "status": {
        "current": "RED",
        "next_change": 25,
        "next_state": "GREEN",
        "cycle_position": 58.33
      }
    },
    {
      "id": "5",
      "name": "Oxford Street & Bond Street",
      "latitude": "51.514756",
      "longitude": "-0.119434",
      "direction": "west",
      "red_duration": "50",
      "green_duration": "40",
      "distance": 0.78,
      "status": {
        "current": "GREEN",
        "next_change": 15,
        "next_state": "RED",
        "cycle_position": 62.5
      }
    }
  ]
}

Add Light

Add a new traffic light to the system.

Request

POST https://imators.systems/traffic/api.php?endpoint=light&api_key=YOUR_API_KEY
Content-Type: application/json

{
    "name": "Oak Street & Maple Avenue",
  "latitude": "51.512756",
  "longitude": "-0.121892",
  "direction": "south",
  "red_duration": "55",
  "green_duration": "35"
}

Parameters

Parameter Type Description
name string Name of the traffic light (required)
latitude float Latitude coordinate (required)
longitude float Longitude coordinate (required)
direction string Direction (north, east, south, west) (required)
red_duration integer Duration of red light in seconds (required)
green_duration integer Duration of green light in seconds (required)

Response

{
  "status": "success",
  "data": {
    "id": "6",
    "name": "Oak Street & Maple Avenue",
    "latitude": "51.512756",
    "longitude": "-0.121892",
    "direction": "south",
    "red_duration": "55",
    "green_duration": "35",
    "status": {
      "current": "RED",
      "next_change": 55,
      "next_state": "GREEN",
      "cycle_position": 0.0
    }
  }
}

Update Light

Update an existing traffic light.

Request

POST https://imators.systems/traffic/api.php?endpoint=update&api_key=YOUR_API_KEY
Content-Type: application/json

{
  "id": 6,
  "name": "Oak Street & Maple Avenue",
  "red_duration": "60",
  "green_duration": "40"
}

Parameters

Parameter Type Description
id integer ID of the traffic light to update (required)
name string Name of the traffic light (optional)
latitude float Latitude coordinate (optional)
longitude float Longitude coordinate (optional)
direction string Direction (north, east, south, west) (optional)
red_duration integer Duration of red light in seconds (optional)
green_duration integer Duration of green light in seconds (optional)

Response

{
  "status": "success",
  "data": {
    "id": "6",
    "name": "Oak Street & Maple Avenue",
    "latitude": "51.512756",
    "longitude": "-0.121892",
    "direction": "south",
    "red_duration": "60",
    "green_duration": "40",
    "status": {
      "current": "GREEN",
      "next_change": 22,
      "next_state": "RED",
      "cycle_position": 45.0
    }
  }
}

JavaScript SDK

Use our JavaScript SDK to easily integrate TrafficLight API in your web applications.

Installation

<script src="https://imators.systems/traffic/traffic-api-sdk.js"></script>

Usage Example

// Initialize the SDK with your API key
const trafficAPI = new TrafficLightAPI('YOUR_API_KEY');

// Get all traffic lights
trafficAPI.getAllLights()
  .then(lights => {
    console.log('All lights:', lights);
  })
  .catch(error => {
    console.error('Error:', error);
  });

// Get nearby traffic lights
trafficAPI.getNearbyLights(51.509865, -0.118092, 1)
  .then(lights => {
    console.log('Nearby lights:', lights);
  })
  .catch(error => {
    console.error('Error:', error);
  });

// Get a specific traffic light
trafficAPI.getLightById(1)
  .then(light => {
    console.log('Light details:', light);
  })
  .catch(error => {
    console.error('Error:', error);
  });

PHP SDK

Use our PHP SDK to easily integrate TrafficLight API in your PHP applications.

Installation

composer require trafficlight/api-sdk

Usage Example

<?php
require 'vendor/autoload.php';

use TrafficLight\ApiSdk\TrafficLightAPI;

// Initialize the SDK with your API key
$trafficAPI = new TrafficLightAPI('YOUR_API_KEY');

// Get all traffic lights
try {
    $lights = $trafficAPI->getAllLights();
    print_r($lights);
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}

// Get nearby traffic lights
try {
    $nearbyLights = $trafficAPI->getNearbyLights(51.509865, -0.118092, 1);
    print_r($nearbyLights);
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}

// Get a specific traffic light
try {
    $light = $trafficAPI->getLightById(1);
    print_r($light);
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}