> This page is for SDKs, version Legacy.
> For other versions, use one of these documentation indexes:
> - Current (default): https://buildwithfern.com/learn/sdks/current/llms.txt
> - Legacy: https://buildwithfern.com/learn/sdks/legacy/llms.txt

> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt.

# Adding custom code

> Learn how to add custom logic, methods, and dependencies to your TypeScript SDK with Fern. Extend generated clients easily.

> **You're viewing the legacy SDK docs**
>
> These pages cover SDK generation configured with `generators.yml`. New projects use `sdk-config.yml`, so see [the Current version of this page](/learn/sdks/generators/typescript/custom-code) for up-to-date guidance. To move an existing project, follow the [SDK Config migration guide](/learn/sdks/legacy/generators/migration-guides/sdk-config), which walks through `fern sdk migrate`, and review the [`sdk-config.yml` reference](/learn/sdks/reference/sdk-config).

> **Enterprise feature**
>
> This feature is available only for the [Enterprise plan](https://buildwithfern.com/pricing). To get started, reach out to [support@buildwithfern.com](mailto:support@buildwithfern.com).

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

## Adding custom logic

To get started adding custom code:

> **Note**
>
> These steps cover adding a new custom file to the SDK. To preserve line-level edits to a generated file, use [Replay](/learn/sdks/legacy/overview/custom-code#replay) instead.

### Create a new file and add your custom logic

**`src/helper.ts`**

```typescript title="src/helper.ts"
export function myHelper(): void {
    return console.log("Hello world!");
}
```

### Add your file to `.fernignore`

**`.fernignore`**

```yaml {3} title=".fernignore"
# Specify files that shouldn't be modified by Fern

src/helper.ts
```

### Consume the helper

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

```typescript
import { myHelper } from "sdk/helper";

myHelper();
```

#### Advanced: Custom import paths

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`](/learn/sdks/legacy/generators/typescript/configuration#packagejson) in `generators.yml`:

**`generators.yml`**

```yaml {5-14} title="generators.yml"
- name: fern-typescript-sdk
  version: 3.107.3
  config:
    packageJson:
      exports:
        "./helper": # consumer import path — not the source file path
          types: "./dist/cjs/helper.d.ts"
          import:
            types: "./dist/esm/helper.d.mts"
            default: "./dist/esm/helper.mjs"
          require:
            types: "./dist/cjs/helper.d.ts"
            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](https://nodejs.org/api/packages.html#package-entry-points), 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.

> **Note**
>
> See an example from Flatfile using this process in their [TypeScript SDK](https://github.com/FlatFilers/flatfile-node)

### 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`**

```typescript title="src/wrapper/MyClient.ts"
import { MyClient as FernClient } from "../client"; // alias the Fern generated client

export class MyClient extends FernClient { // extend the Fern generated client

  public myHelper(): void {
    console.log("Hello world!");
  }

}
```

> **Note**
>
> See an example from Flatfile doing this in their [FlatfileClient](https://github.com/FlatFilers/flatfile-node/blob/main/src/wrapper/FlatfileClient.ts)

### Export the extended client

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

**`src/index.ts`**

```typescript title="src/index.ts"
export { MyClient } from "src/wrapper/MyClient"; // instead of "src/client"
```

> **Note**
>
> See an example [index.ts](https://github.com/FlatFilers/flatfile-node/blob/main/src/index.ts) from Flatfile

### Update `.fernignore`

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

**`.fernignore`**

```diff title=".fernignore"
+ src/wrapper
+ src/index.ts
```

> **Note**
>
> See an example [.fernignore](https://github.com/FlatFilers/flatfile-node/blob/main/.fernignore) from Flatfile

### Consume the method

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

```typescript
client.myHelper()
```

## Custom dependencies

To add packages that your custom code requires, use the [`extraDependencies`](/learn/sdks/legacy/generators/typescript/configuration#extradependencies) and [`extraDevDependencies`](/learn/sdks/legacy/generators/typescript/configuration#extradevdependencies) options in your `generators.yml`.

**`generators.yml`**

```yaml {4-7} title="generators.yml"
- name: fern-typescript-sdk
  version: 3.107.3
  config:
    extraDependencies:
      lodash-es: '1.0.0'
    extraDevDependencies:
      "@types/lodash-es": '1.0.0'
```