PHP code example of sikkerkey / sdk

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

    

sikkerkey / sdk example snippets




ikkerKey\SikkerKey;

$sikkerKey = SikkerKey::create('vault_abc123');
$apiKey = $sikkerKey->getSecret('sk_stripe_key');

// Select a registered vault.
$byVault = SikkerKey::create('vault_abc123');

// Load a specific identity file.
$byPath = SikkerKey::create(
    '/etc/sikkerkey/vaults/vault_abc123/identity.json'
);

// Use SIKKERKEY_IDENTITY or auto-select the only registered vault.
$automatically = SikkerKey::create();

$sikkerKey = SikkerKey::bootstrapInMemory(
    getenv('SIKKERKEY_VAULT_ID'),
    getenv('SIKKERKEY_ENROLLMENT_TOKEN'),
);

$databaseUrl =
    $sikkerKey->getSecret('sk_db_prod');

$sikkerKey = SikkerKey::bootstrapInMemory(
    $vaultId,
    $enrollmentToken,
    hostname: 'worker-1',
    name: 'invoice-runner',
);

$apiKey =
    $sikkerKey->getSecret('sk_stripe_prod');

$database =
    $sikkerKey->getFields('sk_db_prod');

$host = $database['host'];
$username = $database['username'];
$password = $database['password'];

$password = $sikkerKey->getField(
    'sk_db_prod',
    'password'
);

$secrets = $sikkerKey->listSecrets();

foreach ($secrets as $secret) {
    echo "{$secret->id}: {$secret->name}\n";
}

$productionSecrets =
    $sikkerKey->listSecretsByProject(
        'proj_production'
    );

$configuration = $sikkerKey->export();

$productionConfiguration =
    $sikkerKey->export('proj_production');

foreach ($configuration as $name => $value) {
    putenv("{$name}={$value}");
}

$sikkerKey = SikkerKey::create('vault_abc123')
    ->enableCache();

$sikkerKey = SikkerKey::create('vault_abc123')
    ->enableCache(
        maxAge: 3600,
        onFallback: static function (
            string $secretId,
            int $cachedAt
        ): void {
            error_log(
                "Using cached value for {$secretId} from epoch {$cachedAt}"
            );
        },
    );

$production =
    SikkerKey::create('vault_production');
$staging =
    SikkerKey::create('vault_staging');

$productionKey =
    $production->getSecret('sk_api_key');
$stagingKey =
    $staging->getSecret('sk_api_key');

$vaultIds = SikkerKey::listVaults();

echo $sikkerKey->machineId;
echo $sikkerKey->machineName;
echo $sikkerKey->vaultId;
echo $sikkerKey->apiUrl;

echo $sikkerKey->machineId();
echo $sikkerKey->machineName();
echo $sikkerKey->vaultId();
echo $sikkerKey->apiUrl();

use SikkerKey\AccessDeniedError;
use SikkerKey\ApiError;
use SikkerKey\AuthenticationError;
use SikkerKey\ConfigurationError;
use SikkerKey\NotFoundError;
use SikkerKey\RateLimitedError;

try {
    $value =
        $sikkerKey->getSecret('sk_example');
} catch (NotFoundError $error) {
    error_log('Secret not found');
} catch (AccessDeniedError $error) {
    error_log('Access denied');
} catch (AuthenticationError $error) {
    error_log('Authentication failed');
} catch (RateLimitedError $error) {
    error_log('Request remained rate-limited');
} catch (ApiError $error) {
    error_log(
        "SikkerKey returned HTTP {$error->httpStatus}"
    );
} catch (ConfigurationError $error) {
    error_log('Machine identity could not be loaded');
}