Publishing to PyPI

View as Markdown

Publish your public-facing Fern Python SDK to the PyPI registry. After following the steps on this page, you’ll have a versioned package published on PyPI.

Versioned package published on PyPI

This page assumes that you have:

  • An initialized fern folder, a GitHub repository for your Python SDK, and a Python generator group in generators.yml. See Generating an SDK (Python).

Configure SDK package settings

Update your generators.yml file to configure the package name, output location, and client naming for PyPI publishing. Your generators.yml should live in your source repository (or on your local machine), not the repository that contains your Python SDK code.

1

Configure output location

In the group for your Python SDK, change the output location in from local-file-system (the default) to pypi to indicate that Fern should publish your package directly to the PyPi registry:

generators.yml
1groups:
2 python-sdk:
3 generators:
4 - name: fern-python-sdk
5 version: 5.14.15
6 output:
7 location: pypi
2

Add a unique package name

Your package name must be unique in the PyPI repository, otherwise publishing your SDK to PyPI will fail.

generators.yml
1groups:
2 python-sdk:
3 generators:
4 - name: fern-python-sdk
5 version: 5.14.15
6 output:
7 location: pypi
8 package-name: your-package-name
3

Configure client-class-name

The client-class-name option controls the name of the generated client. This is the name customers use to import your SDK (import { your-client-name } from 'your-package-name';).

generators.yml
1groups:
2 python-sdk:
3 generators:
4 - name: fern-python-sdk
5 version: 5.14.15
6 output:
7 location: pypi
8 package-name: your-package-name
9 config:
10 client_class_name: YourClientName # must be PascalCase
4

Add PyPI metadata (optional)

You can add publishing metadata to your PyPI package to improve discoverability and provide additional information to users. This metadata appears on your package’s PyPI page and includes keywords for PyPI search and discovery, documentation-link for your package documentation, and homepage-link for your project homepage.

You can additionally add general metadata for the SDK (description, contact email, author, license, etc.) at the individual SDK level or globally for all SDKs.

generators.yml
1groups:
2 python-sdk:
3 generators:
4 - name: fern-python-sdk
5 version: 5.14.15
6 output:
7 location: pypi
8 package-name: your-package-name
9 metadata: # Publishing metadata
10 keywords: ["api", "sdk", "client"]
11 documentation-link: "https://docs.yourcompany.com"
12 homepage-link: "https://yourcompany.com"
13 metadata: # General SDK metadata
14 license: MIT
15 config:
16 client_class_name: YourClientName

Configure GitHub publishing

Fern can automatically publish your SDK to PyPI via GitHub Actions. Configure your GitHub repository and publishing mode:

Optionally set the mode to control how Fern handles SDK publishing:

  • mode: release (default): Fern generates code, commits to the default branch (or the branch you specify), and tags a release automatically
  • mode: pull-request (recommended): Fern generates code and creates a PR for you to review before release
  • mode: push: Fern generates code and pushes to a branch you specify for you to review before release

You can also configure other settings, like the reviewers or license. Refer to the full github (generators.yml) reference for more information.

generators.yml
1groups:
2 python-sdk:
3 generators:
4 - name: fern-python-sdk
5 version: 5.14.15
6 output:
7 location: pypi
8 package-name: your-package-name
9 config:
10 client_class_name: YourClientName
11 github:
12 repository: your-org/your-repository
13 mode: push # or "pull-request"
14 branch: your-branch-name # Required for mode: push

Configure authentication

Choose how you want to authenticate with PyPI when publishing. OpenID Connect (OIDC) authentication is recommended because it removes the need to manage long-lived API tokens.

1

Generate a PyPI token

  1. Log into PyPI or create a new account
  2. Click on your profile picture and select Account settings
  3. Scroll down to API tokens and click Add API token
  4. Name your token and set the scope to the relevant projects
  5. Click Create token
Creating a New API Token
Save your new token - it won’t be displayed after you leave the page.
2

Add token to generators.yml

Add token: ${PYPI_TOKEN} to the output section:

generators.yml
1groups:
2 python-sdk:
3 generators:
4 - name: fern-python-sdk
5 version: 5.14.15
6 output:
7 location: pypi
8 package-name: your-package-name
9 token: ${PYPI_TOKEN}
10 config:
11 client_class_name: YourClientName
12 github:
13 repository: your-org/your-repository
3

Add PYPI_TOKEN as a GitHub Actions secret

  1. Open your repository on GitHub and go to Settings
  2. Navigate to Secrets and variables > Actions
  3. Click New repository secret
  4. Name it PYPI_TOKEN and paste your PyPI token
  5. Click Add secret
PYPI_TOKEN secret

Publish your SDK

Your SDK will automatically be published to PyPI when you create a GitHub release with a version tag:

  1. Create a GitHub release with a version tag (for example, v1.0.0)
  2. The CI workflow will run automatically and publish to PyPI
  3. View your package on PyPI to confirm the version

Set up a release workflow via GitHub Actions so you can trigger new SDK releases directly from your source repository.

If you prefer to trigger publishes manually from your source repository, set up a release workflow via GitHub Actions.

1

Add secret for your Fern API key

  1. Open your Fern repository on GitHub and go to Settings > Secrets and variables > Actions
  2. Select New repository secret
  3. Name your secret FERN_TOKEN
  4. Add your Fern API key. If you don’t already have one, generate one by running fern token. By default, the API key is generated for the organization listed in fern.config.json.
  5. Click Add secret

If you use token-based authentication, also add a PYPI_TOKEN secret with your PyPI token.

2

Set up a new workflow

Set up a CI workflow that you can manually trigger from the GitHub UI. In your repository, navigate to Actions. Select New workflow, then Set up workflow yourself. Add a workflow that’s similar to this:

.github/workflows/publish.yml
1name: Publish Python SDK
2
3on:
4 workflow_dispatch:
5 inputs:
6 version:
7 description: "The version of the Python SDK that you would like to release"
8 required: true
9 type: string
10
11jobs:
12 release:
13 runs-on: ubuntu-latest
14 steps:
15 - name: Checkout repo
16 uses: actions/checkout@v4
17
18 - name: Install Fern CLI
19 run: npm install -g fern-api
20
21 - name: Release Python SDK
22 env:
23 FERN_TOKEN: ${{ secrets.FERN_TOKEN }}
24 PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }} # Only needed for token-based authentication
25 run: |
26 fern generate --group python-sdk --version ${{ inputs.version }} --log-level debug
3

Regenerate and release your SDK

Navigate to the Actions tab, select the workflow you just created, specify a version number, and click Run workflow. This regenerates your SDK.

Running Python publish workflow

The rest of the release process depends on your chosen mode:

  • Release mode (default): If you didn’t specify a mode or set mode: release, no further action is required. Fern automatically tags the new release with your specified version number and initiates the publishing workflow in your SDK repository.

  • Pull request or push mode: If you set mode: pull-request or mode: push, Fern creates a pull request or pushes to a branch respectively. Review and merge the PR (pull-request) or branch (push), then tag a new release to initiate the publishing workflow in your SDK repository.

Once the workflow completes, you can view your new release by logging into PyPi and navigating to Your projects.

1

Set PyPI environment variable

If you use token-based authentication, set the PYPI_TOKEN environment variable on your local machine:

$export PYPI_TOKEN=your-actual-pypi-token
2

Regenerate and release your SDK

Regenerate your SDK, specifying the version:

$fern generate --group python-sdk --version <version>

The rest of the release process depends on your chosen mode:

  • Release mode (default): If you didn’t specify a mode or set mode: release, no further action is required. Fern automatically tags the new release with your specified version number and initiates the publishing workflow in your SDK repository.

  • Pull request or push mode: If you set mode: pull-request or mode: push, Fern creates a pull request or pushes to a branch respectively. Review and merge the PR (pull-request) or branch (push), then tag a new release to initiate the publishing workflow in your SDK repository.

Once the workflow completes, you can view your new release by logging into PyPI and navigating to Your projects.