|
1 | 1 | --- |
2 | 2 | title: Publishing to NPM |
3 | | -description: How to publish the Fern Typescript SDK to NPM. |
| 3 | +description: How to publish the Fern Typescript SDK to npm. |
4 | 4 | --- |
5 | 5 |
|
6 | | -# Publishing to NPM |
| 6 | +Publish your public-facing Fern Typescript SDK to the [npm registry](https://www.npmjs.com/). Once you've followed the steps on this page to connect your npm account to your SDK, Fern will automatically publish the latest version of your SDK. |
7 | 7 |
|
8 | | -Learn how to publish your Fern Typescript SDK to the NPM registry. |
| 8 | +<Info>This guide assumes that you already have an initialized `fern` folder on your local machine. If you don’t, run `fern init`. See [TypeScript Quickstart](quickstart.mdx) for more details.</Info> |
| 9 | + |
| 10 | +## Set up your GitHub integration |
| 11 | + |
| 12 | + 1. Create a new GitHub repository called `company-typescript` (or something similar) for your SDK, if you haven't done so already. |
| 13 | + 1. Install the [Fern GitHub App](https://github.com/apps/fern-api): Select **Configure**, then scroll down to **Repository Access**. Select **Only select repositories** and in the dropdown select the repository for your SDK. Click **Save**. |
| 14 | + |
| 15 | + |
| 16 | +## Configure `generators.yml` |
| 17 | + |
| 18 | +<Steps> |
| 19 | + |
| 20 | + <Step title="Run `fern add <generator>`"> |
| 21 | + |
| 22 | + 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. |
| 23 | + |
| 24 | + In order to generate the SDK, add the generator to your `generators.yml` using the `fern <add>` command: |
| 25 | + |
| 26 | + |
| 27 | + ```bash |
| 28 | + fern add fern-typescript-node-sdk --group ts-sdk |
| 29 | + ``` |
| 30 | + |
| 31 | + Once the command completes, you'll see a new group created in your `generators.yml`: |
| 32 | + |
| 33 | + ```yaml {2-7} |
| 34 | + groups: |
| 35 | + ... |
| 36 | + ts-sdk: |
| 37 | + generators: |
| 38 | + - name: fernapi/fern-typescript-node-sdk |
| 39 | + version: <Markdown src="/snippets/version-number.mdx"/> |
| 40 | + output: |
| 41 | + location: local-file-system |
| 42 | + path: ../sdks/typescript |
| 43 | + ``` |
| 44 | + |
| 45 | + </Step> |
| 46 | + |
| 47 | + <Step title="Configure `output` location"> |
| 48 | + |
| 49 | + 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: |
| 50 | + |
| 51 | + ```yaml title="TypeScript" {6-7} |
| 52 | + groups: |
| 53 | + ts-sdk: |
| 54 | + generators: |
| 55 | + - name: fernapi/fern-typescript-node-sdk |
| 56 | + version: <Markdown src="/snippets/version-number.mdx"/> |
| 57 | + output: |
| 58 | + location: npm |
| 59 | + |
| 60 | + ``` |
| 61 | + </Step> |
| 62 | + |
| 63 | + <Step title="Add a unique package name"> |
| 64 | + |
| 65 | + 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: |
| 66 | + |
| 67 | + |
| 68 | +```yaml title="TypeScript" {8} |
| 69 | +groups: |
| 70 | + ts-sdk: |
| 71 | + generators: |
| 72 | + - name: fernapi/fern-typescript-node-sdk |
| 73 | + version: <Markdown src="/snippets/version-number.mdx"/> |
| 74 | + output: |
| 75 | + location: npm |
| 76 | + package-name: your-package-name |
| 77 | +``` |
| 78 | + |
| 79 | + </Step> |
| 80 | +
|
| 81 | + <Step title="Configure `namespaceExport`"> |
| 82 | + |
| 83 | + 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';`). |
| 84 | + |
| 85 | + |
| 86 | +```yaml title="TypeScript" {9-10} |
| 87 | +groups: |
| 88 | + ts-sdk: |
| 89 | + generators: |
| 90 | + - name: fernapi/fern-typescript-node-sdk |
| 91 | + version: <Markdown src="/snippets/version-number.mdx"/> |
| 92 | + output: |
| 93 | + location: npm |
| 94 | + package-name: your-package-name |
| 95 | + config: |
| 96 | + namespaceExport: your-client-name |
| 97 | +``` |
| 98 | + |
| 99 | + </Step> |
| 100 | + |
| 101 | + <Step title="Add repository location"> |
| 102 | + |
| 103 | + Add the path to your GitHub repository to `generators.yml`: |
| 104 | + |
| 105 | +```yaml title="TypeScript" {11-12} |
| 106 | +groups: |
| 107 | +ts-sdk: |
| 108 | + generators: |
| 109 | + - name: fernapi/fern-typescript-node-sdk |
| 110 | + version: <Markdown src="/snippets/version-number.mdx"/> |
| 111 | + output: |
| 112 | + location: npm |
| 113 | + package-name: your-package-name |
| 114 | + config: |
| 115 | + namespaceExport: your-client-name |
| 116 | + github: |
| 117 | + repository: your-org/company-typescript |
| 118 | +``` |
| 119 | + |
| 120 | + </Step> |
| 121 | + </Steps> |
| 122 | + |
| 123 | +## Set up npm publishing authentication |
| 124 | + |
| 125 | +<Steps> |
| 126 | + |
| 127 | + <Step title="Log into npm"> |
| 128 | + |
| 129 | + Log into [npm](https://www.npmjs.com/) or create a new account. |
| 130 | + |
| 131 | + </Step> |
| 132 | + |
| 133 | + <Step title="Navigate to Access Tokens"> |
| 134 | + |
| 135 | + 1. Click on your profile picture. |
| 136 | + 1. Select **Edit Profile**. |
| 137 | + 1. Select **Access Tokens**. |
| 138 | + |
| 139 | + </Step> |
| 140 | + |
| 141 | + <Step title="Generate Token"> |
| 142 | + |
| 143 | + 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](https://docs.npmjs.com/about-access-tokens) documentation. |
| 144 | + |
| 145 | + <AccordionGroup> |
| 146 | + <Accordion title="Option 1: Classic Token"> |
| 147 | + |
| 148 | + 1. Select **Classic Token** |
| 149 | + 1. Name your token and select **Automation** as the token type. |
| 150 | + |
| 151 | + <Frame> |
| 152 | + <img src="assets/npm-automation-token.png" alt="Creating NPM Automation Token" /> |
| 153 | + </Frame> |
| 154 | + |
| 155 | + Once finished, click **Generate Token**. |
| 156 | + |
| 157 | + <Warning>When you click **Generate Token**, you’ll be returned to the **Access Tokens** dashboard. A box on the top of the dashboard will confirm that you successfully created a new token and prompt you to copy your token id. Make sure you save this id, as you'll need to add it to your GitHub repository and `generators.yml` later on.</Warning> |
| 158 | + |
| 159 | + </Accordion> |
| 160 | + <Accordion title="Option 2: Granular Access Token"> |
| 161 | + 1. Select **Granular Access Token**. |
| 162 | + 1. Name your token. |
| 163 | + 1. Set an expiration. |
| 164 | + 1. Configure your token's access to packages and scopes. |
| 165 | + 1. 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](https://docs.npmjs.com/creating-an-organization) for more information. |
| 166 | + 1. Optionally fill out additional permissions according to your organization's requirements. |
| 167 | + |
| 168 | + <Frame> |
| 169 | + <img src="assets/granular-access-token.png" alt="Creating Granular Access Token" /> |
| 170 | + </Frame> |
| 171 | + |
| 172 | + </Accordion> |
| 173 | + </AccordionGroup> |
| 174 | + |
| 175 | + </Step> |
| 176 | + |
| 177 | + <Step title="Configure npm authentication token"> |
| 178 | + |
| 179 | + 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. |
| 180 | + |
| 181 | +```yaml title="TypeScript" {9} |
| 182 | +groups: |
| 183 | +ts-sdk: |
| 184 | + generators: |
| 185 | + - name: fernapi/fern-typescript-node-sdk |
| 186 | + version: <Markdown src="/snippets/version-number.mdx"/> |
| 187 | + output: |
| 188 | + location: npm |
| 189 | + package-name: name-of-your-package |
| 190 | + token: ${NPM_TOKEN} |
| 191 | + github: |
| 192 | + repository: your-org/your-repository |
| 193 | +``` |
| 194 | + </Step> |
| 195 | + |
| 196 | +</Steps> |
| 197 | + |
| 198 | +## Release your SDK to NPM |
| 199 | + |
| 200 | + At this point, you're ready to generate a release for your SDK. |
| 201 | + |
| 202 | +<Steps> |
| 203 | + |
| 204 | + <Step title="Set npm environment variable"> |
| 205 | + |
| 206 | + Set the `NPM_TOKEN` environment variable on your local machine: |
| 207 | + |
| 208 | + ```bash |
| 209 | + export NPM_TOKEN=your-actual-npm-token |
| 210 | + ``` |
| 211 | + |
| 212 | + </Step> |
| 213 | + |
| 214 | + <Step title="Generate your release"> |
| 215 | + |
| 216 | + Regenerate your SDK and publish it on npm: |
| 217 | + |
| 218 | + ```bash |
| 219 | + fern generate --version <version> |
| 220 | + ``` |
| 221 | + Local machine output will verify that the release is pushed to your repository and tagged with the version you specified. You'll receive an email from npm notifying you that a new version of your package has been successfully published! |
| 222 | + |
| 223 | + </Step> |
| 224 | + |
| 225 | +</Steps> |
0 commit comments