---
title: schema init
description: Fetches the current schema of a Storyblok space and writes it as TypeScript files using @storyblok/schema helpers.
url: https://www.storyblok.com/docs/tooling/cli/schema/init
---

# schema init

Fetches the current schema of a Storyblok space and writes it as TypeScript files using [`@storyblok/schema`](/docs/libraries/js/schema) helpers. `schema init` is a one-time setup step for adopting an existing space in code.

The command generates the following files:

-   One file per block, in subdirectories that follow the component group hierarchy
-   One file per datasource
-   A `folders.ts` file for all component groups
-   A `schema.ts` file in the root that combines everything into a single exported `schema` object.

```text
.storyblok/
└── schema/
    ├── blocks/
    │   ├── <block-name>.ts
    │   └── <group-slug>/
    │       └── <block-name>.ts
    ├── datasources/
    │   └── <datasource-slug>.ts
    ├── folders.ts
    └── schema.ts
```

After initialization, treat the local files as the source of truth and use [`schema push`](/docs/tooling/cli/schema/push) for further changes.

## Prerequisites

The [`@storyblok/schema`](/docs/libraries/js/schema) package must be installed in the target project: `npm install @storyblok/schema`.

## Usage

```bash
storyblok schema init [flags]
```

## Flags

| Flag | Type | Description |
| --- | --- | --- |
| `--space`, `-s` | integer | _Required._ The ID of the Storyblok space to read the schema from. |
| `--out-dir` | string | _Optional._ Output directory for generated files. Defaults to `.storyblok/schema`. |

## Examples

The following examples assume you defined a `space` in the configuration file.

```bash
# Bootstrap a local TypeScript schema from a space
storyblok schema init

# Bootstrap into a custom output directory
storyblok schema init --out-dir src/schema
```

## Pagination

-   [Previous: prune](/docs/tooling/cli/reports/prune)
-   [Next: push](/docs/tooling/cli/schema/push)
