Skip to content

@storyblok/astro is Storyblok’s official SDK for Astro applications.

  • Astro version 3.0 or later
  • Node.js LTS (version 22.x recommended)
  • Modern web browser (for example, Chrome, Firefox, Safari, Edge — latest versions)

Add the package to a project by running this command in the terminal:

Terminal window
npm install @storyblok/astro@latest

Import and initialize the SDK using the access token of a Storyblok space.

astro.config.mjs
import { defineConfig } from "astro/config";
import { storyblok } from "@storyblok/astro";
export default defineConfig({
integrations: [
storyblok({
accessToken: "YOUR_ACCESS_TOKEN",
apiOptions: {
region: "eu",
},
components: {
page: "storyblok/Page",
feature: "storyblok/Feature",
},
}),
],
});

Create an Astro component for each block defined in Storyblok and registered in the configuration. Each component will receive a blok prop, containing the content of the block.

src/storyblok/Feature.astro
---
import { storyblokEditable } from "@storyblok/astro";
const { blok } = Astro.props;
---
<div {...storyblokEditable(blok)}>
<h2>{blok.headline}</h2>
</div>

Use <StoryblokComponent> to automatically render nested components (provided they are registered globally).

src/storyblok/Page.astro
---
import { storyblokEditable } from "@storyblok/astro";
import StoryblokComponent from "@storyblok/astro/StoryblokComponent.astro";
const { blok } = Astro.props;
---
<main {...storyblokEditable(blok)}>{blok.body?.map((blok) => <StoryblokComponent blok={blok} />)}</main>

In an Astro page or component, use the client to fetch a story and render the content using StoryblokComponent.

src/pages/index.astro
---
import { useStoryblokApi } from "@storyblok/astro";
import StoryblokComponent from "@storyblok/astro/StoryblokComponent.astro";
import BaseLayout from "../layouts/BaseLayout.astro";
const sbApi = useStoryblokApi();
const { data } = await sbApi.get("cdn/stories/home", {
version: "draft",
resolve_relations: ["featured-articles.posts"],
});
const story = data.story;
---
<BaseLayout>
<StoryblokComponent blok={story.content} />
</BaseLayout>

Import and initialize the SDK to access and configure all features.

import { defineConfig } from "astro/config";
import { storyblok } from "@storyblok/astro";
export default defineConfig({
integrations: [storyblok(OPTIONS)],
});

All options listed in the @storyblok/js package reference are available. The following additional options are available:

KeyDescriptionType
bridgeEnable or disable the Storyblok Bridge. Without livePreview being enabled, the Bridge is configured to refresh on saving and publishing the story. Pass an object with Bridge options for additional configuration. See example below.boolean | object
componentsRegister Astro components intended to render Storyblok blocks. Located in the src directory by default. See example below.object
componentsDirDefine a custom directory other than src as the location of components.string
livePreviewEnable or disable live preview. Disabled by default.boolean
enableFallbackComponentEnable or disable a fallback component to be rendered if no Astro component has been defined for a Storyblok block. Disabled by default.boolean
customFallbackComponentRegister a custom fallback component. Requires enableFallbackComponent to be enabled. See example below.string
useCustomApiBy default, the apiPlugin from @storyblok/js is loaded. Disable this behavior by setting useCustomApi to true to optimize the final bundle when using a custom method to fetch data from Storyblok.boolean

Register components as follows. The key represents the technical name of the Storyblok block, the value represents the location of the Astro component. The src folder is prepended, the .astro file extension is appended.

storyblok({
components: {
page: "storyblok/Page",
feature: "storyblok/Feature",
grid: "storyblok/Grid",
teaser: "storyblok/Teaser",
},
});
storyblok({
enableFallbackComponent: true,
customFallbackComponent: "storyblok/MyCustomFallback",
});

Learn more in the @storyblok/preview-bridge reference. Note that resolveRelations and resolveLinks only become effective when livePreview is enabled.

storyblok({
bridge: {
customParent: "iframe-container",
preventClicks: true, // Defaults to false.
resolveRelations: ["article.author"],
resolveLinks: "story",
},
});

useStoryblokApi() returns the client instantiated in the application.

---
import { useStoryblokApi } from "@storyblok/astro";
const storyblokApi = useStoryblokApi();
const { data } = await storyblokApi.get(URL, API_OPTIONS);
---

For the API_OPTIONS, see the storyblok-js-client reference.

The storyblokApi client provides direct access to the Storyblok API.

import { storyblokApi } from "@storyblok/astro/client";
const { data } = await storyblokApi.get("cdn/stories/home", {
version: "draft",
});

Deprecated since 8.2.0

getLiveStory returns the updated story object whenever changes are made in the Storyblok Visual Editor, providing a real-time editing experience. Requires livePreview to be enabled.

await getLiveStory(Astro);

getPayload provides access to live preview data, including the story and any custom server-side data. Requires livePreview to be enabled.

It enables:

  • Access to live preview story data
  • Passing and reusing server-side data during preview
  • Avoiding unnecessary refetching of external resources
---
import { getPayload, useStoryblokApi } from "@storyblok/astro";
import StoryblokComponent from "@storyblok/astro/StoryblokComponent.astro";
import StoryblokServerData from "@storyblok/astro/StoryblokServerData.astro";
import { getUsers, type User } from "src/lib/getUsers";
interface ServerData {
users?: User[];
}
interface MyStory extends ISbStoryData {
content: { myField: string };
}
const { slug } = Astro.params;
const payload = await getPayload<ServerData, MyStory>({
locals: Astro.locals,
});
// Use live preview data or fallback to API
const story =
payload?.story ??
(
await storyblokApi.get(`cdn/stories/${slug || "home"}`, {
version: "draft",
resolve_relations: ["featured-articles.posts"],
})
).data.story;
const users = payload?.serverData?.users ?? (await getUsers());
---
<p>{users?.length} users loaded.</p>
<StoryblokComponent blok={story.content} />
<StoryblokServerData users={users} />

StoryblokServerData allows for providing pre-fetched server-side data to the live preview environment.

This is useful when an application depends on external data sources (for example, APIs, databases) that are slow or expensive to fetch repeatedly. The data is sent once and reused during live preview updates.

<StoryblokServerData users={users} />

This component automatically renders Storyblok blocks for corresponding Astro components registered using the components option of the storyblok Astro integration. It requires a blok property. Any additional passed properties are forwarded to the Astro component.

<StoryblokComponent blok={story.content} />

Use it to iterate over blocks fields as follows:

{
blok.nested_bloks?.map((currentBlok) => <StoryblokComponent blok={currentBlok} />);
}

This function connects Astro components to their Storyblok counterparts, adding HTML attributes that allow the Storyblok Bridge to work. See the @storyblok/js reference for further details.

---
import { storyblokEditable } from "@storyblok/astro";
const { blok } = Astro.props;
---
<section {...storyblokEditable(blok)}>{blok.title}</section>

Use these events to hook into the live preview rendering lifecycle:

  • storyblok-live-preview-updating: Triggered when a live preview update starts (before the DOM is updated).
  • storyblok-live-preview-updated: Triggered after the DOM has been updated.
<script>
document.addEventListener("storyblok-live-preview-updating", () => {
console.log("Storyblok live preview: DOM updating");
});
document.addEventListener("storyblok-live-preview-updated", () => {
console.log("Storyblok live preview: DOM updated");
});
</script>

Use the data-preserve-state attribute to prevent parts of the UI from re-rendering during live preview updates.

<div data-preserve-state></div>

Use the storyblok-live-preview meta tag to disable live preview for specific routes.

<meta name="storyblok-live-preview" content="disabled" />

Use the storyblok-live-preview-updating event to listen to live preview update events and control rendering behavior:

document.addEventListener("storyblok-live-preview-updating", (event) => {
event.preventDefault();
});

Used to render a rich text field from a Storyblok story.

<StoryblokRichText document={blok.richtext_field} />

The component accepts the following optional props:

  • components: Allows custom Astro components to be provided for supported rich text nodes and marks.
  • optimizeImage: Allows image optimization options to be configured for image nodes.

See the @storyblok/richtext reference for a complete list of supported nodes, marks, and customization options.

Custom components can be registered using the components prop. The key must match a supported rich text node or mark name.

import type { SbAstroRichTextComponentMap } from "@storyblok/astro";
const components: SbAstroRichTextComponentMap = {
heading: CustomHeading,
link: CustomLink,
table: CustomTable,
};

The components can then be passed to StoryblokRichText:

<StoryblokRichText document={blok.richtext_field} components={components} />

Marks receive their rendered child content through Astro’s <slot />.

---
import type { SbAstroRichTextProps } from "@storyblok/astro";
type Props = SbAstroRichTextProps<"link">;
const { attrs } = Astro.props;
---
<a href={attrs?.href ?? ""} target={attrs?.target ?? "_self"}>
<slot />
</a>

Nodes can use Astro’s <slot /> to pass through child content that has already been rendered.

---
import type { SbAstroRichTextProps } from "@storyblok/astro";
type Props = SbAstroRichTextProps<"heading">;
const { attrs } = Astro.props;
const Tag = `h${attrs?.level ?? 1}`;
---
<Tag class="custom-heading">
<slot />
</Tag>

Example: Custom table component (advanced node)

Section titled “Example: Custom table component (advanced node)”

For advanced use cases, nodes can access content and context to recursively render child nodes using StoryblokRichText.

The splitTableRows utility can be used to separate table header rows from body rows when creating custom table components.

---
import { splitTableRows, type SbAstroRichTextProps } from "@storyblok/astro";
import StoryblokRichText from "@storyblok/astro/StoryblokRichText.astro";
type Props = SbAstroRichTextProps<"table">;
const { content, context } = Astro.props;
const { headerRows, bodyRows } = splitTableRows(content);
---
<table class="custom-table">
{
headerRows && (
<thead>
<StoryblokRichText document={headerRows} {...context} />
</thead>
)
}
<tbody>
<StoryblokRichText document={bodyRows} {...context} />
</tbody>
</table>

The Astro SDK also exports the core rich text utilities from @storyblok/richtext. These can be useful when implementing advanced custom components, such as custom image or table rendering.

Available utilities include:

  • renderRichText: The core rich text rendering function.
  • buildStoryblokImage: Generates optimized Storyblok Image Service URLs.
  • splitTableRows: Splits table rows into headerRows and bodyRows.

Was this page helpful?

What went wrong?

This site uses reCAPTCHA and Google's Privacy Policy (opens in a new window) . Terms of Service (opens in a new window) apply.