Adding custom code

This page covers how to add custom logic and methods to your Go SDK.

Adding custom logic

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

.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()