NodeJS Library for MindSphere Connectivity - TypeScript SDK for MindSphere - MindSphere Command Line Interface - MindSphere Development Proxy
The full documentation can be found at https://opensource.mindsphere.io/docs/mindconnect-nodejs/index.html
Installing the library
There are several ways to install the library. The most common one is via npm registry:
# install the latest stable library from the npm registrynpm install @mindconnect/mindconnect-nodejs --save# install the latest alpha library from the npm registrynpm install @mindconnect/mindconnect-nodejs@alpha --save
The easiest way to start is to use the provided command line interface to create a starter project:
How to create a nodejs MindSphere agent
The following steps describe the easiest way to test the library. You can of course create the required dependencies also programmatically via API calls.
Step 0: Create an asset type and aspect types
MindSphere V3 IoT model requires that you create an asset type and aspect types to describe your assets. For the example we will create an asset type of type Engine with two aspect types: Environment and Vibration. (Note that my tenant is called castidev, you will have to use your own tenant name)
More information about MindSphere Data Model.
Step 1: Create an asset
Create an asset (In example it is called AcmeMotor) of type Engine in AssetManager for your data.
Step 2: Create an agent of type MindConnectLib in MindSphere
Create an agent in Asset Manager of type core.MindConnectLib create initial JSON token and store it to file (e.g. agentconfig.json)
Step 3 : Create an agent
Read the initial configuration from the config file and create the agent. If you are using the SHARED_SECRET profile there is no need to setup the local certificate for the communication (recommended for smaller devices).
If you want to use the RSA_3072 profile you must also set up the agent certificate.
// you can create the private.key for example using openssl:// openssl genrsa -out private.key 3072agent.SetupAgentCertificatefs.readFileSync"private.key";
Step 4: Onboard the agent
The first operation is onboarding of the agent. This creates a client secret which is used for the communication with MindSphere.
This data is stored by default in the .mc folder in your application if you don't change the base path in the constructor of the agent.
Important: Make sure that your folder with the configurations is not reachable from the internet as it contains the client_secret for the authentication.
Step 5: Configure the data model and data mappings to asset variables. (via code)
Important: From the version 3.8.0 it is possible to create the data source configuration and data mappings fully automatic via code
First you need to create a data source configuration
// create data source configuration for an asset type castidev.Engine;await agent.PutDataSourceConfigurationgeneratedConfig;
and then the data mappings:
The agents have now access to MindSphere TypeScript SDK (in beta)
agent.Sdk;// the sdk gives you access to e.g. asset management client with which you can get asset types or assets from mindsphere// which can be used for automatic data source configuration and automatic mappings;await assetMgmt.GetAssets...;await assetMgmt.GetAspectTypes...;
If you take a look at the MindSphere configuration of your agent now it should look like this:
And the data mappings should be in place
Step 6 After this you can send the data in the code
for ; index < 5; index++
(If you were using UI to configure data mappings you will have long integers instead of human-readable data point Ids.)
Step 6.1 using bulk upload
If you don't want to send the data points one by one, you can also use the BulkPostData method
;await agent.BulkPostData bulk;
Events can now be created with the library. You can create events for your agent or for your entities. In order to create an event for your entity you need to know the assetId of the asset.
const configuration = ;const agent = configuration;if !agentawait agent;const event: MindsphereStandardEvent ="entityId": configurationcontentclientId // use assetid if you dont want to store event in the agent :)"sourceType": "Event""sourceId": "application""source": "Meowz""severity": 20 // 0-99 : 20:error, 30:warning, 40: information"timestamp":"description": "Test";// send event with current timestampawait agent;
Files can now be uploaded via the library. You can upload files for your agent or for your entities. In order to create an event for your entity you need to know the assetid of the asset.
Since version 3.5.1. the agents are using the multipart upload API of the MindSphere. This means that the agents can upload files also bigger > 8 MB, The multipart upload must be switched on (chunk:true) if you want to activate this behavior. The parameter parallelUploads determine the maximal number of parallel uploads. You can increase this on a powerful computer to speed up the upload or decrease to prevent network congestion.
const configuration = ;const agent = configuration;if !agentawait agent;await agent;
Here is a demo agent implementation.
Making sure that the data arrives also with flaky internet connection
You can wrap all asynchronous object calls into the retry function which will automatically retry the operation for n times before throwing an exception.
;// if you want to be more resillient you can wrap every async method// in the retry function which will try to retry several times before throwing an exception// onboarding over flaky connectionawait retry 5,agent.OnBoard// bulk upload with 5 retries;await retry5,agent.BulkPostDatabulk;
The data in the MindSphere can be observed in the fleet manager.
Set the http_proxy or HTTP_PROXY environment variable if you need to connect via proxy.
# set http proxy environment variable if you are using e.g. fiddler on the localhost.export HTTP_PROXY=http://localhost:8888
MindSphere TypeScript SDK
The library comes with the typescript SDK which can be used to access MindSphere APIs
It implements support for both frontend (browser e.g. angular, react...) and backend development in node.js. and different MindSphere authentication types:
Frontend: - Browser (Session, Cookies)
Backend (node.js): - UserCredentials - AppCredentials - ServiceCredentials - MindSphere Agents
and Clients for following APIs
The example below shows how to use the sdk.
// The example shows how to Get Assets from MindSphere with custom AssetType using frontend authentication// you can pass an instance an Authorizer (BrowserAuth, UserAuth, CredentialsAuth, TokenManagerAuth, MindConnectAgent)// to use different authorization types in MindSphere or implement the TokenRotation interface if you want to// provide your own authorizer.//// The default constructor uses frontend authorization.;;;// you will get fully typed assets in response
If an API is missing and you would like to contribute a Client for it take a look at CONTRIBUTING.md.
Command Line Interface
The full documentation for the command line interface can be found at
The library comes with a command line interface which can also be installed globally. You can use the command line mode to upload timeseries, files and create events in the MindSphere.
# install the library globaly if you want to use its command line interface.npm install -g @mindconnect/mindconnect-nodejs
The library can also be downloaded as executable from the GitHub release page.
Download the version for your system and place it in folder which is in your PATH.
Linux, macOS: Rename the file to
mc and make sure that the file is marked as executable (
First step is to configure the CLI. For this you will need a session cookie from MindSphere, service credentials (which have been deprecated) or application credentials from your developer cockpit.
First start the credentials configuration. This will start a web server on your local computer where you can enter the credentials.
# run mc service-credentials --help for full information$ mc service-credentialsnavigate to http://localhost:4994 to configure the CLIpress CTRL + C to exit
The image below shows the dialog for adding new credentials (press on the + sign in the upper left corner)
You can get the application credentials from your developer or operator cockpit in MindSphere. (if you don't have any application you can register a dummy one just for CLI)
Once configured you can press CTRL + C to stop the configuration server and start using the CLI. Remember the passkey you have created as you will be using it with almost all CLI commands.
The CLI can be used to create starter projects, upload timeseries, events and files, read agent diagnostics etc. The CLI commands should only be used in secure environments! (e.g on your working station, not on the agents).
Here is an overview of CLI commands:
# run mc --help to get a full list of the commandsmc --help
MindSphere Development Proxy
The CLI comes with a development proxy which can be used to kickstart your MindSphere development. It provides an endpoint at your local machine at
which will authenticate all requests using either a borrowed SESSION and XSRF-TOKEN cookie from MindSphere or the the configured app credentials or service credentials.
The command below will start your development proxy without any installation and configuration (you just need the cookies from an existing app):
npx @mindconnect/mindconnect-nodejs dev-proxy --session <SESSION-TOKEN> --xsrftoken <XSRF-TOKEN> --host <appname>.<tenant>.<region>.mindsphere.io
For more complex tasks install and configure the CLI
Usage: mc dev-proxy|px [options]starts mindsphere development proxy (optional passkey) *Options:-m, --mode [credentials|session] service/app credentials authentication ofsession authentication (default: "session")-o, --port <port> port for web server (default: "7707")-r, --norewrite don't rewrite hal+json urls-w, --nowarn don't warn for missing headers-d, --dontkeepalive don't keep the session alive-v, --verbose verbose output-s, --session <session> borrowed SESSION cookie from brower-x, --xsrftoken <xsrftoken> borrowed XSRF-TOKEN cookie from browser-h, --host <host> the address where SESSION and XSRF-TOKENhave been borrowed from-t, --timeout <timeout> keep alive timeout in seconds (default:"60")-k, --passkey <passkey> passkey--help display help for commandExamples:mc dev-proxy runs on default port (7707) using cookiesmc dev-proxy --port 7777 --passkey passkey runs on port 7777 using app/service credentialsConfiguration:- create environment variables: MDSP_HOST, MDSP_SESSION and MDSP_XSRF_TOKEN using borrowed cookiessee more documentation at
This project has been released under an Open Source license. The release may include and/or use APIs to Siemens’ or third parties’ products or services. In no event shall the project’s Open Source license grant any rights in or to these APIs, products or services that would alter, expand, be inconsistent with, or supersede any terms of separate license agreements applicable to those APIs. “API” means application programming interfaces and their specifications and implementing code that allows other software to communicate with or call on Siemens’ or third parties’ products or services and may be made available through Siemens’ or third parties’ products, documentations or otherwise.