PHP code example of seamapi / seam

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

    

seamapi / seam example snippets


$seam = new Seam\Seam();

$devices = $seam->devices->list();

$seam = new Seam\Seam();

$lock = $seam->locks->get(name: "Front Door");
$seam->locks->unlock_door(device_id: $lock->device_id);

// Set the SEAM_API_KEY environment variable
$seam = new Seam\Seam();

// Pass as an option to the constructor
$seam = new Seam\Seam(api_key: "your-api-key");

// Use the factory method
$seam = Seam\Seam::from_api_key("your-api-key");

// Set the SEAM_PERSONAL_ACCESS_TOKEN and SEAM_WORKSPACE_ID environment variables
$seam = new Seam\Seam();

// Pass as options to the constructor
$seam = new Seam\Seam(
    personal_access_token: "your-personal-access-token",
    workspace_id: "your-workspace-id"
);

// Use the factory method
$seam = Seam\Seam::from_personal_access_token(
    "your-personal-access-token",
    "your-workspace-id"
);

use Seam\ActionAttemptFailedError;
use Seam\ActionAttemptTimeoutError;

try {
    $seam->locks->unlock_door(device_id: $device_id);
} catch (ActionAttemptFailedError $error) {
    print "Could not unlock the door: " . $error->getMessage();
    print "Error code: " . $error->getErrorCode();
} catch (ActionAttemptTimeoutError $error) {
    print "The door did not unlock in time";
    print "Action attempt: " . $error->getActionAttempt()->action_attempt_id;
}

use Seam\Resources\ActionAttempt\UnlockDoor;

$action_attempt = $seam->locks->unlock_door(
    device_id: $device_id,
    wait_for_action_attempt: false
);

if ($action_attempt instanceof UnlockDoor\Success) {
    var_dump($action_attempt->result); // The result is populated here.
}

if ($action_attempt instanceof UnlockDoor\Error) {
    print $action_attempt->error->message; // The error is populated here.
}

$seam = new Seam\Seam(wait_for_action_attempt: false);

$action_attempt = $seam->locks->unlock_door(device_id: $device_id);
$action_attempt->status; // "pending"

$action_attempt = $seam->locks->unlock_door(
    device_id: $device_id,
    wait_for_action_attempt: false
);

$seam = new Seam\Seam(
    wait_for_action_attempt: ["timeout" => 30.0, "polling_interval" => 2.0]
);

$seam->locks->unlock_door(
    device_id: $device_id,
    wait_for_action_attempt: ["timeout" => 5.0]
);

use Seam\NullValue;

// Leaves the name unchanged.
$seam->devices->update(device_id: $device_id, name: null);

// Unsets the name.
$seam->devices->update(device_id: $device_id, name: NullValue::NULL);

$pages = $seam->createPaginator(
    fn($params) => $seam->connected_accounts->list(...$params),
    ["limit" => 2]
);

[$connectedAccounts, $pagination] = $pages->firstPage();

if ($pagination->has_next_page) {
    [$moreConnectedAccounts] = $pages->nextPage($pagination->next_page_cursor);
}

$params = ["limit" => 20];

$pages = $seam->createPaginator(
    fn($p) => $seam->connected_accounts->list(...$p),
    $params
);

[$connectedAccounts, $pagination] = $pages->firstPage();

// Store pagination state for later use
file_put_contents(
    "/tmp/seam_connected_accounts_list.json",
    json_encode([$params, $pagination])
);

$stored_data = json_decode(
    file_get_contents("/tmp/seam_connected_accounts_list.json") ?: "[]",
    false
);

$params = (array) ($stored_data[0] ?? []);
$pagination =
    $stored_data[1] ??
    (object) ["has_next_page" => false, "next_page_cursor" => null];

if ($pagination->has_next_page) {
    $pages = $seam->createPaginator(
        fn($p) => $seam->connected_accounts->list(...$p),
        $params
    );
    [$moreConnectedAccounts] = $pages->nextPage($pagination->next_page_cursor);
}

$pages = $seam->createPaginator(
    fn($p) => $seam->connected_accounts->list(...$p),
    ["limit" => 20]
);

foreach ($pages->flatten() as $connectedAccount) {
    print $connectedAccount->account_type_display_name . "\n";
}

$pages = $seam->createPaginator(
    fn($p) => $seam->connected_accounts->list(...$p),
    ["limit" => 20]
);

$connectedAccounts = $pages->flattenToArray();

// Set the SEAM_PERSONAL_ACCESS_TOKEN environment variable
$seam = new Seam\SeamWithoutWorkspace();

// Use the factory method
$seam = Seam\SeamWithoutWorkspace::from_personal_access_token(
    "your-personal-access-token"
);

// List workspaces authorized for this Personal Access Token
$workspaces = $seam->workspaces->list();

$workspace = $seam->workspaces->create(
    name: "New Workspace",
    connect_partner_name: "Your Company"
);

$webhook = new Seam\SeamWebhook($webhook_secret);

try {
    $event = $webhook->verify($request_body, $request_headers);

    print match (true) {
        $event instanceof Seam\Resources\Event\AccessCodeCreated
            => "Created access code {$event->access_code_id}",
        $event::class === Seam\Resources\Event::class
            => "Unknown event type {$event->event_type}",
        default => "Received {$event->event_type}",
    };
} catch (Svix\Exception\WebhookVerificationException $error) {
    http_response_code(401);
} catch (Seam\InvalidWebhookPayloadError $error) {
    http_response_code(204);
}

if ($action_attempt->status === "pending") {
    // The action is still running.
}

use Seam\Resources\ActionAttempt\Status;
use Seam\Resources\Event\EventType;

if ($action_attempt->status === Status::PENDING->value) {
    // The action is still running.
}

$status = Status::tryFrom($action_attempt->status);
$event_type = EventType::tryFrom($event->event_type);

$seam = new Seam\Seam(endpoint: "https://example.com");

$seam = new Seam\Seam(
    guzzle_options: [
        "headers" => ["X-Custom-Header" => "value"],
        "proxy" => "http://localhost:8125",
    ]
);

$seam = new Seam\Seam(timeout: 60.0);

// Retry more times
$seam = new Seam\Seam(retries: 5);

// Turn retries off
$seam = new Seam\Seam(retries: 0);

$response = $seam->client->request("POST", "/devices/list", [
    "json" => (object) ["limit" => 10],
]);

$devices = Seam\Http\Body::decode($response)->devices;

$client = new GuzzleHttp\Client([
    "base_uri" => "https://connect.getseam.com",
    "headers" => ["authorization" => "Bearer " . $api_key],
]);

$seam = Seam\Seam::from_client($client);

$handler = GuzzleHttp\HandlerStack::create();

Seam\Http\ClientFactory::add_middleware($handler);

$client = new GuzzleHttp\Client([
    "base_uri" => "https://connect.getseam.com",
    "headers" => ["authorization" => "Bearer " . $api_key],
    "handler" => $handler,
    "http_errors" => false,
]);

$seam = Seam\Seam::from_client($client);

Seam\Http\ClientFactory::add_middleware($handler, retries: 0);

use Seam\StrictUrlSearchParamsSerializer;

$query = StrictUrlSearchParamsSerializer::serialize([
    "device_ids" => ["device1", "device2"],
]);

$response = file_get_contents(
    "https://connect.getseam.com/devices/list?{$query}",
    context: stream_context_create([
        "http" => ["header" => "Authorization: Bearer your-api-key"],
    ]),
);

use Seam\StrictUrlSearchParamsSerializer;
use Seam\UrlSearchParams;

$search_params = new UrlSearchParams();

StrictUrlSearchParamsSerializer::update($search_params, [
    "device_ids" => ["device1", "device2"],
]);

iterator_to_array($search_params);
// => [["device_ids", "device1"], ["device_ids", "device2"], ["_strict", "true"]]

(string) $search_params;
// => 'device_ids=device1&device_ids=device2&_strict=true'

use Seam\HttpInvalidInputError;

try {
    $seam->devices->list(device_ids: ["not-a-uuid"]);
} catch (HttpInvalidInputError $error) {
    print_r($error->getValidationErrorMessages("device_ids"));
}

foreach ($error->validation_errors as $validation_error) {
    printf(
        "%s: %s\n",
        $validation_error->parameter_name,
        implode(", ", $validation_error->error_messages),
    );
}

$ composer test -- tests/MyTest.php