connectToHostApp function establishes communication with the CDF host window and gives you a HostAppAPI instance to retrieve credentials and construct an authenticated Cognite SDK.
Prerequisites
- A Flows app created with
npx @cognite/cli@latest apps create - Basic understanding of React hooks and
useEffect
Related guides
- Get started with Cognite Flows — Create your first app
- Run your app locally — Development with authentication
- Flows features — TanStack Query integration with
connectToHostApp - App state in URLs — Save and restore state with
syncInternalState
connectToHostApp
Connects your app to the CDF host window and returns aHostAppAPI instance.
Return value
| Property | Type | Description |
|---|---|---|
api | HostAppAPI | API for communicating with the CDF host |
initialState | string | undefined | Restored state from a previous session (if syncInternalState was used) |
Example: connect and get project
HostAppAPI
TheHostAppAPI interface is returned by connectToHostApp and provides methods for interacting with the CDF host.
getProject
Returns the current CDF project name.getBaseUrl
Returns the base URL of the CDF cluster (for example,https://greenfield.cognitedata.com).
getAccessToken
Returns the current access token for authenticating CDF API calls.Example: construct a CogniteClient
UsegetProject, getBaseUrl, and getAccessToken together to build an authenticated CogniteClient from @cognite/sdk:
navigateInternal
Navigates to a path within CDF.| Parameter | Type | Description |
|---|---|---|
path | string | CDF-relative path to navigate to |
queryParams | Record<string, string> (optional) | Query parameters |
hash | string (optional) | URL hash fragment |
navigateExternal
Opens an external URL.| Parameter | Type | Description |
|---|---|---|
url | string | HTTPS URL to navigate to |
openInNewTab | boolean (optional) | Open in a new browser tab |
syncInternalState
Saves a serialized state string to thecustomAppInternalState URL search param. The saved state is passed back as initialState the next time the app mounts. Users can return to the same view after navigating away or refreshing. Because the state lives in the URL, the link is shareable.
| Parameter | Type | Required | Description |
|---|---|---|---|
state | string | Yes | JSON-serialized state. Must be a string. Call JSON.stringify before passing. |
| Returns | Promise<boolean>. true if the host saved the state; false if the host did not handle the call (for example, in standalone dev mode). |