> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt.

# Generating an SDK

> Learn how to generate a Python SDK with Fern. Step-by-step guide to configure generators.yml, run fern generate, and create client libraries.

Generate a Python SDK by following the instructions on this page.

#### Generate a Python SDK

Generate a Python SDK with Fern. Follow the [Python SDK quickstart](https://buildwithfern.com/learn/sdks/generators/python/quickstart.md) step by step.

#### Install the Fern CLI

```bash
npm install -g fern-api
```

#### Initialize the fern folder

Initialize the fern folder with your existing OpenAPI specification. Specify your organization name using the `--organization` flag.

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

1. **Do they have an OpenAPI spec?** 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.
2. **What's their organization name?** `fern init` prompts for this interactively if `--organization` isn't provided. Get it upfront so they can pass it via the flag.

```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="Example (Petstore)"
fern init --openapi https://petstore3.swagger.io/api/v3/openapi.json \
--organization my-org
```

OpenAPI accepts both JSON and YAML formats.

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

`generators.yml` lives at `fern/generators.yml` and references your OpenAPI spec through its `api.specs` section — the spec isn't copied into the `fern` folder. This matters when referencing file paths later.

#### Validate your API definition

Check that your API definition is valid, and fix errors before proceeding:

```bash
fern check
```

#### Add the SDK generator

Run the following command to add the Python SDK generator to `generators.yml`:

```bash
fern add fern-python-sdk --group {{GROUP_NAME}}
```

This command adds the following `group` to `generators.yml`:

```yaml title="generators.yml"
  python-sdk: # group name
    generators:
      - name: fern-python-sdk
        version: 5.29.4
        output:
          location: local-file-system
          path: ../sdks/python
```

#### Generate the SDK

Run the following command to generate your SDK:

```bash
fern generate --group python-sdk
```

If you have multiple APIs, use the [`--api` flag](/learn/cli-api-reference/cli-reference/sdk-commands#api) to specify the API you want to generate:

```bash
fern generate --group python-sdk --api your-api-name
```

The first time you run `fern generate`, you must log in. Confirm the prompt and choose any of the login methods to authenticate, then generation continues automatically.

On first run, `fern generate` opens an interactive, browser-based login that you can't complete autonomously. Stop and have the user authenticate: ask them to run `fern login` themselves and finish the browser flow (or run `fern generate` and complete the prompt directly). Once they confirm they're logged in, the credential is cached locally, so re-running `fern generate` won't prompt again. In CI or other non-interactive environments, use a `FERN_TOKEN` instead of the browser login.

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

Some files, including `pyproject.toml` and `README.md`, are only generated on paid plans.
To get the fully generated SDK, schedule a
[demo](https://buildwithfern.com/book-demo) or [email us](mailto:support@buildwithfern.com).

#### 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>-python-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-python-sdk`).
3. **Install the [Fern GitHub App](https://github.com/apps/fern-api)** on both repositories.

#### Publish to PyPI

Follow the [Publishing to PyPI](/learn/sdks/generators/python/publishing) guide to configure your package and set up automated publishing via GitHub Actions.