PHP code example of dcid / server-sdk

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

    

dcid / server-sdk example snippets


use DCID\ServerSDK\DCIDServerSDK;
use DCID\ServerSDK\Types\InitiateOTPOptions;
use DCID\ServerSDK\Types\ConfirmOTPOptions;

$sdk = new DCIDServerSDK([
    'apiKey' => 'your-api-key',
    'environment' => 'prod', // or 'dev'
]);

// Step 1: Initiate OTP
$result = $sdk->auth->registerOtp(new InitiateOTPOptions(email: '[email protected]'));

// Step 2: Confirm OTP (tokens are automatically stored)
$tokens = $sdk->auth->confirmOtp(new ConfirmOTPOptions(
    otp: '123456',
    email: '[email protected]',
));

// Step 3: Use authenticated endpoints
$key = $sdk->identity->encryption->generateKey(
    new \DCID\ServerSDK\Types\GenerateEncryptionKeyOptions(
        did: 'did:iden3:trustid:main:...',
        ownerEmail: '[email protected]',
    )
);

$sdk = new DCIDServerSDK([
    'apiKey' => 'your-api-key',          // Required
    'environment' => 'prod',              // 'dev' or 'prod' (default: 'prod')
    'timeout' => 30000,                   // Request timeout in ms (default: 30000)
    'defaultHeaders' => [],               // Custom headers
    'logger' => new ConsoleLogger(),      // Custom logger
    'enableRequestLogging' => false,      // Enable request/response logging
]);

use DCID\ServerSDK\Errors\AuthenticationError;
use DCID\ServerSDK\Errors\NetworkError;
use DCID\ServerSDK\Errors\ServerError;
use DCID\ServerSDK\Errors\DCIDServerSDKError;

try {
    $result = $sdk->identity->encryption->generateKey($options);
} catch (AuthenticationError $e) {
    if ($e->isApiKeyError) {
        // Invalid API key
    } else {
        // JWT token issue (expired, invalid)
    }
} catch (NetworkError $e) {
    // Connection timeout, DNS failure, etc.
    echo $e->errorCode; // e.g., 'ECONNREFUSED'
} catch (ServerError $e) {
    if ($e->isBackendConnectivityError) {
        // Backend service unreachable (via KrakenD)
    }
} catch (DCIDServerSDKError $e) {
    // Other API errors (4xx)
    echo $e->statusCode;
    echo $e->context?->url;
}

$sdk->setAuthToken('your-access-token');
$sdk->setRefreshToken('your-refresh-token');

$accessToken = $sdk->getAuthToken();
$refreshToken = $sdk->getRefreshToken();