SP SPACEPLACE
EXAMPLE #13 PHP Bash

Get a single event by ID

Retrieve one event when you already know its ID.

13-get-event.php
#!/usr/bin/env php
<?php

/**
 * Example: Retrieve a single event by ID
 *
 * GET /api/events/{event-uuid}
 *
 * Usage:
 *   php 13-get-event.php <event-uuid>
 */

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

$eventId = $argv[1] ?? 'YOUR-EVENT-UUID-HERE';

$ch = curl_init("{$baseUrl}/api/events/{$eventId}");
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) {
    $e = $body['event'];
    echo "Event: {$e['id']}\n";
    echo "  Timestamp:  {$e['timestamp']}\n";
    echo "  Number:     {$e['number']}\n";
    echo "  Source:     {$e['source']}\n";
    echo "  Key:        {$e['key']}\n";
    echo "  Value:      {$e['value']}\n";
    echo "  Remote IP:  {$e['remote_ip']}\n";
    echo "  Created:    {$e['created_at']}\n";
} elseif ($httpCode === 404) {
    echo "Event not found.\n";
} else {
    echo "Failed (HTTP {$httpCode}):\n";
    print_r($body);
}
13-get-event.sh
#!/usr/bin/env bash
#
# Example: Retrieve a single event by ID
#
# GET /api/events/{event-uuid}
#
# Usage:
#   bash 13-get-event.sh <event-uuid>

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

EVENT_ID="${1:-YOUR-EVENT-UUID-HERE}"

response=$(curl -s -w "\n%{http_code}" \
  -H "Accept: application/json" \
  -H "X-DataStream-ID: ${DATASTREAM_ID}" \
  -H "X-Token: ${TOKEN_VALUE}" \
  "${BASE_URL}/api/events/${EVENT_ID}")

http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')

if [ "$http_code" = "200" ]; then
  echo "Event: $(echo "$body" | jq -r '.event.id')"
  echo "  Timestamp:  $(echo "$body" | jq -r '.event.timestamp')"
  echo "  Number:     $(echo "$body" | jq -r '.event.number')"
  echo "  Source:     $(echo "$body" | jq -r '.event.source')"
  echo "  Key:        $(echo "$body" | jq -r '.event.key')"
  echo "  Value:      $(echo "$body" | jq -r '.event.value')"
  echo "  Remote IP:  $(echo "$body" | jq -r '.event.remote_ip')"
  echo "  Created:    $(echo "$body" | jq -r '.event.created_at')"
elif [ "$http_code" = "404" ]; then
  echo "Event not found."
else
  echo "Failed (HTTP ${http_code}):"
  echo "$body" | jq .
fi
← All examples