Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ All notable changes to `fopost/sdk` are documented here.

### Added

- `$client->designs()` over `/designs` (`posts` scope): `list()`, `create()`, `get()`, `update()`,
`delete()`, `versions()`, `restoreVersion()`, `duplicate()`, `export()`, `bulk()`, `posts()`,
`fromPost()` and `textToSlides()` (also `ai` scope), with `Design`, `DesignVersion`,
`DesignRevision`, `DesignExport`, `DesignExportFile`, `DesignPost` and `SlideOutline`. The
design document is passed through as a plain array.
- `$client->fonts()` over `/fonts` (`posts` scope): `list()` returns a `FontCatalog` of
`LibraryFont` and `UploadedFont`, `upload()` sends one font file as multipart from a path or
bytes, and `delete()` removes an uploaded font.
- `$client->videoEdits()` over `/video-edits` (`posts` scope, plus `ai` for AI operations):
`ops()`, `list()`, `create()` with `[{op, params}]` operations, and `get()`, as `VideoEditOp`
and `VideoEdit`.
- `$client->media()->generateAltText($mediaId)` writes and stores alt text for a library image
(also `ai` scope), returning a `GeneratedAltText`.
- `validateBulkImport()` and `commitBulkImport()` take an optional `$accounts` list (account
ids or platform names), sent comma-separated and applied to every CSV row whose own
`accounts` cell is empty.
Expand Down
7 changes: 6 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ throws through `ErrorFactory` or returns the decoded body → the resource calls
**Resources wired today:** `posts`, `queue`, `accounts`, `connectSessions`, `accountGroups`,
`workspaces`, `labels`, `ai`, `automations`, `inbox`, `contacts`, `broadcasts`, `sequences`,
`knowledge`, `ads`, `blogs`, `media`, `validate`, `webhooks`, `whatsapp`, `analytics`, `googleBusiness`,
`activity`, `logs`. Anything else goes through the escape hatch `Client::request()` until a
`activity`, `logs`, `designs`, `fonts`, `videoEdits`. Anything else goes through the escape hatch `Client::request()` until a
resource is added. `scripts/sdk-gaps.mjs` in `fopost-api-collections` lists what is still unwrapped.

- `inbox` (scope `inbox`) covers `/inbox`, `/inbox/posts`, `/inbox/conversations`, `unread-count`,
Expand Down Expand Up @@ -139,6 +139,11 @@ resource is added. `scripts/sdk-gaps.mjs` in `fopost-api-collections` lists what
retry; a non-2xx throws and `complete` is never called). Bodies are camelCase. There is no size
constant in the SDK: the ceiling is whatever `limits()` answers, and an oversize file is a `413`
`file_too_large` that falls through to `ApiException` with `max_file_bytes` on `->body`.
- `designs`, `fonts` and `videoEdits` (scope `posts`; `textToSlides()`, AI video operations and
`media()->generateAltText()` also need `ai`). Bodies and queries are snake_case. The design document
is a plain array, so an empty JSON object round-trips as `[]` unless the caller passes a `stdClass`.
`GET /fonts/{id}/file` is a binary stream and is not wrapped. There is no poll helper for video
edit jobs: callers loop on `get()`.
- `posts()->create()` and `update()` take `files`. Every file at or under
`limits()->multipartMaxFileBytes` (100 MB when the call fails) goes in one multipart request through
`HttpClient::requestMultipart()` (`post` JSON field + `files` parts); otherwise each file goes
Expand Down
83 changes: 83 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,89 @@ $client->media()->delete($page->items[0]->id);
A file over the limit is a `413` with `error: file_too_large`; it arrives as an `ApiException` whose
`getBody()['max_file_bytes']` says what would have fit, and `getBody()['upgrade_url']` where to raise it.

Write alt text for an image with AI. It is stored on the asset, replacing any existing text, and
uses AI credits, so an API key also needs the `ai` scope:

```php
$alt = $client->media()->generateAltText($assetId);
echo $alt->altText, ' ', implode(', ', $alt->tags), PHP_EOL;
```

## Designs

Stories, posts, carousels, thumbnails, cards and collages. Needs the `posts` scope. The design
document is a plain array; pass a `stdClass` where an empty JSON object must stay `{}`.

```php
$design = $client->designs()->create($workspaceId, 'story', name: 'Launch story');
$designs = $client->designs()->list($workspaceId, kind: 'carousel', limit: 20);

$design = $client->designs()->get($design->id);
$document = $design->document;
// ...edit $document...
$revision = $client->designs()->update($design->id, document: $document, createVersion: true);
echo $revision->version?->version, PHP_EOL;

$versions = $client->designs()->versions($design->id);
$client->designs()->restoreVersion($design->id, $versions[1]->id);

// Render into the media library: png, jpg, pdf, mp4 or gif.
$export = $client->designs()->export($design->id, 'png');
echo $export->files[0]->mediaId, PHP_EOL;

// One copy per row, each value filling the layer of the same name.
$copies = $client->designs()->bulk($design->id, [
['name' => 'Ana', 'headline' => 'Welcome, Ana'],
['name' => 'Ben', 'headline' => 'Welcome, Ben'],
], nameColumn: 'name');

$copy = $client->designs()->duplicate($design->id, 'Launch story (copy)');
$posts = $client->designs()->posts($design->id); // posts that used its exports, with metrics
$client->designs()->delete($copy->id);

// From text or a post. textToSlides() uses AI credits and needs the `ai` scope.
$quote = $client->designs()->fromPost($workspaceId, postId: $postId, style: 'quote');
$outline = $client->designs()->textToSlides($workspaceId, text: $longText, maxSlides: 6);
echo $outline->cover['title'], ' ', count($outline->slides), PHP_EOL;
```

## Fonts

The built-in font library, plus fonts uploaded to a workspace. Needs the `posts` scope.

```php
$fonts = $client->fonts()->list($workspaceId);
echo $fonts->library[0]->family, ' ', count($fonts->uploaded), PHP_EOL;

// A path, or ['filename' => ..., 'contents' => ..., 'mimeType' => ...] for bytes in memory.
$font = $client->fonts()->upload($workspaceId, 'BrandSans-Bold.woff2', family: 'Brand Sans', weight: 700);
$client->fonts()->delete($font->id);
```

## Video edits

Edit a library video as a background job, then poll it. Needs the `posts` scope; operations whose
`usesAi` is true (subtitles, translation, clips, voiceover) also need `ai`.

```php
foreach ($client->videoEdits()->ops() as $op) {
echo $op->name, ' ', $op->available ? 'available' : 'off', PHP_EOL;
}

$job = $client->videoEdits()->create($videoId, [
['op' => 'trim', 'params' => ['startSec' => 0, 'endSec' => 30]],
['op' => 'denoise'],
]);

while (!in_array($job->status, ['succeeded', 'failed'], true)) {
sleep(3);
$job = $client->videoEdits()->get($job->id);
}
echo $job->resultMediaId ?? $job->error, PHP_EOL;

$recent = $client->videoEdits()->list(mediaId: $videoId);
```

## Validate

Check a draft before you schedule it. Nothing is stored; needs the `posts` scope.
Expand Down
26 changes: 26 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Fopost\Sdk\Resource\BroadcastsResource;
use Fopost\Sdk\Resource\ConnectSessionsResource;
use Fopost\Sdk\Resource\ContactsResource;
use Fopost\Sdk\Resource\DesignsResource;
use Fopost\Sdk\Resource\FontsResource;
use Fopost\Sdk\Resource\GoogleBusinessResource;
use Fopost\Sdk\Resource\BlogsResource;
use Fopost\Sdk\Resource\InboxResource;
Expand All @@ -27,6 +29,7 @@
use Fopost\Sdk\Resource\QueueResource;
use Fopost\Sdk\Resource\SequencesResource;
use Fopost\Sdk\Resource\ValidateResource;
use Fopost\Sdk\Resource\VideoEditsResource;
use Fopost\Sdk\Resource\WebhooksResource;
use Fopost\Sdk\Resource\WhatsappResource;
use Fopost\Sdk\Resource\WorkspacesResource;
Expand Down Expand Up @@ -73,6 +76,9 @@ final class Client
private readonly WhatsappResource $whatsapp;
private readonly AnalyticsResource $analytics;
private readonly GoogleBusinessResource $googleBusiness;
private readonly DesignsResource $designs;
private readonly FontsResource $fonts;
private readonly VideoEditsResource $videoEdits;

public function __construct(
?string $apiKey = null,
Expand Down Expand Up @@ -114,6 +120,9 @@ public function __construct(
$this->whatsapp = new WhatsappResource($this->http);
$this->analytics = new AnalyticsResource($this->http);
$this->googleBusiness = new GoogleBusinessResource($this->http);
$this->designs = new DesignsResource($this->http);
$this->fonts = new FontsResource($this->http);
$this->videoEdits = new VideoEditsResource($this->http);
}

public function posts(): PostsResource
Expand Down Expand Up @@ -243,6 +252,23 @@ public function blogs(): BlogsResource
return $this->blogs;
}

/** Designs: create, edit, version, export and bulk-fill. */
public function designs(): DesignsResource
{
return $this->designs;
}

public function fonts(): FontsResource
{
return $this->fonts;
}

/** Video edit jobs: trim, cut, subtitles, clips and the rest. */
public function videoEdits(): VideoEditsResource
{
return $this->videoEdits;
}

public function baseUrl(): string
{
return $this->http->baseUrl();
Expand Down
48 changes: 48 additions & 0 deletions src/Model/Design.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Fopost\Sdk\Model;

use DateTimeImmutable;

/** A design: a story, post, carousel, thumbnail, card or collage. */
final class Design extends Model
{
/** @param array<string, mixed>|null $document */
private function __construct(
array $raw,
public readonly string $id,
public readonly string $name,
public readonly string $kind,
public readonly ?int $width,
public readonly ?int $height,
/** The design document as a plain map; null on list rows, which leave it out. */
public readonly ?array $document,
public readonly ?string $thumbnailMediaId,
public readonly ?string $thumbnailUrl,
public readonly ?DateTimeImmutable $createdAt,
public readonly ?DateTimeImmutable $updatedAt,
) {
parent::__construct($raw);
}

public static function fromArray(mixed $data): static
{
$data = is_array($data) ? $data : [];

return new self(
$data,
self::requiredStr($data, 'id'),
self::requiredStr($data, 'name'),
self::requiredStr($data, 'kind'),
self::int($data, 'width'),
self::int($data, 'height'),
self::nested($data, 'document'),
self::str($data, 'thumbnail_media_id'),
self::str($data, 'thumbnail_url'),
self::date($data, 'created_at'),
self::date($data, 'updated_at'),
);
}
}
29 changes: 29 additions & 0 deletions src/Model/DesignExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Fopost\Sdk\Model;

/** The files an export rendered into the media library. */
final class DesignExport extends Model
{
/** @param array<int, DesignExportFile> $files */
private function __construct(
array $raw,
public readonly string $format,
public readonly array $files,
) {
parent::__construct($raw);
}

public static function fromArray(mixed $data): static
{
$data = is_array($data) ? $data : [];

return new self(
$data,
self::requiredStr($data, 'format'),
DesignExportFile::listFrom(self::seq($data, 'files')),
);
}
}
32 changes: 32 additions & 0 deletions src/Model/DesignExportFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Fopost\Sdk\Model;

/** One rendered page of an export, stored in the media library. */
final class DesignExportFile extends Model
{
private function __construct(
array $raw,
public readonly string $mediaId,
public readonly ?int $pageIndex,
public readonly ?string $mimeType,
public readonly ?string $previewUrl,
) {
parent::__construct($raw);
}

public static function fromArray(mixed $data): static
{
$data = is_array($data) ? $data : [];

return new self(
$data,
self::requiredStr($data, 'media_id'),
self::int($data, 'page_index'),
self::str($data, 'mime_type'),
self::str($data, 'preview_url'),
);
}
}
45 changes: 45 additions & 0 deletions src/Model/DesignPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Fopost\Sdk\Model;

use DateTimeImmutable;

/** A post that used media exported from a design, with its metrics. */
final class DesignPost extends Model
{
/** @param array<int, string> $platforms */
private function __construct(
array $raw,
public readonly string $id,
public readonly string $status,
public readonly ?string $title,
public readonly ?string $text,
public readonly ?DateTimeImmutable $scheduleAt,
public readonly ?DateTimeImmutable $publishedAt,
public readonly array $platforms,
public readonly ?int $engagement,
public readonly ?int $views,
) {
parent::__construct($raw);
}

public static function fromArray(mixed $data): static
{
$data = is_array($data) ? $data : [];

return new self(
$data,
self::requiredStr($data, 'id'),
self::requiredStr($data, 'status'),
self::str($data, 'title'),
self::str($data, 'text'),
self::date($data, 'schedule_at'),
self::date($data, 'published_at'),
array_values(array_filter(self::seq($data, 'platforms'), 'is_string')),
self::int($data, 'engagement'),
self::int($data, 'views'),
);
}
}
29 changes: 29 additions & 0 deletions src/Model/DesignRevision.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Fopost\Sdk\Model;

/** The design after an update or restore, plus the version that call saved, if any. */
final class DesignRevision extends Model
{
private function __construct(
array $raw,
public readonly Design $design,
public readonly ?DesignVersion $version,
) {
parent::__construct($raw);
}

public static function fromArray(mixed $data): static
{
$data = is_array($data) ? $data : [];
$version = self::nested($data, 'version');

return new self(
$data,
Design::fromArray(self::map($data, 'data')),
$version !== null ? DesignVersion::fromArray($version) : null,
);
}
}
Loading
Loading