PHP code example of administrate / sdk

1. Go to this page and download the library: Download administrate/sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

administrate / sdk example snippets


use Administrate\Administrate;

$client = new Administrate(apiKey: 'sk_live_...');

// Get account info
$account = $client->account->get();
echo "{$account->name} ({$account->plan})";

// List all clients with auto-pagination
foreach ($client->clients->list() as $c) {
    echo "{$c->name} ({$c->code})\n";
}

// Check for failed executions across all instances
foreach ($client->executions->list(errorsOnly: true) as $execution) {
    echo "{$execution->workflow_name}: {$execution->error_category}\n";
}

// Get LLM cost summary
$costs = $client->llmCosts->summary();
echo "Total: $" . number_format($costs->data->summary->total_cost_cents / 100, 2);

use Administrate\Administrate;

$client = new Administrate(
    apiKey: 'sk_live_...',            // Required. Must start with "sk_live_"
    baseUrl: 'https://...',           // Default: "https://administrate.dev"
    timeout: 30,                      // Request timeout in seconds. Default: 30
    maxRetries: 3,                    // Retry attempts for failed requests. Default: 3
);

$client = new Administrate(
    apiKey: 'sk_live_...',
    client: $yourPsr18Client,
);

// Get current token info and account summary
$me = $client->account->me();
echo "{$me->token->name} ({$me->token->permission})";
echo "{$me->account->name} ({$me->account->plan})";

// Get full account details
$account = $client->account->get();

// Update account settings
$account = $client->account->update(
    name: 'My Agency',
    billingEmail: '[email protected]',
    timezone: 'Australia/Brisbane',
);

// List all clients (auto-paginates)
foreach ($client->clients->list() as $c) {
    echo "{$c->name} ({$c->code})\n";
}

// Get a client (includes 7-day metrics)
$c = $client->clients->get('com_abc123');
echo "{$c->metrics->success_rate}% success, {$c->metrics->time_saved_minutes} min saved";

// Create a client
$c = $client->clients->create(
    name: 'Acme Corp',
    code: 'acme',
    contactEmail: '[email protected]',
    timezone: 'America/New_York',
);

// Update a client
$c = $client->clients->update('com_abc123', notes: 'Enterprise tier');

// Delete a client (

// List all instances
foreach ($client->instances->list() as $inst) {
    echo "{$inst->name} ({$inst->sync_status})\n";
}

// Filter by client or sync status
foreach ($client->instances->list(clientId: 'com_abc123', syncStatus: 'error') as $inst) {
    echo "{$inst->name}: {$inst->last_sync_error}\n";
}

// Get an instance (_here',
);

// Trigger a sync
$client->instances->sync('n8n_abc123', syncType: 'all');

// Sync all instances at once
$client->instances->syncAll(syncType: 'workflows');

// Update an instance
$inst = $client->instances->update('n8n_abc123', name: 'Staging n8n');

// Delete an instance
$client->instances->delete('n8n_abc123');

// List workflows with filters
foreach ($client->workflows->list(clientId: 'com_abc123', active: true) as $wf) {
    echo "{$wf->name} (active: {$wf->is_active})\n";
}

// Search by name
foreach ($client->workflows->list(search: 'onboarding') as $wf) {
    echo $wf->name . "\n";
}

// Get a workflow ();

// List executions with filters
foreach ($client->executions->list(
    clientId: 'com_abc123',
    status: 'failed',
    startDate: '2025-01-01',
    endDate: '2025-01-31',
) as $ex) {
    echo "{$ex->workflow_name} - {$ex->status} ({$ex->duration_ms}ms)\n";
}

// Get only errors
foreach ($client->executions->list(errorsOnly: true) as $ex) {
    echo "{$ex->error_category}: {$ex->workflow_name}\n";
}

// Get execution details (

// List sync run history
foreach ($client->syncRuns->list(instanceId: 'n8n_abc123', status: 'failed') as $run) {
    echo "{$run->sync_type} - {$run->status} ({$run->duration_seconds}s)\n";
}

// Get a specific sync run
$run = $client->syncRuns->get('syn_abc123');

// Get sync health across all instances
foreach ($client->syncRuns->health() as $entry) {
    echo "{$entry->instance_name} ({$entry->sync_status})\n";
    echo "  Workflows last synced: {$entry->workflows->last_synced_at}\n";
    echo "  Executions last synced: {$entry->executions->last_synced_at}\n";
}

// List team members
foreach ($client->users->list() as $user) {
    echo "{$user->name} <{$user->email}> ({$user->role})\n";
}

// Get a user
$user = $client->users->get('usr_abc123');

// Invite a new team member
$invitation = $client->users->invite(email: '[email protected]', role: 'member');
echo $invitation->expires_at;

// Change a user's role
$user = $client->users->update('usr_abc123', role: 'admin');

// Remove a user
$client->users->delete('usr_abc123');

// List webhooks
foreach ($client->webhooks->list() as $wh) {
    echo "{$wh->url} - " . implode(', ', $wh->events) . " (enabled: {$wh->enabled})\n";
}

// Create a webhook
$wh = $client->webhooks->create(
    url: 'https://example.com/hook',
    events: ['execution.failed', 'sync.failed'],
    description: 'Slack failure alerts',
);
echo $wh->secret; // Save this -- used to verify webhook signatures

// Update a webhook
$wh = $client->webhooks->update('whk_abc123', enabled: false);

// Regenerate the signing secret (old secret becomes invalid immediately)
$wh = $client->webhooks->regenerateSecret('whk_abc123');
echo $wh->secret;

// Delete a webhook
$client->webhooks->delete('whk_abc123');

// List all tokens
foreach ($client->apiTokens->list() as $token) {
    echo "{$token->name} ({$token->permission}) - {$token->token_hint}\n";
}

// Create a token (the plain token is only returned once)
$token = $client->apiTokens->create(
    name: 'CI/CD Pipeline',
    permission: 'read',
    ipAllowlist: ['10.0.0.0/8'],
    expiresIn: '90_days',
);
echo $token->token; // sk_live_... -- save this immediately

// Update a token
$token = $client->apiTokens->update('tok_abc123', name: 'Updated Name');

// Revoke a token
$client->apiTokens->delete('tok_abc123');

// List providers
foreach ($client->llmProviders->list() as $provider) {
    echo "{$provider->name} ({$provider->provider_type}) - {$provider->sync_status}\n";
}

// Get a provider (= $client->llmProviders->create(
    name: 'OpenAI Production',
    providerType: 'openai',  // openai, anthropic, openrouter, or azure
    apiKey: 'sk-...',
    organizationId: 'org-...',
);

// Trigger a cost sync
$client->llmProviders->sync('llm_abc123');

// Update a provider
$provider = $client->llmProviders->update('llm_abc123', name: 'OpenAI Staging');

// Delete a provider
$client->llmProviders->delete('llm_abc123');

// List projects for a provider
foreach ($client->llmProjects->list('llm_abc123') as $project) {
    echo "{$project->name}: {$project->total_cost_cents}c ({$project->client_name})\n";
}

// Assign a project to a client
$project = $client->llmProjects->update(
    'llm_abc123', 'proj_456', clientId: 'com_abc123',
);

// Get cost summary (defaults to last 7 days)
$costs = $client->llmCosts->summary();
echo "Total: $" . number_format($costs->data->summary->total_cost_cents / 100, 2) . "\n";
echo "Tokens: {$costs->data->summary->total_tokens}\n";

// Breakdown by provider
foreach ($costs->data->providers as $p) {
    echo "  {$p->name}: $" . number_format($p->cost_cents / 100, 2) . "\n";
}

// Breakdown by model
foreach ($costs->data->models as $m) {
    echo "  {$m->model}: $" . number_format($m->cost_cents / 100, 2) . "\n";
}

// Daily trend
foreach ($costs->data->daily as $day) {
    echo "  {$day->date}: $" . number_format($day->cost_cents / 100, 2) . "\n";
}

// Custom date range
$costs = $client->llmCosts->summary(
    startDate: '2025-01-01',
    endDate: '2025-01-31',
);

// Costs by client
foreach ($client->llmCosts->byClient()->data as $entry) {
    echo "{$entry->name}: $" . number_format($entry->cost_cents / 100, 2) . "\n";
}

// Costs by provider
foreach ($client->llmCosts->byProvider()->data as $entry) {
    echo "{$entry->name}: $" . number_format($entry->cost_cents / 100, 2) . "\n";
}

// Auto-paginate through all results
foreach ($client->clients->list() as $c) {
    echo $c->name . "\n";
}

// Control page size
foreach ($client->clients->list(perPage: 100) as $c) {
    echo $c->name . "\n";
}

// Get a single page
$page = $client->clients->list(perPage: 10)->firstPage();
echo "{$page->meta->total} total, {$page->meta->total_pages} pages\n";
foreach ($page as $c) {
    echo $c->name . "\n";
}

use Administrate\Administrate;
use Administrate\Exceptions\ApiException;
use Administrate\Exceptions\AuthenticationException;
use Administrate\Exceptions\NotFoundException;
use Administrate\Exceptions\RateLimitException;
use Administrate\Exceptions\ValidationException;

$client = new Administrate(apiKey: 'sk_live_...');

try {
    $c = $client->clients->get('com_nonexistent');
} catch (NotFoundException $e) {
    echo "Not found: {$e->getMessage()}";
} catch (AuthenticationException) {
    echo 'Invalid API key';
} catch (RateLimitException $e) {
    echo "Rate limited. Retry after {$e->retryAfter}s";
} catch (ValidationException $e) {
    echo "Invalid params: " . json_encode($e->body);
} catch (ApiException $e) {
    echo "API error {$e->statusCode}: {$e->getMessage()}";
}

$client = new Administrate(apiKey: 'sk_live_...', maxRetries: 0);