Skip to main content

Customize your SDK with hooks

This tutorial includes the following SDK languages and versions:

TypeScriptJavaPythonC#GoPHP

Hooks are a powerful way to customize your SDK. They allow you to hook into the API calls and modify the request or response. You can also use them to add custom functionality to your SDK.

Some examples of ways you can use hooks:

  • Add custom headers to all requests
  • Add an API version as a header or a query string parameter
  • Customize authentication, such as copying an API key from a header to a URL parameter
  • Add telemetry
  • Add logging to API errors

In this tutorial, we'll show you how to use hooks to add API versions to your SDK using the Accept-version header.

Prerequisites

This tutorial assumes you already have:

  • The liblab CLI installed and you are logged in.
  • An API spec.
  • A liblab config file created using liblab init.

Steps

In this tutorial you'll:

  1. Create the hooks
  2. Add the hook code
  3. Build the SDK

Create the hooks

First we'll create the hooks code. Run the following command in the same folder as your config file:

Terminal
liblab hooks add

This will create a hooks folder with hooks for each SDK language defined in your config file:

hooks
├── csharp
├── go
├── java
├── php
├── python
└── typescript

You may see a different set of hooks folders depending on the languages you have defined in your config file.

For each language, you may need to install some necessary dependencies.

To install the TypeScript dependencies, run the following command from the hooks/typescript folder:

Terminal
npm install

Add the hook code

In the hooks folder, you will find a project for each of the languages you have generated. The API version will be added to a header in the before request hook.

Locate the before request hook

Open the relevant file and inspect the hook code.

The beforeRequest method is implemented in the CustomHook class in the src/index.ts file:

src/index.ts
export default class CustomHook implements Hook {
async beforeRequest(request: Request): Promise<void> {
// Your code goes here
}
}

We can modify the request object by adding or removing headers, tampering with the request body, changing the URL, etc.

The Request class lives in the src/index.ts file:

src/index.ts
export interface Request {
method: string;
url: string;
input?: object;
headers: object;
}

Any changes you make to the request will be used by the SDK when making the API call.

Add the version header

To add the version header, make the following code changes to the before request hooks:

src/index.ts
async beforeRequest(request: Request): Promise<void> {
// Add the Accept-version header to the request with version 1.0
request.headers = {
...request.headers,
"Accept-version": "1.0"
};
}

This will add the new header into the request, and this will then be used by the SDK when making the API call.

Build the SDK

Now that your hook code is ready, you can build the SDK with the following command:

Terminal
liblab build
Terminal
Packaging hooks...
✓ C# built
✓ Go built
✓ Java built
✓ Python built
✓ TypeScript built
Successfully generated SDKs downloaded. You can find them inside the "output" folder

This will send your hook code along with your spec and config to liblab to use in the SDK generation. You can see the results in the SDKs that are generated and downloaded to the output folder.

The hooks can be found in the src/hooks folder:

hooks
├── CustomHook.ts
└── Hook.ts

Your hook code is in the Hook.ts file.