> ## Documentation Index
> Fetch the complete documentation index at: https://learn.nexudus.com/llms.txt
> Use this file to discover all available pages before exploring further.

# useLocationByHostContext

> Resolve and access the current business context from the host/domain and bootstrap scoped HTTP clients.

# useLocationByHostContext

`useLocationByHostContext` resolves the current Business based on the configured host (domain) and exposes a business-scoped `httpClient`, a root `apiClient`, and the `business` record. It bootstraps the location context used across the app and is intended to wrap the whole application.

Typical flow: the app loads using the current host (e.g. `acme.spaces.Nexudus.com`), `LocationByHostProvider` finds the matching Business, and makes that available to all children. Route-level overrides can later switch business via `useLocationByRouteContext`.

## What you get

* `Id: number` – Current business ID
* `WebAddress: string` – Current business web address
* `httpClient: HttpClient` – Axios-like client scoped to `https://{WebAddress}.spaces.Nexudus.com`
* `apiClient: HttpClient` – API root client using `apiBaseUrl` from app config
* `business: Business` – Business details from `endpoints.system.business`

## Provider and placement

You must render the provider at the app root, before anything that consumes location state. The repository already does this in `src/App.tsx`:

```tsx theme={null}
// src/App.tsx
<HelmetProvider>
  <ErrorBoundary FallbackComponent={AppErrorFallback}>
    <LocationByHostProvider>
      {/* ... */}
      <HomeRouter>
        <LocationContext>{/* app content */}</LocationContext>
      </HomeRouter>
      {/* ... */}
    </LocationByHostProvider>
  </ErrorBoundary>
</HelmetProvider>
```

This ensures every consumer of `useLocationByHostContext` or `useLocationByRouteContext` is safely wrapped.

## How it works

* Reads `config.host` from `useAppConfig()`.
* Fetches `GET /api/sys/businesses/getByHost?host={config.host}` using the root `apiClient(config.apiBaseUrl)`.
* Builds `httpClient = ApiHttpClient(WebAddress)` and `apiClient = ApiHttpClient(config.apiBaseUrl)`.
* Fetches `business` via `endpoints.system.business` with `includeHeaders: false` to avoid cache invalidation on auth changes.
* Throws `NO_BUSINESS_FOR_HOST` if no matching business is found.

Source: `src/states/useLocationDomainContext.tsx`.

## Usage example

```tsx theme={null}
import { useLocationByHostContext } from '@/states/useLocationDomainContext'

export function HostBusinessBadge() {
  const { business } = useLocationByHostContext()
  return <span>{business.Name}</span>
}
```

`useLocationByRouteContext` will override the `httpClient` and `business` when a `:webAddress` is present in the route; otherwise, consumers see this host-level context.

## Edge cases and errors

* Usage outside the provider throws: `useLocationByHostContext must be used within an LocationByHostContext`.
* Suspense: data loading is Suspense-based, so ensure a `<Suspense>` boundary above the provider tree (the app already has one in `LocationContext`).
* `NO_BUSINESS_FOR_HOST` indicates misconfiguration of the domain/host.

## See also

* [useLocationByRouteContext](/developers/state/use-location-by-route-context) – Route param override for business context
* [LocationContext](/developers/state/location-context) – Provider composition used by the app
