cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Bringing in an external API with the Http template

Ike@ACE
Granite

Bringing in an external API with the Http template

I'm trying to figure out where the different aspects of a call to an API would go. For example, the API key or the web address of the API. Also, what is the purpose of the username and password? Is that supposed to be the username and password of your API's account? Also, where do I get the Authorization Server ID if my server is hosted on PTC's cloud service? What are the CSRF settings? What do I do with the Authorization Server Scopes Settings? I found the PTC Help Center article about this, but it wasn't very helpful. Thanks in advance.

9 REPLIES 9

Re: Bringing in an external API with the Http template

Hi Ike@ACE

 

Please provide more details on what you're trying to call.  Is this an API you're trying to call from a client to the ThingWorx platform, or are you trying to run javascript in ThingWorx for connecting to an external system? 

 

Regards.

 

--Sharon

Re: Bringing in an external API with the Http template

In case, you are unaware, there is a Base Thing Template called Http Connector. If you view the different aspects of tabs of that type of Thing, you'll find the properties I'm talking about.

Re: Bringing in an external API with the Http template

Hello Ike@ACE ,

Have you looked at the tool https://www.postman.com/ which allows you to exercise a REST API to determine how to structure your inputs and what gets returned. 

For example if you wanted to get information from ThingWorx Thing by running a service you could create a POST transaction in Postman. The header would need to contain key/value information.

appKey, Content-type, Accept.

 

The appKey has a value which is defined in ThingWorx. This is how you identify yourself to ThingWorx as an authorized person to make requests.

 

The Content-type has a value of "application/json" which tells the interface how to communite

 

The Accept identifies what type of information you are looking to accept in my test case "text/html".

 

The Cross-site request forgery (CSRF) is information to help you protect from a REST API.

A common defense against CSRF attacks is to use synchronized tokens. However, ThingWorx uses an alternate approach and instead routes all requests through a strict content type filter. This approach is equally secure while being more appropriate for a stateless implementation.

 

Are you trying to make REST API calls into ThingWorx or have ThingWorx make calls into a REST API. I have done some of both. Let me know if any of this information helps.  Postman is a great tool to work out the necessary configuration.

 

Regards

Peter 

Re: Bringing in an external API with the Http template

Hi Peter,
I also have this problem and have never done it before. I will have ThingWorx make calls into a REST API from POSTMAN / MarineTraffic. 

 

Regards

AA

Re: Bringing in an external API with the Http template

Currently, I'm trying to route the Climacell API through Thingworx and then into an AR experience created with Vuforia Studio. This will end up serving the purpose of allowing two different users in two different locations across the country to use the same experience, but get different results based on their location. It will also allow me to edit the Climacell call without having to go through every experience that uses it and edit the code. I can just edit the Thingworx service, instead.

Re: Bringing in an external API with the Http template

Hello Ike@ACE ,

Since you are working with a REST API, which you are trying to access from ThingWorx.

You can use Postman to test making a call to the REST API. If you have a API call which gets general information about the site it will be a "GET" transaction. Postman lets you see the response and verify that you have the various fields correctly specified. 

PEHOWE_0-1611070773273.png

For this transaction it returns a JSON object. From ThingWorx you can create a service something below:

try {
     var params = {
    	url: 'http://<REST API WebSite>/<API Function>'
     };

    var result = Resources["ContentLoaderFunctions"].GetText(params);

    var jsonResult = JSON.parse(result);
        result = result;
    }
} catch(error) {
	logger.error("<REST API WebSite>.GetSiteRequest(): Error Calling GET Request - " 
                     + error.message);
}    

 This function will just return the text, that was returned from the REST API call. You will need to expand and update to meet your needs. 

Let me know if this is the information you are trying to locate. If you can provide more information I may be able to provide more specific answers.

 

Regards,

Peter

Re: Bringing in an external API with the Http template

There is a base template for Things in Thingworx called the Http Connector. I am asking about how to use this specific thing. I can already make a call to the ClimaCell API into Vuforia Studio no problem. In fact, the API's resources give you the exact code that you need to do so. What I want is to route it through Thingworx for reusability. The Http Connector is, I think, exactly what I'm looking for. What I am asking for is information on how to get this type of Thing to work. Thanks

Re: Bringing in an external API with the Http template

Hi Ike@ACE.

 

If we are understanding correctly, you are trying to initiate a call from ThingWorx to get data from ClimaCell, then pass that to Vuforia.  If so, you should look at using the ContentLoaderFunctions.  This post from the IoT Tech Tips forum may help.

 

Regards.

 

--Sharon

Re: Bringing in an external API with the Http template

Hi Ike@ACE.

 

If you have resolved your issue with help from one of the previous responses, please mark the appropriate one as the Accepted Solution for the benefit of others with the same problem.

 

Regards.

 

--Sharon

Announcements

Check out the upcoming Expert Session: Understanding ThingWorx Navigate Licensing in Community "Customer Events" section.