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.
Http facade. Supports dependency injection (DI) natively, fitting Laravel’s IoC container.Event::dispatch()), enabling reactive workflows (e.g., auto-provisioning users).$client = new WorkOS\Client($apiKey);
$user = $client->users()->create(['email' => 'user@example.com']);
Can be wrapped in a Laravel service class for further abstraction..env), aligning with Laravel’s 12-factor config practices.queue:work) for async webhook processing, but the SDK provides helpers for validation/signature verification.WorkOS\V202603Client).retry helper or a custom decorator (e.g., WorkOS\RetryableClient) is needed.workos/sso vs. workos/users).Observers or Model Events.encryption config).Http facade or Guzzle directly.$this->app->singleton(WorkOS\Client::class, function ($app) {
return new WorkOS\Client(config('services.workos.api_key'));
});
UserProvisioned).socialiteproviders/workos (if available) or extend Socialite’s base provider.users()->create()).Http facade first, then refactor to SDK.// Before (raw HTTP)
$response = Http::withHeaders(['Authorization' => 'Bearer '.$apiKey])
->post('https://api.workos.com/users', ['email' => 'test@example.com']);
// After (SDK)
$user = app(WorkOS\Client::class)->users()->create(['email' => 'test@example.com']);
sso methods.routes/web.php):
Route::post('/workos/webhooks', [WorkOSWebhookHandler::class, 'handle']);
queue:work to process bulk operations.WorkOSAuthenticate middleware).php-version in composer.json matches (e.g., ^8.1).migrations for schema changes (e.g., adding workos_user_id to users table)..env:
WORKOS_API_KEY=your_key_here
composer require workos/workos-php
AppServiceProvider:
public function register() {
$this->app->singleton(WorkOS\Client::class, fn() => new WorkOS\Client(config('services.workos.api_key')));
}
config or a package like spatie/laravel-config-array to toggle WorkOS features.composer why-not workos/workos-php to check constraints.Log facade:
Log::debug('WorkOS user created', ['user_id' => $user->id]);
WORKOS_INTEGRATION.md in the repo detailing:
Exception hierarchy:
try {
$user = $client->users()->get($id);
} catch (WorkOS\Exception\NotFoundException $e) {
abort(404, 'User not found in WorkOS');
}
retry helper or a custom decorator:
use Illuminate\Support\Facades\Http;
Http::retry(3, 100)->post(...);
queue:work with a worker pool (e.g., supervisor) to handle concurrent webhook payloads.DB::transaction() for atomicity.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| WorkOS API downtime | Auth failures, user provision |
How can I help you explore Laravel packages today?