Augment with custom code

Pro Feature

This feature is only available on paid plans. Please schedule a demo or email us to get started.

The Fern generated SDKs are designed to be extended with custom code. Your custom code can be used to add additional functionality to the SDK and will live in harmony with the generated code.

Custom logic

If you want to provide any logic in your SDK that goes beyond hitting the REST API, you can do so by leveraging .fernignore.

Simply add your custom files to the SDK repository and list them out in .fernignore. Fern won’t override any files that you add in .fernignore.

1

Create a new file src/helper.ts

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

Add src/helper.ts 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();

Custom SDK methods

Fern also allows you to add custom methods to the SDK itself (e.g. client.my_method() ).

While the specifics are slightly different for each language, the underlying principle is the same: extension. You can inherit the Fern generated client and add whatever methods you want.

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

1

Create a new file src/wrapper/MyClient.ts

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

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}
2

Export the extended client

Instead of exporting the generated client, export the extended client. To do this, you will need to update the index.ts file.

src/index.ts
1export { MyClient } from src/wrapper/MyClient; // instead of `src/Client`
3

Update .fernignore

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

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

Consume the method

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

1client.myHelper()

Custom dependencies

To add custom dependencies to your generated SDKs, you can update your generators.yml.

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