PHP code example of querri / embed

1. Go to this page and download the library: Download querri/embed 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/ */

    

querri / embed example snippets


use Querri\Embed\QuerriClient;

$client = new QuerriClient([
    'api_key' => 'qk_your_api_key',
    'org_id'  => 'org_your_org_id',
]);

$session = $client->getSession([
    'user' => 'customer-42',  // external ID from your system
    'ttl'  => 3600,
]);

echo $session->sessionToken;  // opaque `es_…` session token to pass to the frontend

// public/api/querri-session.php (or your framework's route handler)
$client = new QuerriClient();  // reads QUERRI_API_KEY + QUERRI_ORG_ID from env
$session = $client->getSession([
    'user'   => ['external_id' => $authUser->id, 'email' => $authUser->email],
    // ?: (not ??) so an EMPTY Origin header also falls through to null —
    // same-origin requests and some proxies send none. A null origin makes
    // the SDK use your configured default_origin / QUERRI_EMBED_ORIGIN
    // instead of silently minting a session with no origin binding.
    'origin' => ($_SERVER['HTTP_ORIGIN'] ?? '') ?: null,
    'ttl'    => 3600,
]);
header('Content-Type: application/json');
echo json_encode($session);

// Read from environment variables (QUERRI_API_KEY + QUERRI_ORG_ID)
$client = new QuerriClient();

// API key string shorthand (org_id still read from QUERRI_ORG_ID)
$client = new QuerriClient('qk_...');

// Full config array
$client = new QuerriClient([
    'api_key'     => 'qk_...',
    'org_id'      => 'org_...',
    'host'           => 'https://app.querri.com',
    'timeout'        => 30.0,
    'max_retries'    => 3,
    'default_origin' => 'https://app.yoursite.com',  // or QUERRI_EMBED_ORIGIN
]);

$session = $client->getSession([
    'user' => [
        'external_id' => 'customer-42',
        'email'       => '[email protected]',
        'first_name'  => 'Alice',
    ],
    'access' => [
        'sources' => ['src_sales_data'],
        'filters' => [
            'tenant_id' => 'acme',
            'region'    => ['us-east', 'us-west'],
        ],
    ],
    'origin' => 'https://app.acme.com',
    'ttl'    => 7200,
]);

$session = $client->getSession([
    'user'   => ['external_id' => 'customer-42'],
    'access' => ['policy_ids' => ['pol_abc123', 'pol_def456']],
]);

$session = $client->getSession([
    'user' => ['external_id' => 'customer-42'],
    'role' => 'analyst',   // decided by YOUR server, never taken from the browser request
]);
$session->roleId;          // 'role_…' (or a built-in role id such as 'system:creator') when the role was applied, else null

$session->sessionToken;  // string — opaque `es_…` session token for the embed
$session->expiresIn;     // int — seconds until expiry
$session->userId;        // string — Querri user ID
$session->externalId;    // string|null — your external ID
$session->roleId;        // string|null — the custom role the session runs as (null when none, or an older server ignored `role`)

// JSON-serializable for API responses
echo json_encode($session);
// {"session_token":"...","expires_in":3600,"user_id":"...","external_id":"...","role_id":null}

$user = $client->users->getOrCreate('ext-123', [
    'email' => '[email protected]',
    'role'  => 'member',
]);

$user = $client->users->retrieve('usr_abc123');
$users = $client->users->list(['limit' => 50]);
$client->users->update('usr_abc123', ['first_name' => 'Alice']);
$client->users->del('usr_abc123');

$policy = $client->policies->create([
    'name'        => 'APAC Sales',
    'source_ids'  => ['src_1'],
    'row_filters' => [
        ['column' => 'region', 'values' => ['APAC']],
    ],
]);

$client->policies->assignUsers($policy['id'], ['user_ids' => ['usr_abc123']]);
$client->policies->removeUser($policy['id'], 'usr_abc123');

$session = $client->embed->createSession([
    'user_id' => 'usr_abc123',
    'ttl'     => 3600,
    'role'    => 'analyst',   // optional; response has 'role_id' only when a role was applied
]);

$refreshed = $client->embed->refreshSession($session['session_token']); // keeps the role; echoes 'role_id'
$client->embed->revokeSession('ses_abc123');
$client->embed->revokeUserSessions('usr_abc123');

use Querri\Embed\Exceptions\ApiException;
use Querri\Embed\Exceptions\AuthenticationException;
use Querri\Embed\Exceptions\ErrorCode;
use Querri\Embed\Exceptions\PermissionException;
use Querri\Embed\Exceptions\RateLimitException;
use Querri\Embed\Exceptions\ConfigException;

try {
    $session = $client->getSession([...]);
} catch (AuthenticationException $e) {
    // 401 — invalid API key
    echo "Auth failed: {$e->getMessage()}";
} catch (PermissionException $e) {
    // 403 — branch on the code, not the message
    match ($e->errorCode) {
        ErrorCode::PERMISSION_DENIED => error_log("Denied {$e->permission} ({$e->reason})"),
        ErrorCode::ROLE_DISABLED, ErrorCode::ROLE_NOT_ALLOWED => error_log("Role refused: {$e->getMessage()}"),
        default => error_log("Forbidden: {$e->getMessage()}"),
    };
} catch (RateLimitException $e) {
    // 429 — rate limited (auto-retried up to maxRetries)
    echo "Rate limited, retry after: {$e->retryAfter}s";
} catch (ApiException $e) {
    // Any other API error
    echo "API error {$e->status}: {$e->getMessage()}";
    echo "Request ID: {$e->requestId}";
} catch (ConfigException $e) {
    // Missing API key or invalid config
    echo $e->getMessage();
}

header('Content-Type: application/json');
echo json_encode($session);  // GetSessionResult is JsonSerializable


mermaid
sequenceDiagram
    participant Browser
    participant PHP Server
    participant Querri API

    Browser->>PHP Server: POST /api/querri-session
    PHP Server->>Querri API: getOrCreate user + create session
    Querri API-->>PHP Server: { session_token }
    PHP Server-->>Browser: { session_token }
    Browser->>Querri API: Load embed with token