SP SPACEPLACE
EXAMPLE #05 PHP Bash

Auth via request body

Send credentials in the JSON body instead of HTTP headers.

05-send-event-query-params.php
#!/usr/bin/env php
<?php

/**
 * Example: Authenticate via query/form parameters instead of headers
 *
 * The API accepts DataStream ID and Token either as HTTP headers
 * (X-DataStream-ID / X-Token) or as regular request body fields
 * (data_stream_id / token). This example uses the body approach.
 *
 * Usage:
 *   php 05-send-event-query-params.php
 */

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

$payload = [
    // Auth fields sent as part of the JSON body
    'data_stream_id' => $dataStreamId,
    'token'          => $tokenValue,

    // Event fields
    'number' => 7,
    'source' => 'inline-auth-example',
    'key'    => 'status',
    'value'  => 'ok',
];

$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',
    ],
    CURLOPT_POSTFIELDS => json_encode($payload),
]);

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

$body = json_decode($response, true);

if ($httpCode === 201) {
    echo "Event created: {$body['event_id']}\n";
} else {
    echo "Failed (HTTP {$httpCode}):\n";
    print_r($body);
    exit(1);
}
05-send-event-query-params.sh
#!/usr/bin/env bash
#
# Example: Authenticate via request body instead of headers
#
# The API accepts DataStream ID and Token either as HTTP headers
# (X-DataStream-ID / X-Token) or as request body fields
# (data_stream_id / token).
#
# Usage:
#   bash 05-send-event-query-params.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" \
  -d "{
    \"data_stream_id\": \"${DATASTREAM_ID}\",
    \"token\": \"${TOKEN_VALUE}\",
    \"number\": 7,
    \"source\": \"inline-auth-example\",
    \"key\": \"status\",
    \"value\": \"ok\"
  }")

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

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