Publishing to npm

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

Versioned package published on npm

This page assumes that you have:

Configure SDK package settings

You’ll need to update your generators.yml file to configure the package name, output location, and client naming for npm publishing. Your generators.yml should live in your source repository (or on your local machine), not the repository that contains your TypeScript SDK code.

1

Configure output location

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

generators.yml
1groups:
2 ts-sdk: # Group name for your TypeScript SDK
3 generators:
4 - name: fernapi/fern-typescript-sdk
5 version: 2.5.0
6 output:
7 location: npm
2

Add a unique package name

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

generators.yml
1groups:
2 ts-sdk:
3 generators:
4 - name: fernapi/fern-typescript-sdk
5 version: 2.5.0
6 output:
7 location: npm
8 package-name: your-package-name
3

Configure namespaceExport

The namespaceExport 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 ts-sdk:
3 generators:
4 - name: fernapi/fern-typescript-sdk
5 version: 2.5.0
6 output:
7 location: npm
8 package-name: your-package-name
9 config:
10 namespaceExport: YourClientName # must be PascalCase

Generate an npm token

1

Log into npm

Log into npm or create a new account.

3

Generate Token

Click on Generate New Token, then choose the appropriate token type.

For more information on access tokens and which type to choose, see npm’s About access tokens documentation.
  1. Select Classic Token
  2. Name your token and select Automation as the token type.
  3. Click Generate Token.
Save your new token – it won’t be displayed after you leave the page.
Creating NPM Automation Token
  1. Select Granular Access Token.
  2. Name your token.
  3. Set an expiration.
  4. Configure your token’s access to packages and scopes.
  5. Configure your token’s access to organizations. In order to fill this out, you must have at least one organization already configured in npm. See Creating an organization for more information.
  6. Optionally fill out additional permissions according to your organization’s requirements.
  7. Click Generate Token.
Save your new token – it won’t be displayed after you leave the page.
Creating Granular Access Token

Configure npm publication

1

Add repository location

Add the path to the GitHub repository containing your TypeScript SDK:

generators.yml
1groups:
2 ts-sdk:
3 generators:
4 - name: fernapi/fern-typescript-sdk
5 version: 2.5.0
6 output:
7 location: npm
8 package-name: your-package-name
9 config:
10 namespaceExport: YourClientName
11 github:
12 repository: your-org/company-typescript
2

Configure npm authentication token

Add token: ${NPM_TOKEN} to generators.yml to tell Fern to use the NPM_TOKEN environment variable for authentication when publishing to the npm registry.

generators.yml
1groups:
2 ts-sdk:
3 generators:
4 - name: fernapi/fern-typescript-sdk
5 version: 2.5.0
6 output:
7 location: npm
8 package-name: name-of-your-package
9 token: ${NPM_TOKEN}
10 config:
11 namespaceExport: YourClientName
12 github:
13 repository: your-org/your-repository
3

Choose your publishing mode

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

  • mode: release (default): Fern generates code, commits to main, and tags a release automatically
  • mode: pull-request: 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 ts-sdk:
3 generators:
4 - name: fernapi/fern-typescript-sdk
5 version: 2.5.0
6 output:
7 location: npm
8 package-name: name-of-your-package
9 token: ${NPM_TOKEN}
10 config:
11 namespaceExport: YourClientName
12 github:
13 repository: your-org/your-repository
14 mode: push
15 branch: your-branch-name # Required for mode: push

Publish your SDK

Decide how you want to publish your SDK to npm. You can use GitHub workflows for automated releases or publish directly via the CLI.

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

1

Set up authentication

Open your source repository in GitHub. Click on the Settings tab. Then, under the Security section, open Secrets and variables > Actions.

Adding GitHub Repository Secret

You can also use the url https://github.com/<your-repo>/settings/secrets/actions.

2

Add secret for your npm Token

  1. Select New repository secret.
  2. Name your secret NPM_TOKEN.
  3. Add the corresponding token you generated above.
  4. Click Add secret.
NPM_TOKEN secret
3

Add secret for your Fern Token

  1. Select New repository secret.
  2. Name your secret FERN_TOKEN.
  3. Add your Fern token. If you don’t already have one, generate one by running fern token. By default, the fern_token is generated for the organization listed in fern.config.json.
  4. Click Add secret.
4

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 TypeScript SDK
2
3on:
4 workflow_dispatch:
5 inputs:
6 version:
7 description: "The version of the TypeScript 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 TypeScript SDK
22 env:
23 FERN_TOKEN: ${{ secrets.FERN_TOKEN }}
24 NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
25 run: |
26 fern generate --group ts-sdk --version ${{ inputs.version }} --log-level debug

You can alternatively configure your workflow to execute on: [push]. See Vapi’s npm publishing GitHub Action for an example.

5

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 TS 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 npm and navigating to Packages.

1

Set npm environment variable

Set the NPM_TOKEN environment variable on your local machine:

$export NPM_TOKEN=your-actual-npm-token
2

Regenerate and release your SDK

Regenerate your SDK, specifying the version:

$fern generate --group ts-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 npm and navigating to Packages.