Hey there, fellow code wranglers! Ready to supercharge your workflow management with Process Street's API? You're in the right place. We're going to walk through building a slick PHP integration that'll have you automating processes faster than you can say "efficiency boost". Let's dive in!
Before we start cooking, let's make sure we've got all our ingredients:
First things first, let's create a base API class. This'll be our Swiss Army knife for all Process Street operations.
class ProcessStreetAPI { private $apiKey; private $baseUrl = 'https://api.process.st/api/v1'; public function __construct($apiKey) { $this->apiKey = $apiKey; } // We'll add more methods here soon! }
Now, let's add some meat to our API class. We'll focus on three key operations:
public function getWorkflows() { return $this->makeRequest('GET', '/workflows'); }
public function createWorkflowRun($workflowId, $data) { return $this->makeRequest('POST', "/workflows/{$workflowId}/runs", $data); }
public function updateTaskStatus($runId, $taskId, $status) { $data = ['status' => $status]; return $this->makeRequest('PUT', "/runs/{$runId}/tasks/{$taskId}", $data); }
Let's not forget to play nice with the API. We'll wrap our requests in try-catch blocks and respect those rate limits:
private function makeRequest($method, $endpoint, $data = null) { $curl = curl_init($this->baseUrl . $endpoint); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_HTTPHEADER, [ 'Authorization: Bearer ' . $this->apiKey, 'Content-Type: application/json' ]); if ($data) { curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); try { $response = curl_exec($curl); if (curl_getinfo($curl, CURLINFO_HTTP_CODE) === 429) { // Handle rate limiting sleep(60); // Wait for a minute and try again return $this->makeRequest($method, $endpoint, $data); } return json_decode($response, true); } catch (Exception $e) { // Handle the error return ['error' => $e->getMessage()]; } finally { curl_close($curl); } }
Let's put it all together with a real-world example. We'll create a function that starts a new workflow run when a user signs up:
function onUserSignup($userId) { $api = new ProcessStreetAPI('your-api-key-here'); $workflowId = 'your-workflow-id'; $data = [ 'name' => "New User Onboarding - User {$userId}", 'variables' => [ 'userId' => $userId, 'signupDate' => date('Y-m-d H:i:s') ] ]; $result = $api->createWorkflowRun($workflowId, $data); if (isset($result['error'])) { // Handle the error error_log("Failed to create workflow run: " . $result['error']); } else { // Success! Maybe update your user record with the run ID? updateUserRecord($userId, $result['id']); } }
Don't forget to test your integration thoroughly. Here's a quick unit test to get you started:
function testWorkflowCreation() { $api = new ProcessStreetAPI('your-test-api-key'); $result = $api->createWorkflowRun('test-workflow-id', ['name' => 'Test Run']); assert(isset($result['id']), 'Workflow run creation failed'); }
And there you have it! You've just built a robust Process Street API integration in PHP. With this foundation, you can automate workflows, track tasks, and supercharge your processes. Remember, the API is your oyster - there's so much more you can do with it.
Happy coding, and may your workflows be ever efficient!
For the full example code and more advanced usage, check out our GitHub repository.