Cancel
Showing results for 
Search instead for 
Did you mean: 

Can we create Assets automatically using API?

Hi,

 

Can we create Assets automatically using API? Are there any Node.JS examples on how to create an Asset using API?

 

 

Regards,

Parag

 

4 REPLIES

Re: Can we create Assets automatically using API?

Legend
Legend

I haven't personally used it (yet), but the Asset Management API seems to be what you need. You can find samples of using mdsp APIs from NodeJS in devops-demo devopsadmin component, e.g. using the Notification API with technical user credentials:

https://gitlab.com/mindsphere/devops-demo/blob/master/devops/devopsadmin/services/notification.js

 

In your case the base support code would be the same, you need to adapt it to the Asset Management API.

 

I might add some samples also about the Asset Manager around end of the month to devops-demo, since we need it for another internal project. But no guarantees :-)

Re: Can we create Assets automatically using API?

Hi,

I had referred to the API Documentation, but there are still issue when I call asspecttype API.

So, what I am trying to do is:

1. Create an Aspect using aspecttype API with the variables I need.
2. Then I'll create an Asset Type which uses above Aspect.
3. Then I'll create an Asset of above Asset Type.

However, at first step itself there are issues:
1. I am able to create Aspect using aspecttype API via Swagger UI, it uses the end point as:

https://<mytenant>-assetmanagement.eu1.mindsphere.io/api/assetmanagement/v3/aspecttypes/<mytenant>.<myaspectid>

2. I am also able to create Aspect using aspecttype API via Adavnce REST Client, it can be done using the end point mentioned in your examples:

https://gateway.eu1.mindsphere.io/api/assetmanagement/v3/aspecttypes/<mytenant>.<myaspectid>

3. However when I use end points within my app I get following output:

"/api/assetmanagement/v3/aspecttypes/<mytenant>.<myaspectid>" gives me 500 Internal Server Error

"https://<mytenant>-assetmanagement.eu1.mindsphere.io/api/assetmanagement/v3/aspecttypes/<mytenant>.<myaspectid>" gives me
"Refused to connect to 'https://<mytenant>-assetmanagement.eu1.mindsphere.io/api/assetmanagement/v3/aspecttypes/<mytenant>.<...>' because it violates the following Content Security Policy directive: "default-src 'self' static.eu1.mindsphere.io". Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback."

"https://gateway.eu1.mindsphere.io/api/assetmanagement/v3/aspecttypes/<mytenant>.<myaspectid>" gives me
"Refused to connect to 'https://gateway.eu1.mindsphere.io/api/assetmanagement/v3/aspecttypes/<mytenant>.<myaspectid>' because it violates the following Content Security Policy directive: "default-src 'self' static.eu1.mindsphere.io". Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback."

Although I am using "x-xsrf-token": getCookie('XSRF-TOKEN') in the header and I am sending "origin" as "window.location.protocol + '//' + window.location.host" in the header for all above trials.

Anything wrong I am doing?

 

Just to add, I have already successfully implemented GET for Assets, Aspects and POST for time series, not sure why this one is troubling me... Smiley Wink

Re: Can we create Assets automatically using API?

Legend
Legend

When you call the MindSphere APIs from the client side, the CSP protections configured in your MindSphere app kick in, and by default they only accept connections from the same full domain as your app is running under. When you try to call the MindSphere APIs, they will be automatically mapped to your application domain `/api` subpath.

 

Now, you'd have two options, either add the domain `gateway.eu1....` to the CSP allowed sources, or (I'd recommend this option), just change your references in the client code to refer to relative paths without the domain. That way the browser will actually use as domain the same domain your app has, and you won't have any CSP issues.

 

Below you have an example connecting to the tenantmanagement api endpoint, but it's the same idea for assetmanagement:

https://gitlab.com/mindsphere/devops-demo/blob/master/src/app/mindsphere.service.ts

 

Make sure you add the relevant scopes to the application and that your logged in user id has proper permissions.

Re: Can we create Assets automatically using API?

Actually I was playing with multiple parameters and URLs, which led to some confusion and I did not try the native URL to create the Aspect Type. Changing it to native URL did fix the issue.

 

I am now able to create Aspect Type and hopefully there should not be any issue in creating Asset Type and Asset Smiley Happy

 

Thanks for the guidance.

 

Regards,

Parag