Installation
To install the package:Usage
Using ES modules
Using TypeScript
The language of the SDK is native TypeScript, and you don’t have to define any extra types.Quickstart
Web
Backend
Hello, world! - React App
This guide shows you how to create a simple app using the Cognite JavaScript SDK. The app authenticates with, and fetches data from, Cognite Data Fusion (CDF). The simplest way to retrieve data from CDF is to use an SDK. In this guide, we use data from the Open Industrial Data project.To use this guide you need to have npm installed on your machine.
1
Install create-react-app
Install the create-react-app tool to set up your React application.
2
Install the Cognite JavaScript SDK
Install the SDK package in your project.
Authentication with redirect
1
Set up authentication in App.js
Replace the code in
./src/App.js to configure OAuth authentication with redirect.To test the authentication, it’s important to enable SSL or HTTPS. Cognite only lets trusted web pages authenticate.
2
Run the app with HTTPS
In a terminal window, start your application with HTTPS enabled.
Open your browser and verify that you can sign in to the
publicdata project.3
Fetch data from CDF
Update
./src/App.js to fetch 10 assets from CDF. The client.authenticate() call is removed because the SDK calls it automatically on API requests.4
Display the assets
Update the render method in
./src/App.js to display the asset names.You have now successfully fetched and displayed data using the SDK.
Authentication with popup
You can authenticate the SDK by providing a pop-up window. The benefit is that you don’t lose the state of the application. The example below changes the code to use a pop-up window. Also, there’s some refactoring to make the code more readable; there’s a button to call the pop-up window on user interaction and avoid having it blocked by the browser. In./src/App.js: