Adding custom code
This page covers how to add custom logic, methods, interceptors, and dependencies to your Java SDK.
Adding custom logic
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 an opt-in self-type pattern. When enabled via the enable-extensible-builders flag, generated builders can be extended while maintaining type safety during method chaining.
Common use cases include:
- Dynamic URL construction: Replace placeholders with runtime values (e.g.,
https://api.${TENANT}.example.com) - Custom authentication: Implement complex auth flows beyond basic token authentication
- Request transformation: Add custom headers or modify requests globally
- Multi-tenant support: Add tenant-specific configuration and headers
Default implementation
If you don’t need to extend the builder, use the provided Impl class:
Method reference
Each method serves a specific purpose and is only generated when needed:
Common patterns
Multi-tenant URLs
Dynamic authentication
Environment variable expansion
Request tracking
Requirements
- Fern Java SDK version: 2.39.6 or later
- Configuration:
enable-extensible-builders: trueingenerators.yml
Adding custom interceptors
OkHttp interceptors observe and rewrite every request and response an SDK sends, which covers request signing, public key client validation, and custom logging or tracing. Setting custom-interceptors to true adds an addInterceptor(okhttp3.Interceptor) method to your generated client builder, giving the developers who install your SDK a supported extension point for those customizations:
Developers using your SDK register interceptors as they construct the client. Each call to addInterceptor appends to the chain, and all are applied to the underlying OkHttpClient at build time:
Custom interceptors run in registration order, inside Fern’s retry interceptor, so a retried request passes through them again and a signing interceptor signs each attempt. For OAuth APIs, addInterceptor is available both before and after the token(...) or credentials(...) call.
To attach an interceptor yourself rather than requiring every caller to register one, override the protected setInterceptors(ClientOptions.Builder) method on a custom builder, and add that builder to .fernignore so regeneration doesn’t overwrite it.
Adding custom dependencies
Enterprise feature
This feature is available only for the Enterprise plan. To get started, reach out to support@buildwithfern.com.
To add packages that your custom code requires, update your generators.yml.