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

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

# Class: BackendDispatcher\<S\>

Defined in: [packages/b2c-tooling-sdk/src/compat/dispatcher.ts:82](https://github.com/SalesforceCommerceCloud/b2c-developer-tooling/blob/2feab01f654eec77c6d702777ec2818472f131f1/packages/b2c-tooling-sdk/src/compat/dispatcher.ts#L82)

Stateful router that runs SCAPI optimistically and falls back to OCAPI
once on a safe capability/auth/request rejection, caching the choice for the lifetime of the
dispatcher. See the module-level docs for the full rationale.

Construct one per logical operation (e.g. one per CLI command run, or
one per VSCode user-initiated action). Sharing a dispatcher across
unrelated operations is fine but not required.

## Type Parameters

### S

`S`

The SCAPI ops bundle type (e.g., `ScapiJobsOps`).

## Constructors

### Constructor

> **new BackendDispatcher**\<`S`\>(`preference`, `createScapi`, `domainName`): `BackendDispatcher`\<`S`\>

Defined in: [packages/b2c-tooling-sdk/src/compat/dispatcher.ts:96](https://github.com/SalesforceCommerceCloud/b2c-developer-tooling/blob/2feab01f654eec77c6d702777ec2818472f131f1/packages/b2c-tooling-sdk/src/compat/dispatcher.ts#L96)

#### Parameters

##### preference

[`ApiBackendPreference`](../../clients/type-aliases/ApiBackendPreference.md)

User preference (`auto` | `scapi` | `ocapi`).

##### createScapi

() => `S` \| `undefined`

Lazily builds the SCAPI ops bundle. Returns
  `undefined` when SCAPI is not configured (shortCode/tenantId/auth
  missing).

##### domainName

`string`

Used in fallback log messages (e.g. `'jobs'`).

#### Returns

`BackendDispatcher`\<`S`\>

#### Throws

Error if `preference === 'scapi'` but `createScapi()` returns
  `undefined` — explicit SCAPI without configuration is a hard error.

## Accessors

### active

#### Get Signature

> **get** **active**(): [`ResolvedBackend`](../type-aliases/ResolvedBackend.md) \| `undefined`

Defined in: [packages/b2c-tooling-sdk/src/compat/dispatcher.ts:114](https://github.com/SalesforceCommerceCloud/b2c-developer-tooling/blob/2feab01f654eec77c6d702777ec2818472f131f1/packages/b2c-tooling-sdk/src/compat/dispatcher.ts#L114)

Backend that has handled requests so far, or undefined if none yet.

##### Returns

[`ResolvedBackend`](../type-aliases/ResolvedBackend.md) \| `undefined`

## Methods

### run()

> **run**\<`T`\>(`branches`): `Promise`\<`T`\>

Defined in: [packages/b2c-tooling-sdk/src/compat/dispatcher.ts:123](https://github.com/SalesforceCommerceCloud/b2c-developer-tooling/blob/2feab01f654eec77c6d702777ec2818472f131f1/packages/b2c-tooling-sdk/src/compat/dispatcher.ts#L123)

Runs the operation against the resolved backend. If unresolved (auto
with SCAPI configured), tries SCAPI first; on a safe fallback trigger,
falls back to OCAPI and caches the choice. Ambiguous failures propagate.

#### Type Parameters

##### T

`T`

#### Parameters

##### branches

[`DispatchBranches`](../interfaces/DispatchBranches.md)\<`S`, `T`\>

#### Returns

`Promise`\<`T`\>
