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 guide assumes that you already have an initialized fern folder on your local machine. See Set up the fern folder for more details.

Set up your GitHub integration

  1. Create a new GitHub repository called company-typescript (or something similar) for your SDK, if you haven’t done so already.
  2. Install the Fern GitHub App: Select Configure, then scroll down to Repository Access. Select Only select repositories and in the dropdown select the repository for your SDK. Click Save.

Configure generators.yml

1

Run fern add <generator>

Navigate to your generators.yml on your local machine. Your generators.yml lives inside of your fern folder and contains all the configuration for your Fern generators.

Add a new generator to generators.yml:

$fern add fern-typescript-sdk --group ts-sdk

Once the command completes, you’ll see a new group created in your generators.yml:

1 groups:
2 ...
3 ts-sdk:
4 generators:
5 - name: fernapi/fern-typescript-sdk
6 version: 2.5.0
7 output:
8 location: local-file-system
9 path: ../sdks/typescript
2

Configure output location

Next, change the output location in generators.yml 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:
3 generators:
4 - name: fernapi/fern-typescript-sdk
5 version: 2.5.0
6 output:
7 location: npm
3

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
4

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
5

Add repository location

Add the path to your GitHub repository to 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: your-client-name
11 github:
12 repository: your-org/company-typescript

Set up npm publishing authentication

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
4

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: your-client-name
12 github:
13 repository: your-org/your-repository

Release your SDK to NPM

At this point, you’re ready to generate a release for your SDK.

1

Set npm environment variable

Set the NPM_TOKEN environment variable on your local machine:

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

Generate your release

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.