SP SPACEPLACE
EXAMPLE #11 PHP Bash

List, search, and filter

Pull events with full filter / sort / pagination.

11-list-events.php
#!/usr/bin/env php
<?php

/**
 * Example: List and search events
 *
 * GET /api/events
 *
 * Supported query parameters:
 *   source     - exact match on source
 *   key        - exact match on key
 *   value      - partial match on value (case-insensitive)
 *   number     - exact match on number
 *   number_min - minimum number (inclusive)
 *   number_max - maximum number (inclusive)
 *   from       - events at or after this datetime
 *   to         - events at or before this datetime
 *   search     - search across source, key, and value
 *   sort       - field to sort by (timestamp, number, source, key, value, created_at)
 *   dir        - sort direction (asc, desc) -- default: desc
 *   per_page   - results per page (1-200) -- default: 50
 *   page       - page number
 *
 * Usage:
 *   php 11-list-events.php
 */

$baseUrl       = 'https://www.spaceplace.app';
$dataStreamId  = 'YOUR-DATASTREAM-UUID-HERE';
$tokenValue    = 'YOUR-TOKEN-VALUE-HERE';

// ── List recent events (defaults: newest first, 50 per page) ─────────────────

$params = http_build_query([
    'per_page' => 10,
]);

$ch = curl_init("{$baseUrl}/api/events?{$params}");
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER     => [
        'Accept: application/json',
        "X-DataStream-ID: {$dataStreamId}",
        "X-Token: {$tokenValue}",
    ],
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

$body = json_decode($response, true);

if ($httpCode === 200) {
    echo "Page {$body['current_page']} of {$body['last_page']} ({$body['total']} total events)\n\n";
    foreach ($body['data'] as $event) {
        echo "  [{$event['timestamp']}] #{$event['number']}  {$event['source']}  {$event['key']}={$event['value']}\n";
    }
} else {
    echo "Failed (HTTP {$httpCode}):\n";
    print_r($body);
}

// ── Search by key ────────────────────────────────────────────────────────────

echo "\n--- Events with key='temperature' ---\n";

$params = http_build_query([
    'key'      => 'temperature',
    'per_page' => 5,
    'sort'     => 'timestamp',
    'dir'      => 'desc',
]);

$ch = curl_init("{$baseUrl}/api/events?{$params}");
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER     => [
        'Accept: application/json',
        "X-DataStream-ID: {$dataStreamId}",
        "X-Token: {$tokenValue}",
    ],
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

$body = json_decode($response, true);

if ($httpCode === 200) {
    echo "{$body['total']} matching events:\n";
    foreach ($body['data'] as $event) {
        echo "  [{$event['timestamp']}] {$event['value']}\n";
    }
} else {
    echo "Failed (HTTP {$httpCode})\n";
}

// ── Filter by date range ─────────────────────────────────────────────────────

echo "\n--- Events from March 2026 ---\n";

$params = http_build_query([
    'from'     => '2026-03-01 00:00:00',
    'to'       => '2026-03-31 23:59:59',
    'per_page' => 5,
]);

$ch = curl_init("{$baseUrl}/api/events?{$params}");
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER     => [
        'Accept: application/json',
        "X-DataStream-ID: {$dataStreamId}",
        "X-Token: {$tokenValue}",
    ],
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

$body = json_decode($response, true);

if ($httpCode === 200) {
    echo "{$body['total']} events in range\n";
    foreach ($body['data'] as $event) {
        echo "  [{$event['timestamp']}] {$event['source']} {$event['key']}={$event['value']}\n";
    }
} else {
    echo "Failed (HTTP {$httpCode})\n";
}
11-list-events.sh
#!/usr/bin/env bash
#
# Example: List and search events
#
# GET /api/events
#
# Supported query parameters:
#   source, key, value, number, number_min, number_max,
#   from, to, search, sort, dir, per_page, page
#
# Usage:
#   bash 11-list-events.sh

BASE_URL="https://www.spaceplace.app"
DATASTREAM_ID="YOUR-DATASTREAM-UUID-HERE"
TOKEN_VALUE="YOUR-TOKEN-VALUE-HERE"

HEADERS=(
  -H "Accept: application/json"
  -H "X-DataStream-ID: ${DATASTREAM_ID}"
  -H "X-Token: ${TOKEN_VALUE}"
)

# ── List recent events ────────────────────────────────────────────────────────

echo "--- Recent events (newest first, 10 per page) ---"

result=$(curl -s "${HEADERS[@]}" "${BASE_URL}/api/events?per_page=10")

total=$(echo "$result" | jq '.total')
page=$(echo "$result" | jq '.current_page')
last=$(echo "$result" | jq '.last_page')
echo "Page ${page} of ${last} (${total} total)"
echo "$result" | jq -r '.data[] | "  [\(.timestamp)] #\(.number)  \(.source)  \(.key)=\(.value)"'

# ── Search by key ─────────────────────────────────────────────────────────────

echo ""
echo "--- Events with key='temperature' ---"

result=$(curl -s "${HEADERS[@]}" "${BASE_URL}/api/events?key=temperature&per_page=5")

total=$(echo "$result" | jq '.total')
echo "${total} matching events:"
echo "$result" | jq -r '.data[] | "  [\(.timestamp)] \(.value)"'

# ── Filter by date range ─────────────────────────────────────────────────────

echo ""
echo "--- Events from March 2026 ---"

from="2026-03-01+00:00:00"
to="2026-03-31+23:59:59"

result=$(curl -s "${HEADERS[@]}" "${BASE_URL}/api/events?from=${from}&to=${to}&per_page=5")

total=$(echo "$result" | jq '.total')
echo "${total} events in range:"
echo "$result" | jq -r '.data[] | "  [\(.timestamp)] \(.source)  \(.key)=\(.value)"'

# ── Full-text search ─────────────────────────────────────────────────────────

echo ""
echo "--- Search for 'sensor' ---"

result=$(curl -s "${HEADERS[@]}" "${BASE_URL}/api/events?search=sensor&per_page=5")

total=$(echo "$result" | jq '.total')
echo "${total} results:"
echo "$result" | jq -r '.data[] | "  [\(.timestamp)] \(.source)  \(.key)=\(.value)"'
← All examples