CroctStoriesApi

Learn how to serve personalized content inside Storyblok stories.

This class decorates Storyblok’s Stories API to serve personalized content inside your stories.

It implements the same StoriesApiInterface as the API it wraps, so you can use it as a drop-in replacement. Every story it returns has its blocks linked to a Croct slot replaced with personalized content, converted to the Storyblok shape.

Signature

The constructor has the following signature:

public function __construct(    StoriesApiInterface $api,    Plug $plug,    ?ContentResolver $resolver = null,)

Example

Here is a basic example of how to wrap your Stories API:

12345678910111213141516
<?phpuse Croct\Plug\Croct;use Croct\Plug\Storyblok\CroctStoriesApi;use Storyblok\Api\StoriesApi;use Storyblok\Api\StoryblokClient;
$plug = Croct::fromDotenv();
$client = new StoryblokClient(    baseUri: 'https://api.storyblok.com',    token: 'YOUR_ACCESS_TOKEN',);
$stories = new CroctStoriesApi(new StoriesApi($client), $plug);
$response = $stories->bySlug('home');

Every read method, such as bySlug, byId, and all, returns stories with their linked blocks resolved. When a fetch fails, the block keeps its original content, so your pages never break.

Parameters

The following list describes the constructor parameters:

api

The Storyblok Stories API to decorate.

plug

The Croct SDK instance used to fetch personalized content.

resolver(optional)
ContentResolver

The resolver that converts Croct content to the Storyblok shape.

This is an advanced customization point. The default resolver handles the standard content schema.

Default:null