Adding custom code

View as Markdown

This page covers how to add custom logic, methods, and dependencies to your TypeScript SDK.

Adding custom logic

To get started adding custom code:

1

Create a new file and add your custom logic

src/helper.ts
1export function myHelper(): void {
2 return console.log("Hello world!");
3}
2

Add your file to .fernignore

.fernignore
1# Specify files that shouldn't be modified by Fern
2
3src/helper.ts
3

Consume the helper

Now your users can consume the helper function by importing it from the SDK.

1import { myHelper } from "sdk/helper";
2
3myHelper();

By default, the TypeScript generator only auto-generates exports entries in package.json for your API definition. If you want consumers to import your custom file via a dedicated subpath (e.g. import { myHelper } from "@acme/sdk/helper"), you must register it manually using packageJson.exports in generators.yml:

generators.yml
1- name: fernapi/fern-typescript-sdk
2 version: 3.53.7
3 config:
4 packageJson:
5 exports:
6 "./helper": # consumer import path — not the source file path
7 types: "./dist/cjs/helper.d.ts"
8 import:
9 types: "./dist/esm/helper.d.mts"
10 default: "./dist/esm/helper.mjs"
11 require:
12 types: "./dist/cjs/helper.d.ts"
13 default: "./dist/cjs/helper.js"

The export key (e.g., "./helper") determines the import path your users will use and doesn’t need to match the source file path. These entries are merged into the generated exports, so they persist across regenerations. Since Node.js enforces the exports map, custom files aren’t importable via a subpath until you add them here.

Custom SDK methods

Fern also allows you to add custom methods to the SDK itself (e.g. client.my_method() ) by inheriting the Fern generated client and then extending it.

See an example from Flatfile using this process in their TypeScript SDK

1

Import and extend the generated client

First, import the Fern generated client from ../client and alias it to FernClient. Next, extend FernClient and add whatever methods you want.

src/wrapper/MyClient.ts
1import { MyClient as FernClient } from "../client"; // alias the Fern generated client
2
3export class MyClient extends FernClient { // extend the Fern generated client
4
5 public myHelper(): void {
6 console.log("Hello world!");
7 }
8
9}

See an example from Flatfile doing this in their FlatfileClient

2

Export the extended client

Update your index.ts file to export the extended client instead of the generated client.

src/index.ts
1export { MyClient } from "src/wrapper/MyClient"; // instead of "src/client"

See an example index.ts from Flatfile

3

Update .fernignore

Add both the wrapper directory and index.ts to .fernignore.

.fernignore
1+ src/wrapper
2+ src/index.ts

See an example .fernignore from Flatfile

4

Consume the method

Now your users can consume the helper function by importing it from the SDK.

1client.myHelper()

Custom dependencies

Pro and Enterprise feature

This feature is available only for the Pro and Enterprise plans. To get started, reach out to support@buildwithfern.com.

To add packages that your custom code requires, use the extraDependencies and extraDevDependencies options in your generators.yml.

generators.yml
1- name: fernapi/fern-typescript-sdk
2 version: 3.53.7
3 config:
4 extraDependencies:
5 lodash-es: '1.0.0'
6 extraDevDependencies:
7 "@types/lodash-es": '1.0.0'