API Reference

PHP SDK

PHP SDK

The Spartera PHP SDK provides native PHP methods to interact with the
Spartera REST API for comprehensive asset management, analytics, and
billing operations.

Installation

Install via Composer:

composer require spartera/api-sdk

Requirements

  • PHP 7.4 or higher
  • Composer for dependency management
  • cURL extension
  • JSON extension

Features

The PHP SDK offers:

  • Native PHP methods for all API endpoints
  • Automated asset management and publishing
  • Analytics and billing operation support
  • Data platform connection management
  • Marketplace integration tools
  • PSR-4 autoloading compliance
  • Exception-based error handling

Configuration

Initialize the client with your API credentials:

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

use Spartera\ApiSdk\Client;

// Using environment variables
$client = new Client();

// Direct configuration
$client = new Client([
    'api_key' => 'your-api-key',
    'endpoint' => 'https://api.spartera.com'
]);

Quick Start

<?php
use Spartera\ApiSdk\Client;

$client = new Client(['api_key' => $_ENV['SPARTERA_API_KEY']]);

// Create data connection
$connection = $client->connections()->create([
    'name' => 'MySQL Production',
    'type' => 'mysql',
    'credentials' => [
        'host' => 'db.example.com',
        'database' => 'analytics',
        'username' => 'api_user',
        'password' => 'secure_password'
    ]
]);

// Create and configure asset
$asset = $client->assets()->create([
    'name' => 'Customer Analytics',
    'type' => 'sql',
    'query' => 'SELECT customer_id, total_orders FROM customers',
    'connection_id' => $connection['id']
]);

// Set pricing strategy
$client->assets()->setPricing($asset['id'], [
    'strategy' => 'subscription',
    'price' => 49.99
]);

// Publish to marketplace
$client->marketplace()->publish($asset['id']);

Connection Management

Support for multiple data platforms:

// PostgreSQL connection
$postgres = $client->connections()->create([
    'name' => 'PostgreSQL Analytics',
    'type' => 'postgresql',
    'credentials' => [
        'host' => 'postgres.example.com',
        'port' => 5432,
        'database' => 'warehouse',
        'username' => 'analytics',
        'password' => 'password'
    ]
]);

// BigQuery connection
$bigquery = $client->connections()->create([
    'name' => 'BigQuery Data',
    'type' => 'bigquery',
    'credentials' => [
        'project_id' => 'my-gcp-project',
        'credentials_json' => file_get_contents('service-account.json')
    ]
]);

Asset Operations

// List assets with pagination
$assets = $client->assets()->list([
    'page' => 1,
    'per_page' => 25,
    'filter' => 'active'
]);

// Retrieve specific asset
$asset = $client->assets()->get('asset-id');

// Update asset metadata
$client->assets()->update('asset-id', [
    'name' => 'Updated Asset Name',
    'description' => 'Enhanced description',
    'tags' => ['analytics', 'customers', 'revenue']
]);

// Archive asset
$client->assets()->archive('asset-id');

Analytics and Reporting

// Usage analytics
$usage = $client->analytics()->getUsage('asset-id', [
    'start_date' => '2024-01-01',
    'end_date' => '2024-01-31',
    'granularity' => 'daily'
]);

// Performance metrics
$performance = $client->analytics()->getPerformance('asset-id');

// Financial reporting
$revenue = $client->billing()->getRevenueReport([
    'start_date' => '2024-01-01',
    'end_date' => '2024-01-31',
    'group_by' => 'asset'
]);

Error Handling

use Spartera\ApiSdk\Exceptions\NotFoundException;
use Spartera\ApiSdk\Exceptions\ApiException;

try {
    $asset = $client->assets()->get('invalid-asset-id');
} catch (NotFoundException $e) {
    echo "Asset not found: " . $e->getMessage();
} catch (ApiException $e) {
    echo "API Error [{$e->getStatusCode()}]: " . $e->getMessage();
} catch (Exception $e) {
    echo "Unexpected error: " . $e->getMessage();
}

Laravel Integration

For Laravel applications, publish the configuration:

php artisan vendor:publish --provider="Spartera\ApiSdk\LaravelServiceProvider"

Configure in config/spartera.php:

return [
    'api_key' => env('SPARTERA_API_KEY'),
    'endpoint' => env('SPARTERA_ENDPOINT', 'https://api.spartera.com'),
    'timeout' => 30,
];

Use via dependency injection:

use Spartera\ApiSdk\Client;

class AnalyticsController extends Controller
{
    public function __construct(private Client $spartera)
    {
    }
    
    public function getAssetData($assetId)
    {
        return $this->spartera->assets()->get($assetId);
    }
}

Batch Operations

// Bulk asset creation
$assetsData = [
    [
        'name' => 'Sales Metrics',
        'type' => 'sql',
        'query' => 'SELECT * FROM sales'
    ],
    [
        'name' => 'User Metrics', 
        'type' => 'sql',
        'query' => 'SELECT * FROM users'
    ]
];

$results = $client->assets()->bulkCreate($assetsData);