digitalstate/bpm-camunda-sdk
Laravel/PHP SDK for interacting with Camunda BPM/Platform REST APIs. Helps connect your app to process and task automation: start process instances, manage tasks, query history, and integrate workflows from PHP with simple, framework-friendly helpers.
Installation
composer require digitalstate/bpm-camunda-sdk
Ensure your Laravel project has guzzlehttp/guzzle (dependency for HTTP requests).
Basic Client Initialization
use DigitalState\Bpm\Camunda\Client;
$client = new Client('http://your-camunda-server:8080/engine-rest');
$client->setAuth('username', 'password'); // Basic Auth
First Use Case: Start a Process
$processDefinitionKey = 'your-process-key';
$variables = ['key' => 'value'];
$processInstance = $client->processInstance()->start($processDefinitionKey, $variables);
echo "Started process with ID: " . $processInstance->getId();
Key Documentation
processInstance(), task(), history()).Process Startup
$client->processInstance()->start('order-process', ['customerId' => 123]);
start() with a ProcessDefinitionKey and optional variables (serialized as JSON).Task Handling
$task = $client->task()->listTasks('user123');
$task->complete(['customVar' => 'value']);
listTasks($assignee) and complete them with complete($variables).Variable Management
$client->processInstance()->setVariables('procInstId', ['status' => 'approved']);
$variables = $client->processInstance()->getVariables('procInstId');
Event Listeners (Webhooks)
$client->eventSubscription()->subscribe(
'process-started',
'http://your-app.com/camunda/webhook',
['processDefinitionKey' => 'order-process']
);
Http facade to handle incoming webhook payloads.Service Provider Binding
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(Client::class, function () {
return new Client(config('camunda.url'))
->setAuth(config('camunda.username'), config('camunda.password'));
});
}
Queue Jobs for Async Operations
// app/Jobs/StartCamundaProcess.php
public function handle()
{
$client = app(Client::class);
$client->processInstance()->start('async-process', ['data' => $this->payload]);
}
Middleware for Auth/Validation
// app/Http/Middleware/ValidateCamundaRequest.php
public function handle($request, Closure $next)
{
if ($request->is('camunda/webhook')) {
$this->validateWebhook($request);
}
return $next($request);
}
Authentication
Client class to add OAuth:
$client->setAuth(new \GuzzleHttp\Auth\OAuth2\OAuth2Client());
.env (never hardcode).Variable Serialization
$variables = ['metadata' => ['nested' => ['key' => 'value']]];
json_encode() for complex data or ensure Client uses json_encode($data, JSON_THROW_ON_ERROR).Rate Limiting
use Symfony\Component\RateLimiter\RateLimiter;
$limiter = new RateLimiter(10, 'minute');
if (!$limiter->isAllowed()) {
sleep($limiter->waitTime());
}
Idempotency
businessKey may create duplicates. Use processDefinitionId + variables for uniqueness:
$client->processInstance()->start(
'order-process',
['businessKey' => 'order_123', 'customerId' => 123]
);
Enable Guzzle Middleware Add a logging middleware to inspect requests/responses:
$client->getHttpClient()->getEmitter()->attach(
new \GuzzleHttp\Middleware::tap(function ($request) {
\Log::debug('Camunda Request', ['url' => (string) $request->getUri()]);
})
);
Error Handling
try-catch for DigitalState\Bpm\Camunda\Exception\CamundaException:
try {
$client->task()->complete('taskId');
} catch (\Exception $e) {
\Log::error("Camunda task completion failed: " . $e->getMessage());
throw new \RuntimeException("Process failed", 0, $e);
}
Version Mismatches
Custom Endpoints
Extend the Client class to add non-standard endpoints:
class CustomCamundaClient extends Client
{
public function customEndpoint($path, $method = 'GET', $data = [])
{
return $this->request($method, $path, $data);
}
}
Laravel Events Trigger Laravel events on Camunda callbacks:
// In your webhook handler
event(new \App\Events\CamundaProcessStarted($payload));
Mocking for Tests
Use Laravel’s MockHttp to test SDK interactions:
$this->mock(Http::class)->shouldReceive('post')
->once()
->andReturn(Http::response(['id' => 'test'], 200));
How can I help you explore Laravel packages today?