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:
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.
Update generators.yml
configuration
Name your Fern-generated client something like BaseClient
to reflect that this client will be extended.
Adding custom client configuration
The Java SDK generator supports builder extensibility through a template method pattern. By extending the generated builder classes and overriding protected methods, you can customize how your SDK client is configured without modifying generated code.
Common use cases include:
- Dynamic URL construction: Replace placeholders with runtime values (e.g.,
https://api.${DEV_NAMESPACE}.example.com
) - Custom authentication: Implement complex auth flows beyond basic token authentication
- Request transformation: Add custom headers or modify requests globally
How it works
Generated builders use protected methods that can be overridden:
Create a custom client class
Extend the generated base client:
Method reference
Each method serves a specific purpose and is only generated when needed:
Common patterns
Multi-tenant URLs
Dynamic authentication
Request tracking and monitoring
Advanced HTTP client configuration
Requirements
- Fern Java SDK version: 2.39.1 or later
Adding custom dependencies
To add packages that your custom code requires, update your generators.yml
.