Speakeasy Logo
Skip to Content

Custom Code With SDK Hooks

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

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 the HookContext 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 to true, providing full configuration access.
  • Existing SDKs (before May 2025): The flag defaults to false to maintain backward compatibility. You can manually set it to true in your gen.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:

Language
Go
Directory Path
Python
Directory Path
TypeScript
Directory Path
Java
Directory Path
C#
Directory Path
Ruby
Directory Path

Steps to Add a Hook

  1. 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.

  1. Locate the registration file.

Find the appropriate registration file for the language:

Language
Go
Registration File Path
Python
Registration File Path
TypeScript
Registration File Path
Java
Registration File Path
C#
Registration File Path
Ruby
Registration File Path
  1. 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.

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