PHP code example of nxi / factro-sdk

1. Go to this page and download the library: Download nxi/factro-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nxi / factro-sdk example snippets


use Nxi\Factro\FactroClientFactory;
use Nxi\Factro\FactroOptions;
use Nxi\Factro\Policy\RequestPolicy;
use Nxi\Factro\Resource\Task\Input\NewTask;
use Nxi\Factro\Resource\Task\Input\TaskChanges;
use Nxi\Factro\Resource\Task\TaskState;
use Nxi\Factro\Time\CalendarDate;

$client = FactroClientFactory::create(
    token: $token,                        // raw factro Core API token, sent without "Bearer"
    options: new FactroOptions(
        policy: RequestPolicy::withoutDeletes(),
        logger: $logger,
        limiterFactory: $limiterFactory,  // optional, default: in-memory sliding window 480/min
    ),
    httpClient: $baseClient,              // optional, default: HttpClient::create()
);

$task = $client->tasks()->get('c1c9...');
$open = array_filter($client->tasks()->listByProject($projectId), fn ($t) => $t->taskState->isOpen());

$created = $client->tasks()->create(new NewTask(
    title: 'Review offer',
    targetParentId: $packageId,
    endDate: CalendarDate::fromYmd('2026-09-30'),
));
$client->tasks()->update($created->id, new TaskChanges(description: '<p>Done</p>'));
$client->tasks()->setState($created->id, TaskState::CLOSED);

$client->tasks()->update($taskId, new TaskChanges(clear: ['executorId']));

$users = array_filter($client->users()->list(), fn (User $u) => $u->isActive && !$u->securityGroup->isGuest());
$tags = $client->users()->tagsForMany(array_column($users, 'id'), concurrency: 5);   // user id => list<EmployeeTag>
$all = $client->users()->employeeTags();                                            // GET /users/tags

$entry = $client->tasks()->addChecklistEntry($taskId, new NewChecklistEntry('Call customer'));
$client->tasks()->updateChecklistEntry($taskId, $entry->id, new ChecklistEntryChanges(checked: true));

$tag = $client->tasks()->createTaskTag('urgent');
$client->tasks()->addTag($taskId, $tag->id);

$client->tasks()->moveToPackage($taskId, $packageId);          // PUT /tasks/{id}/package
$client->packages()->shiftWithSuccessors($projectId, $packageId, daysDelta: 5);

$rights = $client->projects()->accessRights($projectId);
$rights->grantTeamWrite($teamId);
$reasons = $rights->readRights();                                // employee id => list<AccessRightReason>
foreach ($reasons[$employeeId] ?? [] as $r) {
    echo $r->reason, ' ', $r->packageId ?? $r->projectId ?? $r->taskId; // e.g. HasDirectPackageTeamWriteRight + packageId, teamId
}

$document = $client->tasks()->addDocument($taskId, DocumentUpload::fromFile('/tmp/offer.pdf', 'application/pdf'));
$all = $client->documents()->list();
$quota = $client->documents()->quota();                          // maxDiskSpace, usedDiskSpace, freeDiskSpace()

$client->appointments()->create(new NewAppointment($employeeId, $start, $end, subject: 'Kickoff'));
$team = $client->teams()->create(new NewTeam('Backend', '#3366ff'));
$client->teams()->addMember($team->id, $employeeId);
$contact = $client->contacts()->create(new NewContact('Erika', 'Muster', emailAddress: '[email protected]'));

$project = $client->templates()->applyStructureTemplate($templateId, new ApplyStructureTemplate(
    projectPropertyOverwrites: new ProjectPropertyOverwrites(title: 'Relaunch 2027'),
));

$records = $client->workRecords()->listByProject($projectId, CalendarDate::fromYmd('2026-01-01'), CalendarDate::fromYmd('2026-01-31'));
$start = $records[0]->startsAt(new DateTimeZone('Europe/Berlin'));   // uses the record's utcOffset when present

$client->workRecords()->create(new NewWorkRecord(
    startDate: CalendarDate::fromYmd('2026-09-01'),
    minutesWorked: 90,
    utcOffsetMinutes: 120,
    bookedOnReferenceId: $taskId,
    bookedOnReferenceType: WorkRecordReferenceType::TASK,
    startTime: '09:00',
    isBillable: true,
));

use Nxi\Factro\Testing\Fixtures;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\JsonMockResponse;

$mock = new MockHttpClient(fn ($method, $url) => JsonMockResponse::fromFile(Fixtures::path('task')), 'https://factro.test/api/core/');
$client = FactroClientFactory::create('test-token', new FactroOptions(baseUrl: 'https://factro.test/api/core', maxRetries: 0), $mock);

$row = TaskFactory::make(['title' => 'Custom', 'executorId' => null]);   // array, overrides merged with array_replace
$task = TaskFactory::dto(['taskState' => 'closed']);                     // Nxi\Factro\Resource\Task\Output\Task
$rows = ProjectFactory::many(3);                                         // distinct ids "<fixture-id>-<n>", increasing numbers