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

helper.go
1func MyHelper() {
2 fmt.Println("Hello World!")
3}
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
3helper.go
3

Consume the helper

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

1import "github.com/package/example"
2
3example.MyHelper();

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-go-sdk
2 version: "..."
3 config:
4 clientName: BaseClient
2

Import and extend the generated client

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

client/my_client.go
1type MyClient struct {
2 *Client // Embed the Fern generated client.
3}
4
5func NewMyClient(opts ...option.RequestOption) *MyClient {
6 return &MyClient{
7 Client: NewClient(opts...),
8 }
9}
10
11func (m *MyClient) MyHelper() {
12 fmt.Println("Hello World!")
13}
3

Update .fernignore

Add the client/my_client.go. to .fernignore.

.fernignore
1+ client/my_client.go
4

Consume the method

Instead of constructing the generated client, your users will want to construct the extended client.

main.go
1import exampleclient "github.com/package/example/client"
2
3client := exampleclient.NewMyClient():

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

1client.MyHelper()