From ab22ab12ef7c6adb3f92304beb99eed4b75ec02b Mon Sep 17 00:00:00 2001 From: Ali Hesari Date: Fri, 20 Nov 2026 23:05:26 +0100 Subject: [PATCH 1/5] feat(designs): add the designs resource List, create, get, update, delete, versions, restore, duplicate, export, bulk, posts, text-to-slides and from-post over /v1/designs. --- src/Client.php | 9 + src/Model/Design.php | 48 +++++ src/Model/DesignExport.php | 29 +++ src/Model/DesignExportFile.php | 32 ++++ src/Model/DesignPost.php | 45 +++++ src/Model/DesignRevision.php | 29 +++ src/Model/DesignVersion.php | 35 ++++ src/Model/SlideOutline.php | 38 ++++ src/Resource/DesignsResource.php | 210 ++++++++++++++++++++ tests/DesignsTest.php | 317 +++++++++++++++++++++++++++++++ 10 files changed, 792 insertions(+) create mode 100644 src/Model/Design.php create mode 100644 src/Model/DesignExport.php create mode 100644 src/Model/DesignExportFile.php create mode 100644 src/Model/DesignPost.php create mode 100644 src/Model/DesignRevision.php create mode 100644 src/Model/DesignVersion.php create mode 100644 src/Model/SlideOutline.php create mode 100644 src/Resource/DesignsResource.php create mode 100644 tests/DesignsTest.php diff --git a/src/Client.php b/src/Client.php index 9fff119..470c5f2 100644 --- a/src/Client.php +++ b/src/Client.php @@ -15,6 +15,7 @@ use Fopost\Sdk\Resource\BroadcastsResource; use Fopost\Sdk\Resource\ConnectSessionsResource; use Fopost\Sdk\Resource\ContactsResource; +use Fopost\Sdk\Resource\DesignsResource; use Fopost\Sdk\Resource\GoogleBusinessResource; use Fopost\Sdk\Resource\BlogsResource; use Fopost\Sdk\Resource\InboxResource; @@ -73,6 +74,7 @@ final class Client private readonly WhatsappResource $whatsapp; private readonly AnalyticsResource $analytics; private readonly GoogleBusinessResource $googleBusiness; + private readonly DesignsResource $designs; public function __construct( ?string $apiKey = null, @@ -114,6 +116,7 @@ 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); } public function posts(): PostsResource @@ -243,6 +246,12 @@ public function blogs(): BlogsResource return $this->blogs; } + /** Designs: create, edit, version, export and bulk-fill. */ + public function designs(): DesignsResource + { + return $this->designs; + } + public function baseUrl(): string { return $this->http->baseUrl(); diff --git a/src/Model/Design.php b/src/Model/Design.php new file mode 100644 index 0000000..88b2e07 --- /dev/null +++ b/src/Model/Design.php @@ -0,0 +1,48 @@ +|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'), + ); + } +} diff --git a/src/Model/DesignExport.php b/src/Model/DesignExport.php new file mode 100644 index 0000000..bb9add6 --- /dev/null +++ b/src/Model/DesignExport.php @@ -0,0 +1,29 @@ + $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')), + ); + } +} diff --git a/src/Model/DesignExportFile.php b/src/Model/DesignExportFile.php new file mode 100644 index 0000000..a6aa908 --- /dev/null +++ b/src/Model/DesignExportFile.php @@ -0,0 +1,32 @@ + $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'), + ); + } +} diff --git a/src/Model/DesignRevision.php b/src/Model/DesignRevision.php new file mode 100644 index 0000000..0c0bf36 --- /dev/null +++ b/src/Model/DesignRevision.php @@ -0,0 +1,29 @@ +|null $document */ + private function __construct( + array $raw, + public readonly string $id, + public readonly int $version, + public readonly ?array $document, + public readonly ?DateTimeImmutable $createdAt, + ) { + parent::__construct($raw); + } + + public static function fromArray(mixed $data): static + { + $data = is_array($data) ? $data : []; + + return new self( + $data, + self::requiredStr($data, 'id'), + self::int($data, 'version') ?? 0, + self::nested($data, 'document'), + self::date($data, 'created_at'), + ); + } +} diff --git a/src/Model/SlideOutline.php b/src/Model/SlideOutline.php new file mode 100644 index 0000000..8880fec --- /dev/null +++ b/src/Model/SlideOutline.php @@ -0,0 +1,38 @@ + $slides + * @param array{heading?: string, body?: string} $cta + * @param array $brand colors, fonts and logo_media_id + */ + private function __construct( + array $raw, + public readonly array $cover, + public readonly array $slides, + public readonly array $cta, + public readonly array $brand, + ) { + parent::__construct($raw); + } + + public static function fromArray(mixed $data): static + { + $data = is_array($data) ? $data : []; + + return new self( + $data, + self::map($data, 'cover'), + array_values(array_filter(self::seq($data, 'slides'), 'is_array')), + self::map($data, 'cta'), + self::map($data, 'brand'), + ); + } +} diff --git a/src/Resource/DesignsResource.php b/src/Resource/DesignsResource.php new file mode 100644 index 0000000..53fb27c --- /dev/null +++ b/src/Resource/DesignsResource.php @@ -0,0 +1,210 @@ +designs(): stories, posts, carousels, thumbnails, cards and collages. Needs the + * `posts` scope; `textToSlides()` spends AI and also needs `ai`. + * + * The design document is a plain map. Pass a `\stdClass` (or decode with objects) where an + * empty JSON object must stay `{}` rather than `[]`. + */ +final class DesignsResource extends Resource +{ + /** + * Designs in a workspace, newest first, without their documents. `$kind` is story, post, + * carousel, thumbnail, card or collage. + * + * @return array + */ + public function list(string $workspaceId, ?string $kind = null, ?int $limit = null, ?int $offset = null): array + { + $query = ['workspace_id' => $workspaceId, 'kind' => $kind, 'limit' => $limit, 'offset' => $offset]; + + return Design::listFrom(self::unwrap($this->http->get('/designs', $query))); + } + + /** @param array|object|null $document Omitted, the kind's blank document. */ + public function create( + string $workspaceId, + string $kind, + ?string $name = null, + array|object|null $document = null, + ?string $brandVoiceId = null, + ): Design { + $body = self::compact([ + 'workspace_id' => $workspaceId, + 'name' => $name, + 'kind' => $kind, + 'document' => $document, + 'brand_voice_id' => $brandVoiceId, + ]); + + return Design::fromArray(self::unwrap($this->http->post('/designs', $body))); + } + + public function get(string $designId): Design + { + return Design::fromArray(self::unwrap($this->http->get("/designs/{$designId}"))); + } + + /** + * Partial update: only the fields you pass are sent. `$createVersion` saves a version + * of the result, returned on `->version`. + * + * @param array|object|null $document + */ + public function update( + string $designId, + ?string $name = null, + ?string $kind = null, + array|object|null $document = null, + ?bool $createVersion = null, + ): DesignRevision { + $body = self::compact([ + 'name' => $name, + 'kind' => $kind, + 'document' => $document, + 'create_version' => $createVersion, + ]); + + return DesignRevision::fromArray( + $this->http->patch("/designs/{$designId}", $body === [] ? (object) [] : $body), + ); + } + + public function delete(string $designId): void + { + $this->http->delete("/designs/{$designId}"); + } + + /** + * Saved versions, newest first. `$withDocument` includes each version's document. + * + * @return array + */ + public function versions(string $designId, bool $withDocument = false): array + { + $query = $withDocument ? ['include' => 'document'] : null; + + return DesignVersion::listFrom(self::unwrap($this->http->get("/designs/{$designId}/versions", $query))); + } + + /** Put a saved version's document back on the design. */ + public function restoreVersion(string $designId, string $versionId): DesignRevision + { + return DesignRevision::fromArray( + $this->http->post("/designs/{$designId}/versions/{$versionId}/restore"), + ); + } + + public function duplicate(string $designId, ?string $name = null): Design + { + $body = $name === null ? (object) [] : ['name' => $name]; + + return Design::fromArray(self::unwrap($this->http->post("/designs/{$designId}/duplicate", $body))); + } + + /** + * Render pages into the media library. `$format` is png, jpg, pdf, mp4 or gif; + * `$durationSec` (5, 10 or 15) applies to mp4 and gif. + * + * @param array|null $pageIndexes Omitted, every page. + */ + public function export( + string $designId, + string $format, + ?array $pageIndexes = null, + ?int $durationSec = null, + ): DesignExport { + $body = self::compact([ + 'format' => $format, + 'page_indexes' => $pageIndexes, + 'duration_sec' => $durationSec, + ]); + + return DesignExport::fromArray(self::unwrap($this->http->post("/designs/{$designId}/export", $body))); + } + + /** + * One copy of the design per row, each row's values filling the layers of the same name. + * `$nameColumn` names the column that titles each copy. + * + * @param array> $rows + * @return array + */ + public function bulk(string $designId, array $rows, ?string $nameColumn = null): array + { + $body = self::compact(['rows' => $rows, 'name_column' => $nameColumn]); + + return Design::listFrom(self::unwrap($this->http->post("/designs/{$designId}/bulk", $body))); + } + + /** + * Posts that used this design's exported media, with their metrics. + * + * @return array + */ + public function posts(string $designId): array + { + return DesignPost::listFrom(self::unwrap($this->http->get("/designs/{$designId}/posts"))); + } + + /** Split long text, or a post's text, into carousel slides. Uses AI credits. */ + public function textToSlides( + string $workspaceId, + ?string $text = null, + ?string $postId = null, + ?int $maxSlides = null, + ?string $brandVoiceId = null, + ): SlideOutline { + $body = self::compact([ + 'workspace_id' => $workspaceId, + 'text' => $text, + 'post_id' => $postId, + 'max_slides' => $maxSlides, + 'brand_voice_id' => $brandVoiceId, + ]); + + return SlideOutline::fromArray(self::unwrap($this->http->post('/designs/text-to-slides', $body))); + } + + /** + * A design built from text or a post. `$style` is quote, post or plain (default quote); + * `$carousel` builds a multi-page carousel. Needs `$text` or `$postId`. + */ + public function fromPost( + string $workspaceId, + ?string $text = null, + ?string $postId = null, + ?string $style = null, + ?string $preset = null, + ?string $accountId = null, + ?bool $carousel = null, + ?string $name = null, + ?string $brandVoiceId = null, + ): Design { + $body = self::compact([ + 'workspace_id' => $workspaceId, + 'text' => $text, + 'post_id' => $postId, + 'style' => $style, + 'preset' => $preset, + 'account_id' => $accountId, + 'carousel' => $carousel, + 'name' => $name, + 'brand_voice_id' => $brandVoiceId, + ]); + + return Design::fromArray(self::unwrap($this->http->post('/designs/from-post', $body))); + } +} diff --git a/tests/DesignsTest.php b/tests/DesignsTest.php new file mode 100644 index 0000000..bf7f715 --- /dev/null +++ b/tests/DesignsTest.php @@ -0,0 +1,317 @@ + 'd_1', + 'name' => 'Launch story', + 'kind' => 'story', + 'width' => 1080, + 'height' => 1920, + 'document' => ['pages' => [['layers' => []]]], + 'thumbnail_media_id' => 'm_9', + 'thumbnail_url' => 'https://api.fopost.com/v1/media/m_9/file', + 'created_at' => '2026-11-20T10:00:00Z', + 'updated_at' => '2026-11-20T11:00:00Z', + ]; + + private const VERSION = ['id' => 'v_1', 'version' => 3, 'created_at' => '2026-11-20T11:00:00Z']; + + public function testListSendsTheFiltersAndMapsRows(): void + { + $row = self::DESIGN; + unset($row['document']); + $this->transport->push(200, ['data' => [$row]]); + + $designs = $this->client()->designs()->list('w_1', kind: 'story', limit: 10, offset: 20); + + $this->assertSame('GET', $this->transport->last()['method']); + $this->assertSame( + 'https://api.fopost.com/v1/designs?workspace_id=w_1&kind=story&limit=10&offset=20', + $this->transport->last()['url'], + ); + $this->assertCount(1, $designs); + $this->assertSame('d_1', $designs[0]->id); + $this->assertSame('story', $designs[0]->kind); + $this->assertSame(1080, $designs[0]->width); + $this->assertNull($designs[0]->document); + $this->assertSame('m_9', $designs[0]->thumbnailMediaId); + $this->assertSame('2026-11-20T10:00:00+00:00', $designs[0]->createdAt?->format('c')); + } + + public function testCreatePostsSnakeCaseFields(): void + { + $this->transport->push(201, ['data' => self::DESIGN]); + + $design = $this->client()->designs()->create( + 'w_1', + 'story', + name: 'Launch story', + document: ['pages' => []], + brandVoiceId: 'bv_1', + ); + + $this->assertSame('POST', $this->transport->last()['method']); + $this->assertSame('https://api.fopost.com/v1/designs', $this->transport->last()['url']); + $this->assertSame([ + 'workspace_id' => 'w_1', + 'name' => 'Launch story', + 'kind' => 'story', + 'document' => ['pages' => []], + 'brand_voice_id' => 'bv_1', + ], $this->transport->lastJson()); + $this->assertSame(['pages' => [['layers' => []]]], $design->document); + } + + public function testCreateKeepsAnEmptyObjectDocumentAsAnObject(): void + { + $this->transport->push(201, ['data' => self::DESIGN]); + + $this->client()->designs()->create('w_1', 'post', document: (object) ['data' => (object) []]); + + $this->assertStringContainsString('"document":{"data":{}}', (string) $this->transport->last()['body']); + } + + public function testGetReadsOneDesign(): void + { + $this->transport->push(200, ['data' => self::DESIGN]); + + $design = $this->client()->designs()->get('d_1'); + + $this->assertSame('GET', $this->transport->last()['method']); + $this->assertSame('https://api.fopost.com/v1/designs/d_1', $this->transport->last()['url']); + $this->assertSame('Launch story', $design->name); + $this->assertSame('https://api.fopost.com/v1/media/m_9/file', $design->thumbnailUrl); + } + + public function testGetMapsAMissingDesignToNotFound(): void + { + $this->transport->push(404, ['error' => 'not_found', 'message' => 'Design not found']); + + $this->expectException(NotFoundException::class); + $this->client()->designs()->get('d_404'); + } + + public function testUpdatePatchesOnlyThePassedFieldsAndReadsTheVersion(): void + { + $this->transport->push(200, ['data' => self::DESIGN, 'version' => self::VERSION]); + + $revision = $this->client()->designs()->update('d_1', name: 'Renamed', createVersion: true); + + $this->assertSame('PATCH', $this->transport->last()['method']); + $this->assertSame('https://api.fopost.com/v1/designs/d_1', $this->transport->last()['url']); + $this->assertSame(['name' => 'Renamed', 'create_version' => true], $this->transport->lastJson()); + $this->assertSame('d_1', $revision->design->id); + $this->assertSame(3, $revision->version?->version); + $this->assertNull($revision->version?->document); + } + + public function testAnEmptyUpdateSendsAnObject(): void + { + $this->transport->push(200, ['data' => self::DESIGN, 'version' => null]); + + $revision = $this->client()->designs()->update('d_1'); + + $this->assertSame('{}', $this->transport->last()['body']); + $this->assertNull($revision->version); + } + + public function testDeleteSendsADelete(): void + { + $this->transport->push(200, ['data' => ['id' => 'd_1', 'deleted' => true]]); + + $this->client()->designs()->delete('d_1'); + + $this->assertSame('DELETE', $this->transport->last()['method']); + $this->assertSame('https://api.fopost.com/v1/designs/d_1', $this->transport->last()['url']); + } + + public function testVersionsAsksForDocumentsOnlyWhenTold(): void + { + $this->transport->push(200, ['data' => [self::VERSION]]); + $this->transport->push(200, ['data' => [self::VERSION + ['document' => ['pages' => []]]]]); + + $plain = $this->client()->designs()->versions('d_1'); + $this->assertSame('https://api.fopost.com/v1/designs/d_1/versions', $this->transport->last()['url']); + $this->assertSame('v_1', $plain[0]->id); + $this->assertNull($plain[0]->document); + + $full = $this->client()->designs()->versions('d_1', withDocument: true); + $this->assertSame( + 'https://api.fopost.com/v1/designs/d_1/versions?include=document', + $this->transport->last()['url'], + ); + $this->assertSame(['pages' => []], $full[0]->document); + } + + public function testRestoreVersionPostsToTheVersion(): void + { + $this->transport->push(200, ['data' => self::DESIGN, 'version' => self::VERSION]); + + $revision = $this->client()->designs()->restoreVersion('d_1', 'v_1'); + + $this->assertSame('POST', $this->transport->last()['method']); + $this->assertSame( + 'https://api.fopost.com/v1/designs/d_1/versions/v_1/restore', + $this->transport->last()['url'], + ); + $this->assertSame('d_1', $revision->design->id); + $this->assertSame('v_1', $revision->version?->id); + } + + public function testDuplicateSendsTheNameOrAnEmptyObject(): void + { + $this->transport->push(201, ['data' => ['id' => 'd_2'] + self::DESIGN]); + $this->transport->push(201, ['data' => ['id' => 'd_3'] + self::DESIGN]); + + $copy = $this->client()->designs()->duplicate('d_1', 'Copy'); + $this->assertSame('https://api.fopost.com/v1/designs/d_1/duplicate', $this->transport->last()['url']); + $this->assertSame(['name' => 'Copy'], $this->transport->lastJson()); + $this->assertSame('d_2', $copy->id); + + $this->client()->designs()->duplicate('d_1'); + $this->assertSame('{}', $this->transport->last()['body']); + } + + public function testExportMapsTheRenderedFiles(): void + { + $this->transport->push(200, ['data' => [ + 'format' => 'mp4', + 'files' => [[ + 'media_id' => 'm_1', + 'page_index' => 0, + 'mime_type' => 'video/mp4', + 'preview_url' => 'https://api.fopost.com/v1/media/m_1/file', + ]], + ]]); + + $export = $this->client()->designs()->export('d_1', 'mp4', pageIndexes: [0], durationSec: 10); + + $this->assertSame('POST', $this->transport->last()['method']); + $this->assertSame('https://api.fopost.com/v1/designs/d_1/export', $this->transport->last()['url']); + $this->assertSame( + ['format' => 'mp4', 'page_indexes' => [0], 'duration_sec' => 10], + $this->transport->lastJson(), + ); + $this->assertSame('mp4', $export->format); + $this->assertSame('m_1', $export->files[0]->mediaId); + $this->assertSame(0, $export->files[0]->pageIndex); + $this->assertSame('video/mp4', $export->files[0]->mimeType); + } + + public function testBulkSendsTheRowsAndReturnsTheCopies(): void + { + $this->transport->push(201, ['data' => [self::DESIGN, ['id' => 'd_2'] + self::DESIGN]]); + + $designs = $this->client()->designs()->bulk( + 'd_1', + [['name' => 'Ana', 'headline' => 'Hi'], ['name' => 'Ben', 'headline' => 'Yo']], + nameColumn: 'name', + ); + + $this->assertSame('https://api.fopost.com/v1/designs/d_1/bulk', $this->transport->last()['url']); + $this->assertSame([ + 'rows' => [['name' => 'Ana', 'headline' => 'Hi'], ['name' => 'Ben', 'headline' => 'Yo']], + 'name_column' => 'name', + ], $this->transport->lastJson()); + $this->assertSame(['d_1', 'd_2'], array_map(static fn ($d) => $d->id, $designs)); + } + + public function testPostsMapsMetrics(): void + { + $this->transport->push(200, ['data' => [[ + 'id' => 'p_1', + 'status' => 'published', + 'title' => null, + 'text' => 'Out now', + 'schedule_at' => null, + 'published_at' => '2026-11-19T09:00:00Z', + 'platforms' => ['instagram', 'tiktok'], + 'engagement' => 42, + 'views' => 1000, + ]]]); + + $posts = $this->client()->designs()->posts('d_1'); + + $this->assertSame('GET', $this->transport->last()['method']); + $this->assertSame('https://api.fopost.com/v1/designs/d_1/posts', $this->transport->last()['url']); + $this->assertSame('published', $posts[0]->status); + $this->assertSame(['instagram', 'tiktok'], $posts[0]->platforms); + $this->assertSame(42, $posts[0]->engagement); + $this->assertSame(1000, $posts[0]->views); + $this->assertNull($posts[0]->scheduleAt); + } + + public function testTextToSlidesMapsTheOutline(): void + { + $this->transport->push(200, ['data' => [ + 'cover' => ['title' => 'Five ideas', 'subtitle' => 'For launch week'], + 'slides' => [['heading' => 'One', 'body' => 'Start small']], + 'cta' => ['heading' => 'Follow', 'body' => 'More every week'], + 'brand' => ['colors' => ['#111111'], 'fonts' => [], 'logo_media_id' => null], + ]]); + + $outline = $this->client()->designs()->textToSlides('w_1', text: 'Long text', maxSlides: 6); + + $this->assertSame('https://api.fopost.com/v1/designs/text-to-slides', $this->transport->last()['url']); + $this->assertSame( + ['workspace_id' => 'w_1', 'text' => 'Long text', 'max_slides' => 6], + $this->transport->lastJson(), + ); + $this->assertSame('Five ideas', $outline->cover['title']); + $this->assertSame('Start small', $outline->slides[0]['body']); + $this->assertSame('Follow', $outline->cta['heading']); + $this->assertSame(['#111111'], $outline->brand['colors']); + } + + public function testTextToSlidesSurfacesAnEmptyBalance(): void + { + $this->transport->push(402, [ + 'error' => 'insufficient_credits', + 'message' => 'Your AI balance is empty.', + 'upgrade_url' => '/settings/billing', + ]); + + try { + $this->client()->designs()->textToSlides('w_1', postId: 'p_1'); + $this->fail('expected the 402 to throw'); + } catch (PaymentRequiredException $e) { + $this->assertSame('insufficient_credits', $e->getErrorCode()); + $this->assertSame('/settings/billing', $e->getUpgradeUrl()); + } + } + + public function testFromPostSendsTheOptions(): void + { + $this->transport->push(201, ['data' => self::DESIGN]); + + $design = $this->client()->designs()->fromPost( + 'w_1', + postId: 'p_1', + style: 'quote', + preset: 'instagram-portrait', + accountId: 'a_1', + carousel: false, + name: 'Quote card', + ); + + $this->assertSame('https://api.fopost.com/v1/designs/from-post', $this->transport->last()['url']); + $this->assertSame([ + 'workspace_id' => 'w_1', + 'post_id' => 'p_1', + 'style' => 'quote', + 'preset' => 'instagram-portrait', + 'account_id' => 'a_1', + 'carousel' => false, + 'name' => 'Quote card', + ], $this->transport->lastJson()); + $this->assertSame('d_1', $design->id); + } +} From b5e48153df90d5f5ebe6efe8eefef49a94b6d33d Mon Sep 17 00:00:00 2001 From: Ali Hesari Date: Fri, 20 Nov 2026 23:06:07 +0100 Subject: [PATCH 2/5] feat(fonts): add the fonts resource List the library and a workspace's fonts, upload one font file as multipart from a path or bytes, and delete an uploaded font. --- src/Client.php | 8 +++ src/Model/FontCatalog.php | 32 +++++++++ src/Model/LibraryFont.php | 41 +++++++++++ src/Model/UploadedFont.php | 41 +++++++++++ src/Resource/FontsResource.php | 48 +++++++++++++ tests/FontsTest.php | 127 +++++++++++++++++++++++++++++++++ 6 files changed, 297 insertions(+) create mode 100644 src/Model/FontCatalog.php create mode 100644 src/Model/LibraryFont.php create mode 100644 src/Model/UploadedFont.php create mode 100644 src/Resource/FontsResource.php create mode 100644 tests/FontsTest.php diff --git a/src/Client.php b/src/Client.php index 470c5f2..3c59a72 100644 --- a/src/Client.php +++ b/src/Client.php @@ -16,6 +16,7 @@ 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; @@ -75,6 +76,7 @@ final class Client private readonly AnalyticsResource $analytics; private readonly GoogleBusinessResource $googleBusiness; private readonly DesignsResource $designs; + private readonly FontsResource $fonts; public function __construct( ?string $apiKey = null, @@ -117,6 +119,7 @@ public function __construct( $this->analytics = new AnalyticsResource($this->http); $this->googleBusiness = new GoogleBusinessResource($this->http); $this->designs = new DesignsResource($this->http); + $this->fonts = new FontsResource($this->http); } public function posts(): PostsResource @@ -252,6 +255,11 @@ public function designs(): DesignsResource return $this->designs; } + public function fonts(): FontsResource + { + return $this->fonts; + } + public function baseUrl(): string { return $this->http->baseUrl(); diff --git a/src/Model/FontCatalog.php b/src/Model/FontCatalog.php new file mode 100644 index 0000000..a76145f --- /dev/null +++ b/src/Model/FontCatalog.php @@ -0,0 +1,32 @@ + $library + * @param array $uploaded + */ + private function __construct( + array $raw, + public readonly array $library, + public readonly array $uploaded, + ) { + parent::__construct($raw); + } + + public static function fromArray(mixed $data): static + { + $data = is_array($data) ? $data : []; + + return new self( + $data, + LibraryFont::listFrom(self::seq($data, 'library')), + UploadedFont::listFrom(self::seq($data, 'uploaded')), + ); + } +} diff --git a/src/Model/LibraryFont.php b/src/Model/LibraryFont.php new file mode 100644 index 0000000..11b8d12 --- /dev/null +++ b/src/Model/LibraryFont.php @@ -0,0 +1,41 @@ + $scripts + * @param array $weights + * @param array> $files + */ + private function __construct( + array $raw, + public readonly string $slug, + public readonly string $family, + public readonly ?string $category, + public readonly array $scripts, + public readonly array $weights, + 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, 'slug'), + self::requiredStr($data, 'family'), + self::str($data, 'category'), + array_values(array_filter(self::seq($data, 'scripts'), 'is_string')), + array_values(array_filter(self::seq($data, 'weights'), 'is_int')), + array_values(array_filter(self::seq($data, 'files'), 'is_array')), + ); + } +} diff --git a/src/Model/UploadedFont.php b/src/Model/UploadedFont.php new file mode 100644 index 0000000..1007170 --- /dev/null +++ b/src/Model/UploadedFont.php @@ -0,0 +1,41 @@ +fonts(): the font library and a workspace's own fonts. Needs the `posts` scope. */ +final class FontsResource extends Resource +{ + /** The library, plus the workspace's uploaded fonts when `$workspaceId` is given. */ + public function list(?string $workspaceId = null): FontCatalog + { + return FontCatalog::fromArray(self::unwrap($this->http->get('/fonts', ['workspace_id' => $workspaceId]))); + } + + /** + * Upload one font file (TTF, OTF, WOFF or WOFF2). `$family` defaults to the file name, + * `$weight` to 400 and `$style` (normal or italic) to normal. + * + * @param string|array{filename: string, contents: string, mimeType?: string} $file + * A path or an in-memory file. + */ + public function upload( + string $workspaceId, + string|array $file, + ?string $family = null, + ?int $weight = null, + ?string $style = null, + ): UploadedFont { + $fields = array_map('strval', self::compact([ + 'workspace_id' => $workspaceId, + 'family' => $family, + 'weight' => $weight, + 'style' => $style, + ])); + $body = $this->http->requestMultipart('POST', '/fonts', $fields, self::normalizeFiles([$file]), 'file'); + + return UploadedFont::fromArray(self::unwrap($body)); + } + + public function delete(string $fontId): void + { + $this->http->delete("/fonts/{$fontId}"); + } +} diff --git a/tests/FontsTest.php b/tests/FontsTest.php new file mode 100644 index 0000000..4440ca9 --- /dev/null +++ b/tests/FontsTest.php @@ -0,0 +1,127 @@ + 'f_1', + 'family' => 'Brand Sans', + 'style' => 'italic', + 'weight' => 700, + 'format' => 'woff2', + 'file_url' => 'https://api.fopost.com/v1/fonts/f_1/file', + 'created_at' => '2026-11-20T10:00:00Z', + ]; + + public function testListReadsTheLibraryAndTheWorkspaceFonts(): void + { + $this->transport->push(200, [ + 'library' => [[ + 'slug' => 'inter', + 'family' => 'Inter', + 'category' => 'sans-serif', + 'scripts' => ['latin', 'cyrillic'], + 'weights' => [400, 700], + 'files' => [['weight' => 400, 'url' => '/fonts/inter-400.woff2']], + ]], + 'uploaded' => [self::UPLOADED], + ]); + + $catalog = $this->client()->fonts()->list('w_1'); + + $this->assertSame('GET', $this->transport->last()['method']); + $this->assertSame('https://api.fopost.com/v1/fonts?workspace_id=w_1', $this->transport->last()['url']); + $this->assertSame('inter', $catalog->library[0]->slug); + $this->assertSame(['latin', 'cyrillic'], $catalog->library[0]->scripts); + $this->assertSame([400, 700], $catalog->library[0]->weights); + $this->assertCount(1, $catalog->library[0]->files); + $this->assertSame('f_1', $catalog->uploaded[0]->id); + $this->assertSame('italic', $catalog->uploaded[0]->style); + $this->assertSame(700, $catalog->uploaded[0]->weight); + $this->assertSame('https://api.fopost.com/v1/fonts/f_1/file', $catalog->uploaded[0]->fileUrl); + } + + public function testListWithoutAWorkspaceSendsNoQuery(): void + { + $this->transport->push(200, ['library' => [], 'uploaded' => []]); + + $catalog = $this->client()->fonts()->list(); + + $this->assertSame('https://api.fopost.com/v1/fonts', $this->transport->last()['url']); + $this->assertSame([], $catalog->uploaded); + } + + public function testUploadSendsOneMultipartFile(): void + { + $this->transport->push(201, self::UPLOADED); + + $font = $this->client()->fonts()->upload( + 'w_1', + ['filename' => 'brand.woff2', 'contents' => 'wOF2', 'mimeType' => 'font/woff2'], + family: 'Brand Sans', + weight: 700, + style: 'italic', + ); + + $request = $this->transport->last(); + $body = (string) $request['body']; + $this->assertSame('POST', $request['method']); + $this->assertSame('https://api.fopost.com/v1/fonts', $request['url']); + $this->assertStringStartsWith('multipart/form-data; boundary=', $request['headers']['Content-Type']); + $this->assertStringContainsString('name="workspace_id"' . "\r\n\r\nw_1", $body); + $this->assertStringContainsString('name="family"' . "\r\n\r\nBrand Sans", $body); + $this->assertStringContainsString('name="weight"' . "\r\n\r\n700", $body); + $this->assertStringContainsString('name="style"' . "\r\n\r\nitalic", $body); + $this->assertStringContainsString('name="file"; filename="brand.woff2"', $body); + $this->assertStringContainsString("Content-Type: font/woff2\r\n\r\nwOF2", $body); + $this->assertSame('f_1', $font->id); + $this->assertSame('woff2', $font->format); + } + + public function testUploadReadsAPath(): void + { + $path = tempnam(sys_get_temp_dir(), 'font'); + $this->assertIsString($path); + file_put_contents($path, 'OTTO'); + $this->transport->push(201, self::UPLOADED); + + try { + $this->client()->fonts()->upload('w_1', $path); + } finally { + unlink($path); + } + + $body = (string) $this->transport->last()['body']; + $this->assertStringContainsString('filename="' . basename($path) . '"', $body); + $this->assertStringContainsString("\r\n\r\nOTTO\r\n", $body); + $this->assertStringNotContainsString('name="family"', $body); + } + + public function testAnOversizeFontSurfacesTheError(): void + { + $this->transport->push(413, ['error' => 'file_too_large', 'message' => 'Font files can be up to 5 MB']); + + try { + $this->client()->fonts()->upload('w_1', ['filename' => 'big.ttf', 'contents' => 'x']); + $this->fail('expected the 413 to throw'); + } catch (ApiException $e) { + $this->assertSame(413, $e->getStatus()); + $this->assertSame('file_too_large', $e->getErrorCode()); + } + } + + public function testDeleteSendsADelete(): void + { + $this->transport->push(204); + + $this->client()->fonts()->delete('f_1'); + + $this->assertSame('DELETE', $this->transport->last()['method']); + $this->assertSame('https://api.fopost.com/v1/fonts/f_1', $this->transport->last()['url']); + } +} From fc6c7be4f95e1ddbfa33b7026ffb923388edd1f4 Mon Sep 17 00:00:00 2001 From: Ali Hesari Date: Fri, 20 Nov 2026 23:06:57 +0100 Subject: [PATCH 3/5] feat(video-edits): add the video edits resource List the operations, list jobs, queue a job with [{op, params}] operations, and read one job. --- src/Client.php | 9 +++ src/Model/VideoEdit.php | 59 ++++++++++++++ src/Model/VideoEditOp.php | 31 ++++++++ src/Resource/VideoEditsResource.php | 58 ++++++++++++++ tests/VideoEditsTest.php | 115 ++++++++++++++++++++++++++++ 5 files changed, 272 insertions(+) create mode 100644 src/Model/VideoEdit.php create mode 100644 src/Model/VideoEditOp.php create mode 100644 src/Resource/VideoEditsResource.php create mode 100644 tests/VideoEditsTest.php diff --git a/src/Client.php b/src/Client.php index 3c59a72..dfa8d30 100644 --- a/src/Client.php +++ b/src/Client.php @@ -29,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; @@ -77,6 +78,7 @@ final class Client private readonly GoogleBusinessResource $googleBusiness; private readonly DesignsResource $designs; private readonly FontsResource $fonts; + private readonly VideoEditsResource $videoEdits; public function __construct( ?string $apiKey = null, @@ -120,6 +122,7 @@ public function __construct( $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 @@ -260,6 +263,12 @@ 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(); diff --git a/src/Model/VideoEdit.php b/src/Model/VideoEdit.php new file mode 100644 index 0000000..4930ae3 --- /dev/null +++ b/src/Model/VideoEdit.php @@ -0,0 +1,59 @@ +}> $operations + * @param array{id?: string, preview_url?: ?string, mime_type?: ?string}|null $resultMedia + * @param array> $extraOutputs Additional files, such as clips or subtitles. + * @param array|null $result + */ + private function __construct( + array $raw, + public readonly string $id, + /** queued, running, succeeded or failed. */ + public readonly string $status, + public readonly ?float $progress, + public readonly ?string $sourceMediaId, + public readonly array $operations, + public readonly ?string $resultMediaId, + public readonly ?array $resultMedia, + public readonly array $extraOutputs, + public readonly ?array $result, + public readonly ?string $error, + public readonly ?DateTimeImmutable $createdAt, + public readonly ?DateTimeImmutable $updatedAt, + public readonly ?DateTimeImmutable $completedAt, + ) { + 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::num($data, 'progress'), + self::str($data, 'source_media_id'), + array_values(array_filter(self::seq($data, 'operations'), 'is_array')), + self::str($data, 'result_media_id'), + self::nested($data, 'result_media'), + array_values(array_filter(self::seq($data, 'extra_outputs'), 'is_array')), + self::nested($data, 'result'), + self::str($data, 'error'), + self::date($data, 'created_at'), + self::date($data, 'updated_at'), + self::date($data, 'completed_at'), + ); + } +} diff --git a/src/Model/VideoEditOp.php b/src/Model/VideoEditOp.php new file mode 100644 index 0000000..08bd2c7 --- /dev/null +++ b/src/Model/VideoEditOp.php @@ -0,0 +1,31 @@ +videoEdits(): trim, cut, subtitle, clip and otherwise edit a library video as a + * background job. Needs the `posts` scope; operations that use AI also need `ai`. + */ +final class VideoEditsResource extends Resource +{ + /** @return array */ + public function ops(): array + { + return VideoEditOp::listFrom(self::unwrap($this->http->get('/video-edits/ops'))); + } + + /** + * Recent jobs, newest first. Needs `$workspaceId` or `$mediaId`. + * + * @return array + */ + public function list(?string $workspaceId = null, ?string $mediaId = null, ?int $limit = null): array + { + $query = ['workspace_id' => $workspaceId, 'media_id' => $mediaId, 'limit' => $limit]; + + return VideoEdit::listFrom(self::unwrap($this->http->get('/video-edits', $query))); + } + + /** + * Queue up to ten operations on a video, run in order. Each operation is + * `['op' => 'trim', 'params' => ['startSec' => 0, 'endSec' => 10]]`; poll get() for the result. + * + * @param array|object}> $operations + */ + public function create(string $mediaId, array $operations, ?string $workspaceId = null): VideoEdit + { + $ops = []; + foreach ($operations as $operation) { + $params = $operation['params'] ?? []; + $ops[] = $params === [] ? ['op' => $operation['op']] : ['op' => $operation['op'], 'params' => $params]; + } + $body = self::compact(['workspace_id' => $workspaceId, 'media_id' => $mediaId, 'operations' => $ops]); + + return VideoEdit::fromArray(self::unwrap($this->http->post('/video-edits', $body))); + } + + public function get(string $jobId, ?string $workspaceId = null): VideoEdit + { + return VideoEdit::fromArray( + self::unwrap($this->http->get("/video-edits/{$jobId}", ['workspace_id' => $workspaceId])), + ); + } +} diff --git a/tests/VideoEditsTest.php b/tests/VideoEditsTest.php new file mode 100644 index 0000000..74eb5c9 --- /dev/null +++ b/tests/VideoEditsTest.php @@ -0,0 +1,115 @@ + 've_1', + 'status' => 'succeeded', + 'progress' => 1, + 'source_media_id' => 'm_1', + 'operations' => [['op' => 'trim', 'params' => ['startSec' => 0, 'endSec' => 10]]], + 'result_media_id' => 'm_2', + 'result_media' => [ + 'id' => 'm_2', + 'preview_url' => 'https://api.fopost.com/v1/media/m_2/file', + 'mime_type' => 'video/mp4', + ], + 'extra_outputs' => [['media_id' => 'm_3', 'label' => 'Subtitles', 'mime' => 'text/vtt', 'preview_url' => null]], + 'result' => null, + 'error' => null, + 'created_at' => '2026-11-20T10:00:00Z', + 'updated_at' => '2026-11-20T10:01:00Z', + 'completed_at' => '2026-11-20T10:01:00Z', + ]; + + public function testOpsListsTheOperations(): void + { + $this->transport->push(200, ['data' => [ + ['name' => 'trim', 'available' => true, 'uses_ai' => false], + ['name' => 'subtitles', 'available' => true, 'uses_ai' => true], + ]]); + + $ops = $this->client()->videoEdits()->ops(); + + $this->assertSame('GET', $this->transport->last()['method']); + $this->assertSame('https://api.fopost.com/v1/video-edits/ops', $this->transport->last()['url']); + $this->assertSame('trim', $ops[0]->name); + $this->assertTrue($ops[0]->available); + $this->assertFalse($ops[0]->usesAi); + $this->assertTrue($ops[1]->usesAi); + } + + public function testListSendsTheFilters(): void + { + $this->transport->push(200, ['data' => [self::JOB]]); + + $jobs = $this->client()->videoEdits()->list('w_1', mediaId: 'm_1', limit: 5); + + $this->assertSame( + 'https://api.fopost.com/v1/video-edits?workspace_id=w_1&media_id=m_1&limit=5', + $this->transport->last()['url'], + ); + $this->assertSame('ve_1', $jobs[0]->id); + } + + public function testCreateSendsTheOperationsAndMapsTheJob(): void + { + $this->transport->push(201, ['status' => 'queued', 'progress' => 0, 'result_media' => null] + self::JOB); + + $job = $this->client()->videoEdits()->create('m_1', [ + ['op' => 'trim', 'params' => ['startSec' => 0, 'endSec' => 10]], + ['op' => 'denoise'], + ['op' => 'remove_silence', 'params' => []], + ], workspaceId: 'w_1'); + + $this->assertSame('POST', $this->transport->last()['method']); + $this->assertSame('https://api.fopost.com/v1/video-edits', $this->transport->last()['url']); + $this->assertSame([ + 'workspace_id' => 'w_1', + 'media_id' => 'm_1', + 'operations' => [ + ['op' => 'trim', 'params' => ['startSec' => 0, 'endSec' => 10]], + ['op' => 'denoise'], + ['op' => 'remove_silence'], + ], + ], $this->transport->lastJson()); + $this->assertSame('queued', $job->status); + $this->assertSame(0.0, $job->progress); + $this->assertNull($job->resultMedia); + } + + public function testCreateSurfacesARejectedOperation(): void + { + $this->transport->push(400, ['error' => 'validation_error', 'message' => 'Invalid video edit request']); + + $this->expectException(ValidationException::class); + $this->client()->videoEdits()->create('m_1', [['op' => 'speed', 'params' => ['factor' => 9]]]); + } + + public function testGetMapsTheFinishedJob(): void + { + $this->transport->push(200, self::JOB); + + $job = $this->client()->videoEdits()->get('ve_1', 'w_1'); + + $this->assertSame('GET', $this->transport->last()['method']); + $this->assertSame( + 'https://api.fopost.com/v1/video-edits/ve_1?workspace_id=w_1', + $this->transport->last()['url'], + ); + $this->assertSame('succeeded', $job->status); + $this->assertSame(1.0, $job->progress); + $this->assertSame('m_1', $job->sourceMediaId); + $this->assertSame('trim', $job->operations[0]['op']); + $this->assertSame('m_2', $job->resultMediaId); + $this->assertSame('video/mp4', $job->resultMedia['mime_type'] ?? null); + $this->assertSame('Subtitles', $job->extraOutputs[0]['label']); + $this->assertSame('2026-11-20T10:01:00+00:00', $job->completedAt?->format('c')); + } +} From 0c3cca00ad023ba3365373b9cf998984dc6a931d Mon Sep 17 00:00:00 2001 From: Ali Hesari Date: Fri, 20 Nov 2026 23:07:27 +0100 Subject: [PATCH 4/5] feat(media): generate alt text for a library image --- src/Model/GeneratedAltText.php | 31 +++++++++++++++++++++++++++++++ src/Resource/MediaResource.php | 10 ++++++++++ tests/MediaTest.php | 30 ++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 src/Model/GeneratedAltText.php diff --git a/src/Model/GeneratedAltText.php b/src/Model/GeneratedAltText.php new file mode 100644 index 0000000..93908e4 --- /dev/null +++ b/src/Model/GeneratedAltText.php @@ -0,0 +1,31 @@ + $tags */ + private function __construct( + array $raw, + public readonly string $altText, + public readonly ?string $altTextSource, + public readonly array $tags, + ) { + parent::__construct($raw); + } + + public static function fromArray(mixed $data): static + { + $data = is_array($data) ? $data : []; + + return new self( + $data, + self::requiredStr($data, 'alt_text'), + self::str($data, 'alt_text_source'), + array_values(array_filter(self::seq($data, 'tags'), 'is_string')), + ); + } +} diff --git a/src/Resource/MediaResource.php b/src/Resource/MediaResource.php index 4c5c03d..29188ec 100644 --- a/src/Resource/MediaResource.php +++ b/src/Resource/MediaResource.php @@ -4,6 +4,7 @@ namespace Fopost\Sdk\Resource; +use Fopost\Sdk\Model\GeneratedAltText; use Fopost\Sdk\Model\MediaAsset; use Fopost\Sdk\Model\Page; use Fopost\Sdk\Model\PresignedUpload; @@ -58,6 +59,15 @@ public function delete(string $mediaId): void $this->http->delete("/media/{$mediaId}"); } + /** + * Write alt text for a library image with AI and store it, replacing any existing text. + * Uses AI credits, so an API key also needs the `ai` scope. + */ + public function generateAltText(string $mediaId): GeneratedAltText + { + return GeneratedAltText::fromArray(self::unwrap($this->http->post("/media/{$mediaId}/alt-text"))); + } + /** The largest file the account may upload, and which route carries it. */ public function limits(?string $workspaceId = null): UploadLimits { diff --git a/tests/MediaTest.php b/tests/MediaTest.php index 752a793..0e9ee6e 100644 --- a/tests/MediaTest.php +++ b/tests/MediaTest.php @@ -6,6 +6,7 @@ use Fopost\Sdk\Exception\ApiException; use Fopost\Sdk\Exception\PermissionDeniedException; +use Fopost\Sdk\Exception\ValidationException; final class MediaTest extends TestCase { @@ -246,4 +247,33 @@ public function testListAndDeleteReachTheLibrary(): void $this->assertSame('DELETE', $this->transport->last()['method']); $this->assertSame('https://api.fopost.com/v1/media/m_1', $this->transport->last()['url']); } + + public function testGenerateAltTextPostsAndMapsTheResult(): void + { + $this->transport->push(200, [ + 'alt_text' => 'A red bicycle against a white wall', + 'alt_text_source' => 'ai', + 'tags' => ['bicycle', 'red'], + ]); + + $alt = $this->client()->media()->generateAltText('m_1'); + + $this->assertSame('POST', $this->transport->last()['method']); + $this->assertSame('https://api.fopost.com/v1/media/m_1/alt-text', $this->transport->last()['url']); + $this->assertNull($this->transport->last()['body']); + $this->assertSame('A red bicycle against a white wall', $alt->altText); + $this->assertSame('ai', $alt->altTextSource); + $this->assertSame(['bicycle', 'red'], $alt->tags); + } + + public function testGenerateAltTextOnANonImageIsAValidationError(): void + { + $this->transport->push(400, [ + 'error' => 'validation_error', + 'message' => 'Alt text can only be generated for images', + ]); + + $this->expectException(ValidationException::class); + $this->client()->media()->generateAltText('m_video'); + } } From 9d6499e050eb4de154c1797c28b06dac84fbb47a Mon Sep 17 00:00:00 2001 From: Ali Hesari Date: Fri, 20 Nov 2026 23:08:04 +0100 Subject: [PATCH 5/5] docs: document designs, fonts, video edits and alt text --- CHANGELOG.md | 13 ++++++++ CLAUDE.md | 7 ++++- README.md | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2371a6..756d157 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/CLAUDE.md b/CLAUDE.md index 89cc74a..0cc3b6c 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -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`, @@ -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 diff --git a/README.md b/README.md index 3aba776..083d9b1 100644 --- a/README.md +++ b/README.md @@ -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.