PHPackages                             storyblok/php-content-api-client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. storyblok/php-content-api-client

ActiveLibrary[API Development](/categories/api)

storyblok/php-content-api-client
================================

PHP Client for Storyblok Content API

1.16.0(2mo ago)11136.8k↓14.6%7[1 PRs](https://github.com/storyblok/php-content-api-client/pulls)3MITPHPPHP &gt;=8.3CI passing

Since Jan 15Pushed 1mo ago8 watchersCompare

[ Source](https://github.com/storyblok/php-content-api-client)[ Packagist](https://packagist.org/packages/storyblok/php-content-api-client)[ RSS](/packages/storyblok-php-content-api-client/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (34)Versions (33)Used By (3)

 [![Storyblok PHP Content API Client](assets/php-content-delivery-api-client-github-repository.png)](assets/php-content-delivery-api-client-github-repository.png)Storyblok Content Delivery API Client
=====================================

[](#storyblok-content-delivery-api-client)

Co-created with [SensioLabs](https://sensiolabs.com/), the creators of Symfony.

BranchPHPCode Coverage`master`[![PHP](https://github.com/storyblok/php-content-api-client/actions/workflows/ci.yaml/badge.svg)](https://github.com/storyblok/php-content-api-client/actions/workflows/ci.yaml)[![codecov](https://camo.githubusercontent.com/aeed475ae49144b72f271413908cda074f2ed8e89929684e52e29e09358b1251/68747470733a2f2f636f6465636f762e696f2f67682f73746f7279626c6f6b2f7068702d636f6e74656e742d6170692d636c69656e742f67726170682f62616467652e737667)](https://codecov.io/gh/storyblok/php-content-api-client)Symfony
-------

[](#symfony)

Use the [storyblok/storyblok-bundle](https://github.com/storyblok/storyblok-bundle) to integrate this library into your Symfony application.

Usage
-----

[](#usage)

### Installation

[](#installation)

```
composer require storyblok/php-content-api-client
```

### Setup

[](#setup)

```
use Storyblok\Api\StoryblokClient;

$client = new StoryblokClient(
    baseUri: 'https://api.storyblok.com',
    token: '***********',
    timeout: 10 // optional
);

// you can now request any endpoint which needs authentication
$client->request('GET', '/api/something', $options);
```

Spaces
------

[](#spaces)

In your code you should type-hint to `Storyblok\Api\SpacesApiInterface`

### Get the current space

[](#get-the-current-space)

Returns the space associated with the current token.

```
use Storyblok\Api\SpacesApi;
use Storyblok\Api\StoryblokClient;

$client = new StoryblokClient(/* ... */);
$spacesApi = new SpacesApi($client);

$response = $spacesApi->me();
```

Stories
-------

[](#stories)

In your code you should type-hint to `Storyblok\Api\StoriesApiInterface`

### Get all available stories

[](#get-all-available-stories)

```
use Storyblok\Api\StoriesApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Request\StoriesRequest;

$client = new StoryblokClient(/* ... */);

$storiesApi = new StoriesApi($client);
$response = $storiesApi->all(new StoriesRequest(language: 'de'));
```

### Fetch by Version (`draft`, `published`)

[](#fetch-by-version-draft-published)

#### Global

[](#global)

```
use Storyblok\Api\StoriesApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Domain\Value\Dto\Version;
use Storyblok\Api\Request\StoryRequest;

$client = new StoryblokClient(/* ... */);

$storiesApi = new StoriesApi($client, Version::Draft);
$response = $storiesApi->bySlug('/my-story/', new StoryRequest(
    language: 'de',
));
```

#### Method Call

[](#method-call)

```
use Storyblok\Api\StoriesApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Domain\Value\Dto\Version;
use Storyblok\Api\Request\StoryRequest;

$client = new StoryblokClient(/* ... */);

$storiesApi = new StoriesApi($client, Version::Published);
$response = $storiesApi->bySlug('/my-story/', new StoryRequest(
    language: 'de',
    version: Version::Draft, // This overrides the global "version"
));
```

### Pagination

[](#pagination)

```
use Storyblok\Api\StoriesApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Domain\Value\Dto\Pagination;
use Storyblok\Api\Request\StoriesRequest;

$client = new StoryblokClient(/* ... */);

$storiesApi = new StoriesApi($client);
$response = $storiesApi->all(new StoriesRequest(
    language: 'de',
    pagination: new Pagination(page: 1, perPage: 30)
));
```

#### Sorting

[](#sorting)

```
use Storyblok\Api\StoriesApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Domain\Value\Dto\SortBy;
use Storyblok\Api\Domain\Value\Dto\Direction;
use Storyblok\Api\Request\StoriesRequest;

$client = new StoryblokClient(/* ... */);

$storiesApi = new StoriesApi($client);
$response = $storiesApi->all(new StoriesRequest(
    language: 'de',
    sortBy: new SortBy(field: 'title', direction: Direction::Desc)
));
```

#### Filtering

[](#filtering)

```
use Storyblok\Api\StoriesApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Domain\Value\Filter\FilterCollection;
use Storyblok\Api\Domain\Value\Dto\Direction;
use Storyblok\Api\Domain\Value\Filter\Filters\InFilter;
use Storyblok\Api\Request\StoriesRequest;

$client = new StoryblokClient(/* ... */);

$storiesApi = new StoriesApi($client);
$response = $storiesApi->all(new StoriesRequest(
    language: 'de',
    filters: new FilterCollection([
        new InFilter(field: 'single_reference_field', value: 'f2fdb571-a265-4d8a-b7c5-7050d23c2383')
    ])
));
```

#### Available filters

[](#available-filters)

[AllInArrayFilter.php](src/Domain/Value/Filter/Filters/AllInArrayFilter.php)

Example:

```
use Storyblok\Api\Domain\Value\Filter\Filters\AllInArrayFilter;

new AllInArrayFilter(field: 'tags', value: ['foo', 'bar', 'baz']);
```

[AnyInArrayFilter.php](src/Domain/Value/Filter/Filters/AnyInArrayFilter.php)

Example:

```
use Storyblok\Api\Domain\Value\Filter\Filters\AnyInArrayFilter;

new AnyInArrayFilter(field: 'tags', value: ['foo', 'bar', 'baz']);
```

[GreaterThanDateFilter.php](src/Domain/Value/Filter/Filters/GreaterThanDateFilter.php)

Example:

```
use Storyblok\Api\Domain\Value\Filter\Filters\GreaterThanDateFilter;

new GreaterThanDateFilter(field: 'created_at', value: new \DateTimeImmutable());
```

[LessThanDateFilter.php](src/Domain/Value/Filter/Filters/LessThanDateFilter.php)

Example:

```
use Storyblok\Api\Domain\Value\Filter\Filters\LessThanDateFilter;

new LessThanDateFilter(field: 'created_at', value: new \DateTimeImmutable());
```

[GreaterThanFloatFilter.php](src/Domain/Value/Filter/Filters/GreaterThanFloatFilter.php)

Example:

```
use Storyblok\Api\Domain\Value\Filter\Filters\GreaterThanFloatFilter;

new GreaterThanFloatFilter(field: 'price', value: 39.99);
```

[LessThanFloatFilter.php](src/Domain/Value/Filter/Filters/LessThanFloatFilter.php)

Example:

```
use Storyblok\Api\Domain\Value\Filter\Filters\LessThanFloatFilter;

new LessThanFloatFilter(field: 'price', value: 199.99);
```

[GreaterThanIntFilter.php](src/Domain/Value/Filter/Filters/GreaterThanIntFilter.php)

Example:

```
use Storyblok\Api\Domain\Value\Filter\Filters\GreaterThanIntFilter;

new GreaterThanIntFilter(field: 'stock', value: 0);
```

[LessThanIntFilter.php](src/Domain/Value/Filter/Filters/LessThanIntFilter.php)

Example:

```
use Storyblok\Api\Domain\Value\Filter\Filters\LessThanIntFilter;

new LessThanIntFilter(field: 'stock', value: 100);
```

[InFilter.php](src/Domain/Value/Filter/Filters/InFilter.php)

Example:

```
use Storyblok\Api\Domain\Value\Filter\Filters\InFilter;

new InFilter(field: 'text', value: 'Hello World!');
// or
new InFilter(field: 'text', value: ['Hello Symfony!', 'Hello SensioLabs!']);
```

[NotInFilter.php](src/Domain/Value/Filter/Filters/NotInFilter.php)

Example:

```
use Storyblok\Api\Domain\Value\Filter\Filters\NotInFilter;

new NotInFilter(field: 'text', value: 'Hello World!');
// or
new NotInFilter(field: 'text', value: ['Bye Symfony!', 'Bye SensioLabs!']);
```

[IsFilter.php](src/Domain/Value/Filter/Filters/IsFilter.php)

Example:

```
use Storyblok\Api\Domain\Value\Filter\Filters\IsFilter;

// You can use one of the following constants:
// IsFilter::EMPTY_ARRAY
// IsFilter::NOT_EMPTY_ARRAY
// IsFilter::EMPTY
// IsFilter::NOT_EMPTY
// IsFilter::TRUE
// IsFilter::FALSE
// IsFilter::NULL
// IsFilter::NOT_NULL

new IsFilter(field: 'text', value: IsFilter::EMPTY);
```

[LikeFilter.php](src/Domain/Value/Filter/Filters/LikeFilter.php)

Example:

```
use Storyblok\Api\Domain\Value\Filter\Filters\LikeFilter;

new LikeFilter(field: 'description', value: '*I love Symfony*');
```

[NotLikeFilter.php](src/Domain/Value/Filter/Filters/NotLikeFilter.php)

Example:

```
use Storyblok\Api\Domain\Value\Filter\Filters\NotLikeFilter;

new NotLikeFilter(field: 'description', value: '*Text*');
```

[OrFilter.php](src/Domain/Value/Filter/Filters/OrFilter.php)

Example:

```
use Storyblok\Api\Domain\Value\Filter\Filters\OrFilter;
use Storyblok\Api\Domain\Value\Filter\Filters\LikeFilter;
use Storyblok\Api\Domain\Value\Filter\Filters\NotLikeFilter;

new OrFilter(
    new LikeFilter(field: 'text', value: 'Yes!*'),
    new LikeFilter(field: 'text', value: 'Maybe!*'),
    // ...
);
```

#### Filtering by native date fields

[](#filtering-by-native-date-fields)

The Storyblok API allows filtering by published or updated dates. Those fields could not be filtered by `gt_date` or `lt_date` operations and have dedicated parameters in the request.

Supported date parameters are:

- `published_at_gt`
- `published_at_lt`
- `first_published_at_gt`
- `first_published_at_lt`
- `updated_at_gt`
- `updated_at_lt`

Ensure that the dates used have to be in UTC timezone.

```
use Storyblok\Api\StoriesApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Domain\Value\QueryParameter\PublishedAtGt;
use Storyblok\Api\Request\StoriesRequest;

$dateTime = new \DateTimeImmutable('1969-12-28 12:12:12.425', new \DateTimeZone('UTC'));

$client = new StoryblokClient(/* ... */);

$storiesApi = new StoriesApi($client);
$response = $storiesApi->all(new StoriesRequest(
    language: 'de',
    publishedAtGt: new PublishedAtGt($dateTime)
));
```

### Get all available stories by Content Type (`string`)

[](#get-all-available-stories-by-content-type-string)

```
use Storyblok\Api\StoriesApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Request\StoriesRequest;

$client = new StoryblokClient(/* ... */);

$storiesApi = new StoriesApi($client);
$response = $storiesApi->allByContentType('custom_content_type', new StoriesRequest(
    language: 'de',
));
```

### Get multiple stories by multiple uuid's (`array`)

[](#get-multiple-stories-by-multiple-uuids-array)

```
use Storyblok\Api\StoriesApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Request\StoriesRequest;
use Storyblok\Api\Domain\Value\Uuid;

$client = new StoryblokClient(/* ... */);

$storiesApi = new StoriesApi($client);
$response = $storiesApi->allByUuids([new Uuid(/** ... */), new Uuid(/** ... */)], new StoriesRequest(
    language: 'de',
));
```

### Get by uuid (`Storyblok\Api\Domain\Value\Uuid`)

[](#get-by-uuid-storyblokapidomainvalueuuid)

```
use Storyblok\Api\StoriesApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Domain\Value\Uuid;
use Storyblok\Api\Request\StoryRequest;

$uuid = new Uuid(/** ... */);

$client = new StoryblokClient(/* ... */);

$storiesApi = new StoriesApi($client);
$response = $storiesApi->byUuid($uuid, new StoryRequest(
    language: 'de',
));
```

### Get by slug (`string`)

[](#get-by-slug-string)

```
use Storyblok\Api\StoriesApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Request\StoryRequest;

$client = new StoryblokClient(/* ... */);

$storiesApi = new StoriesApi($client);
$response = $storiesApi->bySlug('folder/slug', new StoryRequest(
    language: 'de',
));
```

### Get by id (`Storyblok\Api\Domain\Value\Id`)

[](#get-by-id-storyblokapidomainvalueid)

```
use Storyblok\Api\StoriesApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Domain\Value\Id;
use Storyblok\Api\Request\StoryRequest;

$id = new Id(/** ... */);

$client = new StoryblokClient(/* ... */);

$storiesApi = new StoriesApi($client);
$response = $storiesApi->byId($id, new StoryRequest(
    language: 'de',
));
```

Links
-----

[](#links)

In your code you should type-hint to `Storyblok\Api\LinksApiInterface`

### Get all available links

[](#get-all-available-links)

```
use Storyblok\Api\LinksApi;
use Storyblok\Api\StoryblokClient;

$client = new StoryblokClient(/* ... */);

$linksApi = new LinksApi($client);
$response = $linksApi->all();
```

### Pagination

[](#pagination-1)

```
use Storyblok\Api\LinksApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Domain\Value\Dto\Pagination;
use Storyblok\Api\Request\LinksRequest;

$client = new StoryblokClient(/* ... */);

$linksApi = new LinksApi($client);
$response = $linksApi->all(new LinksRequest(
    pagination: new Pagination(page: 1, perPage: 1000)
));
```

### Get by parent (`Storyblok\Api\Domain\Value\Id`)

[](#get-by-parent-storyblokapidomainvalueid)

```
use Storyblok\Api\LinksApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Domain\Value\Id;

$id = new Id(/** ... */);

$client = new StoryblokClient(/* ... */);

$linksApi = new LinksApi($client);
$response = $linksApi->byParent($id);
```

### Get all root links

[](#get-all-root-links)

```
use Storyblok\Api\LinksApi;
use Storyblok\Api\StoryblokClient;

$client = new StoryblokClient(/* ... */);

$linksApi = new LinksApi($client);
$response = $linksApi->roots($id);
```

Datasource
----------

[](#datasource)

In your code you should type-hint to `Storyblok\Api\DatasourceApiInterface`

### Get by name (`string`)

[](#get-by-name-string)

```
use Storyblok\Api\DatasourceApi;
use Storyblok\Api\StoryblokClient;

$client = new StoryblokClient(/* ... */);

$api = new DatasourceApi($client);
$response = $api->byName('tags'); // returns Storyblok\Api\Domain\Value\Datasource
```

If it has more than one dimension, you can get the entries by

```
use Storyblok\Api\DatasourceApi;
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\Domain\Value\Datasource\Dimension;

$client = new StoryblokClient(/* ... */);

$api = new DatasourceApi($client);
$response = $api->byName('tags', new Dimension('de')); // returns Storyblok\Api\Domain\Value\Datasource
```

Tags
----

[](#tags)

In your code you should type-hint to `Storyblok\Api\TagsApiInterface`

### Get all available tags

[](#get-all-available-tags)

```
use Storyblok\Api\TagsApi;
use Storyblok\Api\StoryblokClient;

$client = new StoryblokClient(/* ... */);

$api = new TagsApi($client);
$response = $api->all(); // returns Storyblok\Api\Response\TagsResponse
```

Assets
------

[](#assets)

To use the assets API you have to configure the Assets client. The token used for the Assets API is not the preview or public token. It is a specific token for assets. You can create and manage your asset tokens in the **Space Settings** under the **Access Tokens** section. The `get()` method expects the full image URL as its parameter.

```
use Storyblok\Api\StoryblokClient;
use Storyblok\Api\AssetsApi;

$assetsClient = new StoryblokClient(
    baseUri: 'https://api.storyblok.com',
    token: 'your-assets-api-token',
    timeout: 10 // optional
);

$assetsApi = new AssetsApi($assetsClient);
$assetsApi->get('https://a.storyblok/your-full-path-URL-image.jpeg')
```

### DX Enhancement through Abstract Collections

[](#dx-enhancement-through-abstract-collections)

To improve developer experience (DX), especially when working with content types like stories, the following abstract class is provided to manage collections of specific content types. This class simplifies data handling and ensures type safety while dealing with large amounts of content from Storyblok.

#### Abstract ContentTypeCollection Class

[](#abstract-contenttypecollection-class)

The ContentTypeCollection class provides a structured way to work with Storyblok content types. It makes managing pagination, filtering, and sorting more intuitive and reusable, saving time and reducing boilerplate code.

```
