Skip to navigation

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. To distribute the SDK internally instead, generate to the local file system (optionally self-hosted) and build a wheel with fern generate --package.

Already publishing to PyPI?

If you’re using token-based authentication, consider switching to OIDC (trusted publishing). See Migrating from token-based to OIDC publishing for the steps.

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 target in sdk-config.yml. See Generating an SDK (Python).

Configure SDK package settings

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

1

Add a unique package name

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

sdk-config.yml
targets:
- language: python
package:
packageName: your-package-name
2

Configure the client name

The generation.naming.clientName option controls the name of the generated client. This is the name customers use to import your SDK (from your_package import YourClientName).

sdk-config.yml
targets:
- language: python
package:
packageName: your-package-name
generation:
naming:
clientName: YourClientName # must be PascalCase
3

Add PyPI metadata (optional)

Package metadata appears on your package’s PyPI page. keywords support PyPI search and discovery, documentationUrl links to your package documentation, and homepage links to your project homepage. Metadata set in the top-level package block applies to every target.

sdk-config.yml
targets:
- language: python
package:
packageName: your-package-name
keywords: ["api", "sdk", "client"]
documentationUrl: "https://docs.yourcompany.com"
homepage: "https://yourcompany.com"
license:
type: MIT
generation:
naming:
clientName: YourClientName

Configure GitHub publishing

Fern can automatically publish your SDK to PyPI via GitHub Actions. Set output.delivery to github, configure your repository and publishing mode, and set output.publish.registry to pypi:

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 output.github reference for more information.

sdk-config.yml
targets:
- language: python
package:
packageName: your-package-name
generation:
naming:
clientName: YourClientName
output:
delivery: github
github:
repository: your-org/your-repository
mode: push # or "pull-request"
branch: your-branch-name # Required for mode: push
publish:
registry: pypi

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 sdk-config.yml

Add your PyPI token to output.publish. PyPI API tokens use __token__ as the username:

sdk-config.yml
targets:
- language: python
package:
packageName: your-package-name
generation:
naming:
clientName: YourClientName
output:
delivery: github
github:
repository: your-org/your-repository
mode: push
branch: your-branch-name
publish:
registry: pypi
username: __token__
password: ${PYPI_TOKEN}
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
name: Publish Python SDK
on:
workflow_dispatch:
inputs:
version:
description: "The version of the Python SDK that you would like to release"
required: true
type: string
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Install Fern CLI
run: npm install -g fern-api
- name: Release Python SDK
env:
FERN_TOKEN: ${{ secrets.FERN_TOKEN }}
PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }} # Only needed for token-based authentication
run: |
fern generate --target python --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 --target python --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.


Migrating from token-based to OIDC publishing

Migrating to OIDC publishing uses generators.yml. See Migrating from token-based to OIDC publishing in the Legacy docs.