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 generators.yml

1

Configure output location

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

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. Update your package name if you haven’t done so already:

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';).

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
4

Add repository location

Add the path to your GitHub repository to generators.yml, if you haven’t already:

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

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 authentication

Use GitHub Actions to automatically publish new SDK versions to npm when you push code changes.

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

Allow GitHub to run workflows

Change your workflow permissions to allow GitHub to run workflows:

  1. Click on the Settings tab in your repository.
  2. Under the Code and automation section, navigate to Actions > General.
  3. Under Actions permissions, select Allow all actions and reusable workflows.
  4. Save your settings. Now GitHub can run the actions you configure.
4

Add token to generators.yml

Add token: ${NPM_TOKEN} to generators.yml to tell Fern to use the NPM_TOKEN environment variable (which you just configured in your GitHub repo) for authentication when publishing to the npm registry.

1groups:
2ts-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

When you regenerate your release, Fern will automatically create a workflow in your repository called .github/workflows/ci.yml that will automatically publish your release to npm. For an example, see Vapi’s npm publishing GitHub Action

1

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.

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
2

Set npm environment variable

Set the NPM_TOKEN environment variable on your local machine:

$export NPM_TOKEN=your-actual-npm-token

Release your SDK to npm

Regenerate your SDK and publish it on npm:

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

Local machine output will verify that the release is pushed to your repository and tagged with the version you specified. Log back into npm and navigate to Packages to see your new release.