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
connectToHostApp
Connects your app to the CDF host window and returns aHostAppAPI instance.
Parameters
The argument is optional. When provided, it is used for identification in the host.| Parameter | Type | Description |
|---|---|---|
applicationName | string (optional) | The name of your application |
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
Synchronizes app state with CDF for bookmarking and sharing. The saved state is restored asinitialState on the next session.