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

[@salesforce/b2c-tooling-sdk](../../modules.md) / [auth](../index.md) / ApiKeyStrategy

# Class: ApiKeyStrategy

Defined in: [packages/b2c-tooling-sdk/src/auth/api-key.ts:26](https://github.com/SalesforceCommerceCloud/b2c-developer-tooling/blob/e7c16f1ae423da0aa47b3e10be88f9770b53619b/packages/b2c-tooling-sdk/src/auth/api-key.ts#L26)

API Key authentication strategy.

Supports two modes:
- Bearer token: When headerName is 'Authorization', formats as 'Bearer {key}'
- Direct key: For other headers (e.g., 'x-api-key'), sets the key directly

## Examples

```ts
// For MRT API (Bearer token)
const auth = new ApiKeyStrategy(apiKey, 'Authorization');
// Sets: Authorization: Bearer {apiKey}
```

```ts
// For custom API key header
const auth = new ApiKeyStrategy(apiKey, 'x-api-key');
// Sets: x-api-key: {apiKey}
```

## Implements

- [`AuthStrategy`](../interfaces/AuthStrategy.md)

## Constructors

### Constructor

> **new ApiKeyStrategy**(`key`, `headerName`): `ApiKeyStrategy`

Defined in: [packages/b2c-tooling-sdk/src/auth/api-key.ts:30](https://github.com/SalesforceCommerceCloud/b2c-developer-tooling/blob/e7c16f1ae423da0aa47b3e10be88f9770b53619b/packages/b2c-tooling-sdk/src/auth/api-key.ts#L30)

#### Parameters

##### key

`string`

##### headerName

`string` = `'x-api-key'`

#### Returns

`ApiKeyStrategy`

## Methods

### fetch()

> **fetch**(`url`, `init`): `Promise`\<`Response`\>

Defined in: [packages/b2c-tooling-sdk/src/auth/api-key.ts:43](https://github.com/SalesforceCommerceCloud/b2c-developer-tooling/blob/e7c16f1ae423da0aa47b3e10be88f9770b53619b/packages/b2c-tooling-sdk/src/auth/api-key.ts#L43)

Performs a fetch request with authentication.
Implementations MUST handle header injection and 401 retries (token refresh) internally.

#### Parameters

##### url

`string`

##### init

[`FetchInit`](../type-aliases/FetchInit.md) = `{}`

#### Returns

`Promise`\<`Response`\>

#### Implementation of

[`AuthStrategy`](../interfaces/AuthStrategy.md).[`fetch`](../interfaces/AuthStrategy.md#fetch)

***

### getAuthorizationHeader()

> **getAuthorizationHeader**(): `Promise`\<`string`\>

Defined in: [packages/b2c-tooling-sdk/src/auth/api-key.ts:53](https://github.com/SalesforceCommerceCloud/b2c-developer-tooling/blob/e7c16f1ae423da0aa47b3e10be88f9770b53619b/packages/b2c-tooling-sdk/src/auth/api-key.ts#L53)

Returns the authorization header value for use with openapi-fetch middleware.

#### Returns

`Promise`\<`string`\>

#### Implementation of

[`AuthStrategy`](../interfaces/AuthStrategy.md).[`getAuthorizationHeader`](../interfaces/AuthStrategy.md#getauthorizationheader)
