Quickstart Examples

Authentication Quickstart

To use the SDK, you will require an Access Token. As shown in the example below, you can obtain a new Access Token by using your email and password if you do not have SSO enabled for your team. You can then save that token for future use. For SSO and other options, see the Authentication section.

<?php

require_once(__DIR__ . '/vendor/autoload.php');

use Catalytic\SDK\CatalyticClient;

// This will load credentials from the CATALYTIC_CREDENTIALS env var 
// if set, or ~/.catalytic/credentials/default
$catalytic = new CatalyticClient();

// Otherwise you can pass your token in directly
$catalytic = new CatalyticClient('mytoken');

// you are now authenticated and ready to use the Catalytic SDK

Quickstart Examples

Example 1: Start a Workflow

Here is a simple example of finding and and starting a Workflow with a couple inputs. The first time you run it it will import an SDK Example Workflow into your team.

To use this example:

  1. Create a new PHP file
  2. Follow the instructions in Getting Started & Installation to add the catalytic/sdk Composer package to your project and set up your default credentials.
  3. Replace program.php with the following
<?php

require_once(__DIR__ . '/vendor/autoload.php');

use Catalytic\SDK\CatalyticClient;
use Catalytic\SDK\Search\Where;

// create and initialize the Catalytic SDK Client
$catalytic = new CatalyticClient();

// Find workflows matching "Sdk Example Workflow"
$where = (new Where)->text()->matches("Sdk Example Workflow");

// `results` is a results page containing `workflows` and 
// a `nextPageToken` you can use to page through results.
$results = $catalytic->workflows()->find($where);

// Get the workflows from the $results
$workflows = $results->getWorkflows();

// If Workflows were found, get the first one
if ($workflows) {
	$workflow = $workflows[0]; 		
}
// If no Workflows were found, let's import one
else {
  $workflow = $catalytic->workflows()->import(new SplFileObject("https://push.bot/sdk-example"));
}

// Now that we've got our Workflow, let's start an Instance with inputs it expects
$instance = $catalytic->instances()->start(
  $workflow->getId(),
  'SDK Example',
  'My first SDK example',
  array('Age' => 42, 'Name' => 'Alice')
);

$instanceUrl = "https://$catalytic->accessToken->getDomain()/runs/$instance->getId()}";
echo "Workflow started successfully. View your Workflow at $instanceUrl";

Example 2: Complete a Step with Data Tables and Files

Here is a more advanced example showing how you can upload and download files from an Instance, complete a task in an Instance, and upload and download data tables. The Workflow used in this example will email you the results when then example completes.

To use this example:

  1. Create a new PHP file
  2. Follow the instructions in Getting Started & Installation to add the catalytic/sdk Composer package to your project and set up your default credentials.
  3. Replace program.php with the following
<?php

require_once(__DIR__ . '/vendor/autoload.php');

use Catalytic\SDK\CatalyticClient;
use Catalytic\SDK\Search\Where;

// Update this to your email before running this sample
$yourEmail = "[email protected]";
      
// Create and initialize the Catalytic SDK Client
$catalytic = new CatalyticClient();
            
// Find workflows matching "Sdk Example Workflow"
$where = (new Where)->text()->matches("Sdk Example Workflow");

// `results` is a results page containing `workflows` and 
// a `nextPageToken` you can use to page through results.
$results = $catalytic->workflows()->find($where);

// Get the workflows from the $results
$workflows = $results->getWorkflows();

// If Workflows were found, get the first one
if ($workflows) {
	$workflow = $workflows[0]; 		
}
// If no Workflows were found, let's import one
else {
  $workflow = $catalytic->workflows()->import(new SplFileObject("https://push.bot/sdk-example"));
}

// Now that we've got our Workflow, let's start an Instance with inputs it expects
$instance = $catalytic->instances()->start(
  $workflow->getId(),
  'SDK Example',
  'My first SDK example',
  array('Age' => 42, 'Name' => 'Alice')
);

// Get the steps
$steps = $catalytic->instances->getSteps($instance->getId())->getSteps();

// Get the step we want to complete
$uploadStep = array_filter($steps, function ($value, $key) {
   return $key === 'name' && $value === "Upload Updated Spreadsheet and Set Email";
})[0];
            
// Download the Data Table from the "Table" field as a CSV 
$tableId = $instance->getFields["Table"]->getValue();
$csvFile = $catalytic->dataTables()->download($tableId, 'csv');

// Do our "business logic" to transform the csv data
// As a simple example, we replace "Foo" with "Bar"
$updatedCsvContent = csvFile->OpenText()->ReadToEnd()->Replace("Foo", "Bar");

// Create the output csv file locally
$updatedCsvFile = new SplFileObject(sys_get_temp_dir(), 'w');

$updatedCsvFile.fwrite($updatedCsvContent);

// To complete the step, we will set both a text and a file field
$stepRequest = $step
		->SetInput("Updated CSV", updatedCsvFile)
		->SetInput("Email Address", YourEmail);

$catalytic->instances()->completeStep();

echo "You should have an email waiting for you at $yourEmail now ", 
		 "with the updated CSV converted to an Excel attachment");