workos/workos-php
Official PHP SDK for WorkOS. Integrate enterprise features like Single Sign-On, Directory Sync, Admin Portal, Audit Logs, and user management into your Laravel or PHP app with a simple, typed API client and examples for common auth workflows.
Installation
composer require workos/workos-php
Ensure your project meets PHP 8.1+ requirements.
First Use Case: Authentication Initialize the client with your API key:
use WorkOS\WorkOS;
$client = new WorkOS('your_api_key_here');
Verify connectivity with a simple API call:
$response = $client->get('/directory');
$response->json(); // Returns directory data
Where to Look First
src/WorkOS.php for core client logic and request handlingDirectory Management Fetch and update user directories:
// Fetch all users
$users = $client->get('/directory/users')->json();
// Create a new user
$newUser = $client->post('/directory/users', [
'email' => 'user@example.com',
'first_name' => 'John',
'last_name' => 'Doe',
])->json();
SSO Integration Generate SSO links for users:
$ssoLink = $client->post('/sso/link', [
'user_id' => 'user_123',
'redirect_to' => 'https://app.example.com/dashboard',
])->json();
Event Webhooks Listen for events (e.g., user creation):
$webhook = $client->post('/webhooks', [
'target_url' => 'https://your-app.com/webhook',
'events' => ['user.created'],
])->json();
Laravel Service Provider Bind the client to the container for dependency injection:
// config/services.php
'workos' => [
'api_key' => env('WORKOS_API_KEY'),
];
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(WorkOS::class, function ($app) {
return new WorkOS(config('services.workos.api_key'));
});
}
API Rate Limiting Handle rate limits gracefully:
try {
$response = $client->get('/directory');
} catch (WorkOS\Exception\RateLimitException $e) {
// Retry logic or notify admin
}
Pagination
Use the next_page URL from responses for paginated endpoints:
$response = $client->get('/directory/users');
$users = $response->json();
$nextPage = $response->headers->get('link') ?? null;
API Key Exposure
.env:
WORKOS_API_KEY=your_api_key_here
Idempotency
$client->post('/directory/users', [
'email' => 'user@example.com',
'idempotency_key' => 'unique_key_here',
]);
Webhook Verification
workos-signature header:
use WorkOS\WorkOS;
$payload = file_get_contents('php://input');
$signature = $_SERVER['HTTP_WORKOS_SIGNATURE'] ?? '';
$isValid = WorkOS::verifyWebhook($payload, $signature, 'your_api_key');
Enable Debug Mode Toggle debug mode for verbose error messages:
$client = new WorkOS('api_key', [
'debug' => true,
]);
Logging Requests Use Laravel's logging to track API calls:
$client->get('/directory')->then(function ($response) {
\Log::debug('WorkOS API Response', ['response' => $response->json()]);
});
Custom Requests Extend the client for unsupported endpoints:
$client->customRequest('GET', '/custom/endpoint', [], [
'custom_header' => 'value',
]);
Middleware Add request/response middleware:
$client->addMiddleware(function ($request) {
$request->headers->set('X-Custom-Header', 'value');
});
Mocking for Tests Use Laravel's HTTP mocking or create a test double:
$mockClient = Mockery::mock(WorkOS::class);
$mockClient->shouldReceive('get')->andReturn(new WorkOS\Response(200, [], json_encode(['test' => true])));
How can I help you explore Laravel packages today?