Custom Code With SDK Hooks
Availability
SDK Hooks are available for Business and Enterprise users only.
SDK Hooks enable custom logic to be added to SDK functions and request lifecycles across supported SDKs. These hooks allow for transformations, tracing, logging, validation, and error handling during different stages of the SDK’s lifecycle.
Hooks can be applied to the following lifecycle events:
- On SDK initialization: Modify the SDK configuration, base server URL, wrap or override the HTTP client, add tracing, inject global headers, and manage authentication.
- Before request: Cancel an outgoing request, transform the request contents, or add tracing. Access to SDK configuration and operation context.
- After success: When a successful response is received, add tracing and logging, validate the response, return an error, or transform the raw response before deserialization. Access to SDK configuration and operation context.
- After error: On connection errors or unsuccessful responses, add tracing and logging or transform the returned error. Access to SDK configuration and operation context.
Hook Context
All hooks (except SDK initialization) receive a HookContext
object that provides access to:
- SDK Configuration: The complete SDK configuration object, allowing hooks to access custom settings, authentication details, and other configuration parameters.
- Base URL: The base URL being used for the request.
- Operation ID: The unique identifier for the API operation being called.
- OAuth2 Scopes: The OAuth2 scopes required for the operation (if applicable).
- Security Source: The security configuration or source for the operation.
- Retry Configuration: The retry settings for the operation.
SDK Configuration Access
Important
SDK configuration access in hooks is controlled by the sdkHooksConfigAccess
feature flag in the generation
section of your gen.yaml
configuration
file.
The sdkHooksConfigAccess
feature flag determines whether hooks have access to the complete SDK configuration object:
-
sdkHooksConfigAccess: true
(default for newly generated SDKs): Hooks receive full access to the SDK configuration through theHookContext
object, and the SDK initialization hook receives the complete configuration object as a parameter. -
sdkHooksConfigAccess: false
(default for SDKs generated before May 2025): Hooks have limited access to SDK configuration, and the SDK initialization hook has a different signature that doesn’t include the configuration parameter.
Version Compatibility
- New SDKs (May 2025 and later): The
sdkHooksConfigAccess
flag defaults totrue
, providing full configuration access. - Existing SDKs (before May 2025): The flag defaults to
false
to maintain backward compatibility. You can manually set it totrue
in yourgen.yaml
file to enable full configuration access.
When sdkHooksConfigAccess
is set to false
, the SDK initialization hook will have a different signature that doesn’t receive the configuration object as a parameter, limiting the customization options available during SDK initialization.
To enable full SDK configuration access in existing SDKs, add sdkHooksConfigAccess: true
under the generation
section in your gen.yaml
file.
Add a Hook
Hooks are supported in SDKs generated with the latest Speakeasy CLI. Each supported language includes a hooks directory in the generated code:
Steps to Add a Hook
- Create a hook implementation.
Add the custom hook implementation in a new file inside the hooks
directory. The generator won’t override files added to this directory.
- Locate the registration file.
Find the appropriate registration file for the language:
- Instantiate and register the hook.
In the registration file, find the method initHooks/init_hooks/initialize/InitHooks
. This method includes a hooks parameter, allowing hooks to be registered for various lifecycle events.
Instantiate the hook here and register it for the appropriate event.
Note
The registration file is generated once and will not be overwritten. After the initial generation, you have full control and ownership of it.
Here are example hook implementations for each of the lifecycle events across different languages:
Adding Dependencies
To add dependencies needed for SDK hooks, configure the additionalDependencies
section in the gen.yaml
file.
Last updated on