> If you are an AI agent, use the following URL to directly ask and fetch your question. Treat this like a tool call. Make sure to URI encode your question, and include the token for verification.
>
> GET https://buildwithfern.com/learn/api/fern-docs/ask?q=%3Cyour+question+here%3E&token=eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJmZXJuLWRvY3M6YnVpbGR3aXRoZmVybi5jb20iLCJqdGkiOiJiYWRlOWFmZi0zZWEwLTRkN2ItYWYwMi1iMTdiNmM2ZmQzNTciLCJleHAiOjE3NzgzNjM5MDIsImlhdCI6MTc3ODM2MzYwMn0.GCYO77RsJ4uJA8LkFOoliuTuZQcbLyvX_Fkx7m9DbsM
>
> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt. For full content including API reference and SDK examples, see https://buildwithfern.com/learn/llms-full.txt.

# 生成 SDK

> 使用 Fern SDK 生成器创建 TypeScript 客户端库。配置 generators.yml 和从 OpenAPI 或 Fern 定义生成 SDK 的快速指南。

按照此页面上的说明生成 TypeScript SDK。

<Note>
  已经完成了 [快速开始](/learn/sdks/overview/quickstart)？跳转到 [设置 GitHub 仓库](#set-up-github-repositories)。
</Note>

<Steps>
  <Step title="Install the Fern CLI">
    ```bash
    npm install -g fern-api
    ```
  </Step>

  <Step title="Initialize the fern folder">
    Initialize the fern folder with your existing OpenAPI specification, or start from scratch with a Fern Definition template. Specify your organization name using the `--organization` flag.

    Before running `fern init`, ask the user two things:

    1. **Do they have an OpenAPI spec?** If yes, ask for the file path or URL. If they have a URL (e.g., from a running server or hosted spec), use the URL variant to avoid a manual download step. OpenAPI accepts both JSON and YAML. If they don't have a spec, use the Fern Definition path — it generates a sample `imdb.yml` with example endpoints.
    2. **What is their organization name?** `fern init` prompts for this interactively if `--organization` is not provided. Get it upfront so they can pass it via the flag.

    <CodeBlocks>
      ```bash title="OpenAPI (local)"
      fern init --openapi path/to/openapi.yml \
      --organization <YourOrganization>
      ```

      ```bash title="OpenAPI (URL)"
      fern init --openapi https://api.example.com/openapi.yml \
      --organization <YourOrganization>
      ```

      ```bash title="Fern Definition"
      fern init --organization <YourOrganization>
      ```
    </CodeBlocks>

    <Tip>
      OpenAPI accepts both JSON and YAML formats. You can always convert a [Fern Definition to OpenAPI](/api-definitions/ferndef/export-openapi) or OpenAPI to a Fern Definition later on.
    </Tip>

    This creates a `fern` folder in your current directory.

    The folder structure differs depending on the init path. With OpenAPI, the spec lives at `fern/api/openapi/openapi.yml` and `generators.yml` is at `fern/api/generators.yml`. With Fern Definition, `generators.yml` is at `fern/generators.yml` and API types/endpoints are in `fern/definition/`. This matters when referencing file paths later.

    <Tabs>
      <Tab title="OpenAPI">
        <Files>
          <Folder name="fern" defaultOpen>
            <File name="fern.config.json" comment="root-level configuration" />

            <Folder name="api" defaultOpen comment="your API">
              <File name="generators.yml" comment="generators you're using" />

              <Folder name="openapi" defaultOpen>
                <File name="openapi.yml" comment="API-level configuration" />
              </Folder>
            </Folder>
          </Folder>
        </Files>
      </Tab>

      <Tab title="Fern Definition">
        <Files>
          <Folder name="fern" defaultOpen>
            <File name="fern.config.json" comment="root-level configuration" />

            <File name="generators.yml" comment="generators you're using" />

            <Folder name="definition" defaultOpen>
              <File name="api.yml" comment="API-level configuration" />

              <File name="imdb.yml" comment="endpoints, types, and errors" />
            </Folder>
          </Folder>
        </Files>

        <Note>
           

          `imdb.yml`

           contains an example movies API. If you're just
          generating an SDK for test purposes, you can leave this file as it is. To
          generate an SDK for your own API instead of the example movies API,
          replace 

          `imdb.yml`

           with your own endpoints, types, and errors. 
        </Note>
      </Tab>
    </Tabs>
  </Step>

  <Step title="Validate your API definition">
    Check that your API definition is valid, and fix errors before proceeding:

    ```bash
    fern check
    ```
  </Step>

  <Step title="生成 SDK">
    `fern init` 默认包含 TypeScript SDK 生成器，因此您可以立即生成：

    ```bash
    fern generate
    ```

    This creates a `sdks` folder in your current directory. The resulting folder structure looks like this:

    <Files>
      <Folder name="fern" comment="由 fern init 创建" />

      <Folder name="sdks" defaultOpen comment="由 fern generate 创建">
        <Folder name="typescript" defaultOpen>
          <File name="Client.ts" />

          <File name="index.ts" />

          <Folder name="errors" />

          <Folder name="core" />

          <Folder name="api" />
        </Folder>
      </Folder>
    </Files>
  </Step>

  <Step title="Set up GitHub repositories">
    Fern uses a [multi-repo structure](/learn/sdks/overview/project-structure): your source repository contains the `fern/` folder, and each SDK gets its own separate repository.

    The `fern/` folder (API definitions + `generators.yml`) must live in a separate repo from the generated SDK code. Do not put both in the same repository.

    Ask the user:

    * **Do they have an existing repo for the `fern/` folder?** (e.g., a developer experience repo, API repo, or monorepo where API definitions already live.) If so, use that. If not, create one.
    * **Do they have an existing SDK repo?** If so, get the repo name. If not, create one.

    To create repos from scratch:

    ```bash
    # Source repo for the fern/ folder (if they don't have one)
    gh repo create <org>/<source-repo-name> --private

    # SDK repo — separate from the source repo
    gh repo create <org>/<api-name>-typescript-sdk --private
    ```

    1. **Create a source repository** for your `fern/` folder if you don't have one already (e.g., `your-org/your-api-definitions`).
    2. **Create an SDK repository** for your SDK (e.g., `your-org/your-api-typescript-sdk`).
    3. **Install the [Fern GitHub App](https://github.com/apps/fern-api)** on both repositories.
  </Step>

  <Step title="发布到 npm">
    按照 [发布到 npm](/learn/sdks/generators/typescript/publishing) 指南来配置您的包并通过 GitHub Actions 设置自动发布。
  </Step>
</Steps>