PHP code example of nickmoline / stytch-php

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

    

nickmoline / stytch-php example snippets




use Stytch\Stytch;

$stytch = new Stytch([
    'project_id' => 'project-live-c60c0abe-c25a-4472-a9ed-320c6667d317',
    'secret' => 'secret-live-80JASucyk7z_G8Z-7dVwZVGXL5NT_qGAQ2I=',
]);

$response = $stytch->b2b()->organizations->get('org_id');
$organization = $response->organization;

// Access Carbon instances for date fields
$createdAt = $organization->created_at; // Carbon instance
$updatedAt = $organization->updated_at; // Carbon instance

// Use Carbon's rich date manipulation
echo $createdAt->format('Y-m-d H:i:s'); // "2023-01-01 12:00:00"
echo $createdAt->diffForHumans(); // "2 days ago"
echo $createdAt->isToday(); // true/false
echo $createdAt->addDays(7)->format('Y-m-d'); // Add 7 days

// Session expiration handling
$session = $response->member_session;
$expiresAt = $session->expires_at; // Carbon instance
if ($expiresAt->isPast()) {
    echo "Session has expired";
} else {
    echo "Session expires in " . $expiresAt->diffForHumans();
}

$response = $stytch->b2b()->organizations->get('org_id');
$organization = $response->organization; // Object
echo $organization->organization_name; // Property access
echo $response->request_id; // Debug info

$response = $stytch->b2c()->users->get('user_id');
echo $response['user_id']; // Array access
echo $response['emails'][0]['email']; // Nested array access

$response = $stytch->b2b()->organizations->create([
    'organization_name' => 'Acme Co',
    'organization_slug' => 'acme-co',
    'email_allowed_domains' => ['acme.co'],
]);

// Access the organization object
$organization = $response->organization;
echo $organization->organization_name; // "Acme Co"
echo $organization->organization_id; // "org_1234567890abcdef"
echo $response->request_id; // For debugging

$response = $stytch->b2b()->organizations->get('org_1234567890abcdef');

$organization = $response->organization;
echo $organization->organization_name;
echo $organization->organization_slug;
echo $organization->email_allowed_domains[0]; // Access array properties

$response = $stytch->b2b()->organizations->update('org_1234567890abcdef', [
    'organization_name' => 'Acme Corporation',
    'email_allowed_domains' => ['acme.co', 'acmecorp.com'],
]);

$organization = $response->organization;
echo $organization->organization_name; // "Acme Corporation"

$result = $stytch->b2b()->organizations->delete('org_1234567890abcdef');

$response = $stytch->b2b()->organizations->search([
    'limit' => 10,
    'query' => [
        'operator' => 'AND',
        'operands' => [
            [
                'filter_name' => 'organization_name_fuzzy',
                'filter_value' => 'Acme'
            ]
        ]
    ]
]);

// Access the organizations array
$organizations = $response->organizations;
foreach ($organizations as $org) {
    echo $org->organization_name;
    echo $org->organization_id;
}

// Access metadata
echo $response->results_metadata['total'];

$result = $stytch->b2b()->magicLinks->loginOrSignup([
    'organization_id' => 'org_1234567890abcdef',
    'email_address' => '[email protected]',
]);

$response = $stytch->b2b()->magicLinks->authenticate([
    'token' => 'DOYoip3rvIMMW5lgItikFK-Ak1CfMsgjuiCyI7uuU94=',
]);

// Access session and member information
echo $response->session_token;
echo $response->session_jwt;
echo $response->member->email_address;
echo $response->organization->organization_name;
echo $response->member_authenticated; // boolean

$response = $stytch->b2b()->sessions->authenticate([
    'session_token' => 'session_token_here',
]);

echo $response->session_token;
echo $response->session_jwt;
echo $response->member->member_id;
echo $response->organization->organization_id;

// Access session details
$session = $response->member_session;
echo $session->member_session_id;
echo $session->expires_at;
echo $session->roles[0]; // Access roles array

$session = $stytch->b2b()->sessions->get('session_1234567890abcdef');

$result = $stytch->b2b()->sessions->revoke('session_1234567890abcdef');

$response = $stytch->b2b()->passwords->authenticate([
    'organization_id' => 'org_1234567890abcdef',
    'email_address' => '[email protected]',
    'password' => 'user_password',
]);

echo $response->member_id;
echo $response->organization_id;
echo $response->session_token;
echo $response->member_authenticated;
echo $response->member->email_address;

$response = $stytch->b2b()->passwords->strengthCheck([
    'password' => 'user_password',
    'email_address' => '[email protected]',
]);

echo $response->valid_password; // boolean
echo $response->score; // integer
echo $response->breached_password; // boolean
echo $response->strength_policy;

$result = $stytch->b2b()->oauth->authenticate([
    'oauth_token' => 'oauth_token_here',
]);

$result = $stytch->b2b()->otps->sms->send([
    'organization_id' => 'org_1234567890abcdef',
    'member_id' => 'member_1234567890abcdef',
]);

$result = $stytch->b2b()->otps->sms->authenticate([
    'organization_id' => 'org_1234567890abcdef',
    'member_id' => 'member_1234567890abcdef',
    'code' => '123456',
]);

$result = $stytch->b2b()->otps->email->send([
    'organization_id' => 'org_1234567890abcdef',
    'member_id' => 'member_1234567890abcdef',
]);

$result = $stytch->b2b()->otps->email->authenticate([
    'organization_id' => 'org_1234567890abcdef',
    'member_id' => 'member_1234567890abcdef',
    'code' => '123456',
]);

$result = $stytch->b2b()->totps->create([
    'organization_id' => 'org_1234567890abcdef',
    'member_id' => 'member_1234567890abcdef',
]);

$result = $stytch->b2b()->totps->authenticate([
    'organization_id' => 'org_1234567890abcdef',
    'member_id' => 'member_1234567890abcdef',
    'code' => '123456',
]);

$result = $stytch->b2b()->recoveryCodes->get([
    'organization_id' => 'org_1234567890abcdef',
    'member_id' => 'member_1234567890abcdef',
]);

$result = $stytch->b2b()->recoveryCodes->recover([
    'organization_id' => 'org_1234567890abcdef',
    'member_id' => 'member_1234567890abcdef',
    'recovery_code' => 'recovery_code_here',
]);

$result = $stytch->b2b()->discovery->organizations->list([
    'intermediate_session_token' => 'token_here',
]);

$result = $stytch->b2b()->discovery->organizations->create([
    'intermediate_session_token' => 'token_here',
    'organization_name' => 'New Organization',
]);

$result = $stytch->b2b()->discovery->organizations->get([
    'intermediate_session_token' => 'token_here',
    'organization_id' => 'org_1234567890abcdef',
]);

$result = $stytch->b2b()->discovery->organizations->update([
    'intermediate_session_token' => 'token_here',
    'organization_id' => 'org_1234567890abcdef',
    'organization_name' => 'Updated Organization Name',
]);

$result = $stytch->b2b()->discovery->organizations->delete([
    'intermediate_session_token' => 'token_here',
    'organization_id' => 'org_1234567890abcdef',
]);

$result = $stytch->b2b()->sso->authenticate([
    'sso_token' => 'sso_token_here',
]);

$result = $stytch->b2b()->sso->getConnections([
    'organization_id' => 'org_1234567890abcdef',
]);

$result = $stytch->b2b()->sso->deleteConnection([
    'organization_id' => 'org_1234567890abcdef',
    'connection_id' => 'connection_1234567890abcdef',
]);

// Create SCIM connection
$result = $stytch->b2b()->scim->connection->create([
    'organization_id' => 'org_1234567890abcdef',
    'display_name' => 'SCIM Connection',
    'base_url' => 'https://example.com/scim',
    'bearer_token' => 'token_here',
]);

// Get SCIM connection
$result = $stytch->b2b()->scim->connection->get([
    'organization_id' => 'org_1234567890abcdef',
    'connection_id' => 'connection_1234567890abcdef',
]);

$result = $stytch->b2b()->idp->introspectToken([
    'token' => 'token_here',
    'client_id' => 'client_id_here',
]);

$result = $stytch->b2b()->idp->getToken([
    'token' => 'token_here',
]);

$result = $stytch->b2b()->impersonation->authenticate([
    'impersonation_token' => 'impersonation_token_here',
]);

$result = $stytch->b2b()->rbac->policy();
bash
composer