Adding custom code

View as Markdown

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

Adding custom logic

To get started adding custom code:

1

Create a new file and add your custom logic

src/YourNamespace/Helper.cs
1namespace YourNamespace;
2
3public static class Helper
4{
5 public static void MyHelper()
6 {
7 Console.WriteLine("Hello World!");
8 }
9}
2

Add your file to .fernignore

.fernignore
1# Specify files that shouldn't be modified by Fern
2
3src/YourNamespace/Helper.cs
3

Consume the helper

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

1using YourNamespace;
2
3Helper.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-csharp-sdk
2 version: 2.12.0
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/YourNamespace/MyClient.cs
1namespace YourNamespace;
2
3public class MyClient : BaseClient
4{
5 public MyClient(ClientOptions? clientOptions = null) : base(clientOptions)
6 {
7 }
8
9 public void MyHelper()
10 {
11 Console.WriteLine("Hello World!");
12 }
13}
3

Update .fernignore

Add the MyClient.cs to .fernignore.

.fernignore
1+ src/YourNamespace/MyClient.cs
4

Consume the method

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

1using YourNamespace;
2
3var client = new MyClient();

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

1client.MyHelper();