Adding custom code

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

This page explains how to configure custom logic using a .fernignore file, create custom SDK methods, and add additional dependencies to your SDK.

Adding custom logic

If you want your SDK to do more than just make basic API calls (like combining multiple calls, processing data, adding utilities), you can use .fernignore to protect your custom code from being overwritten during regeneration.

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.

To get started adding custom code:

1

Create a new file and add your custom logic

src/main/java/<package>/Helper.java
1package com.example.helper;
2
3public class Helper {
4
5public static void myHelper() {
6 System.out.println("Hello World!");
7}
8
9}
2

Add your file to .fernignore

A .fernignore file is automatically created in your SDK repository when you use GitHub publishing.
.fernignore
1# Specify files that shouldn't be modified by Fern
2
3src/main/java/<package>/Helper.java
3

Consume the helper

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

1import com.example.helper.Helper;
2
3public class Main {
4
5public static void main(String[] args) {
6 Helper.myHelper();
7}
8
9}

Adding 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.

1

Update generators.yml configuration

Name your Fern-generated client something like BaseClient to reflect that this client will be extended.

generators.yml
1- name: fernapi/fern-java-sdk
2 version: "..."
3 config:
4 client-class-name: BaseClient
2

Import and extend the generated client

First, import the Fern generated base client and extend it. Then, add whatever methods you want.

src/main/java/com/example/MyClient.java
1package com.example;
2
3import com.example.client.BaseClient;
4
5public class MyClient extends BaseClient { // extend the Fern generated client
6
7 public void myHelper() {
8 System.out.println("Hello World!");
9 }
10
11}
3

Update .fernignore

Add the MyClient.java to .fernignore.

.fernignore
1+ src/main/java/com/example/MyClient.java
4

Consume the method

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

1client.myHelper();

Adding custom dependencies

Pro Feature

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

To add packages that your custom code requires, update your generators.yml.

generators.yml
1- name: fernapi/fern-java-sdk
2 version: "..."
3 config:
4 custom-dependencies:
5 - org.apache.commons:commons-lang3:3.12.0
6 - org.slf4j:slf4j-api:2.0.7