SP SPACEPLACE
EXAMPLE #01 PHP Bash

Send a single event

The minimum POST to /api/events, all fields filled in.

01-send-event.php
#!/usr/bin/env php
<?php

/**
 * Example: Send a single Event to Space Place
 *
 * Usage:
 *   php 01-send-event.php
 *
 * Requirements:
 *   - A valid DataStream UUID
 *   - A valid Token value with an active Provision for the /api/events endpoint
 */

// ── Configuration ────────────────────────────────────────────────────────────

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

// ── Event payload ────────────────────────────────────────────────────────────
// All fields are optional; defaults are applied server-side when omitted.

$event = [
    'number' => 42,
    'source' => 'example-script',
    'key'    => 'temperature',
    'value'  => '72.5',
    // 'timestamp' => '2026-03-31 12:00:00',  // omit to use server time
];

// ── Send the request ─────────────────────────────────────────────────────────

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

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

// ── Handle the response ──────────────────────────────────────────────────────

if ($error) {
    fwrite(STDERR, "cURL error: {$error}\n");
    exit(1);
}

$body = json_decode($response, true);

if ($httpCode === 201) {
    echo "Event created successfully!\n";
    echo "  Event ID:  {$body['event_id']}\n";
    echo "  Timestamp: {$body['timestamp']}\n";
} else {
    echo "Request failed (HTTP {$httpCode}):\n";
    print_r($body);
    exit(1);
}
01-send-event.sh
#!/usr/bin/env bash
#
# Example: Send a single Event to Space Place
#
# Usage:
#   bash 01-send-event.sh

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

response=$(curl -s -w "\n%{http_code}" \
  -X POST "${BASE_URL}/api/events" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -H "X-DataStream-ID: ${DATASTREAM_ID}" \
  -H "X-Token: ${TOKEN_VALUE}" \
  -d '{
    "number": 42,
    "source": "example-script",
    "key": "temperature",
    "value": "72.5"
  }')

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

if [ "$http_code" = "201" ]; then
  echo "Event created successfully!"
  echo "  Event ID:  $(echo "$body" | jq -r '.event_id')"
  echo "  Timestamp: $(echo "$body" | jq -r '.timestamp')"
else
  echo "Request failed (HTTP ${http_code}):"
  echo "$body" | jq .
  exit 1
fi
← All examples