---
editLink: false
lastUpdated: false
---

[@salesforce/b2c-tooling-sdk](../../modules.md) / [cli](../index.md) / HttpMiddlewareHook

# Type Alias: HttpMiddlewareHook

> **HttpMiddlewareHook** = `Hook`\<`"b2c:http-middleware"`\>

Defined in: [packages/b2c-tooling-sdk/src/cli/hooks.ts:231](https://github.com/SalesforceCommerceCloud/b2c-developer-tooling/blob/e7c16f1ae423da0aa47b3e10be88f9770b53619b/packages/b2c-tooling-sdk/src/cli/hooks.ts#L231)

Hook type for `b2c:http-middleware`.

Implement this hook in your oclif plugin to provide custom HTTP middleware
that will be applied to all API clients (OCAPI, SLAS, WebDAV, etc.).

The hook is called during command initialization, after flags are parsed
but before any API clients are created.

## Plugin Registration

Register the hook in your plugin's package.json:

```json
{
  "oclif": {
    "hooks": {
      "b2c:http-middleware": "./dist/hooks/http-middleware.js"
    }
  }
}
```

## Hook Context

Inside the hook function, you have access to:
- `this.config` - oclif Config object
- `this.debug()`, `this.log()`, `this.warn()`, `this.error()` - logging methods

## Example

```typescript
import type { HttpMiddlewareHook } from '@salesforce/b2c-tooling-sdk/cli';
import type { HttpMiddlewareProvider } from '@salesforce/b2c-tooling-sdk/clients';

const hook: HttpMiddlewareHook = async function(options) {
  this.debug('Registering custom middleware');

  const metricsProvider: HttpMiddlewareProvider = {
    name: 'metrics-collector',
    getMiddleware(clientType) {
      return {
        onRequest({ request }) {
          (request as any)._startTime = Date.now();
          return request;
        },
        onResponse({ request, response }) {
          const duration = Date.now() - (request as any)._startTime;
          console.log(`[${clientType}] ${request.method} ${request.url} ${response.status} ${duration}ms`);
          return response;
        },
      };
    },
  };

  return { providers: [metricsProvider] };
};

export default hook;
```
