Skip to main content
17-Peridot
April 8, 2022
Question

How to use the REST API inside Windchill?

  • April 8, 2022
  • 5 replies
  • 3918 views

Hello.

I am writing java code in Windchill, one of the parts of the code is getting a representation.

The REST API has a method for getting representation.

How do I use the REST API? How do I get user credentials to send requests?

5 replies

24-Ruby III
April 8, 2022

 Hi,

 

Please find "Windchill REST Services User's Guide" guide which may be useful for you in attachment.

HelesicPetr
22-Sapphire II
22-Sapphire II
April 8, 2022

Hello @VladiSlav 

 

If you use Java code in a Windchill, why do you want to use the RestAPI ?

 

I would use a standard api to get visualization(representation)

 

Representation defaultRep = RepresentationHelper.service.getDefaultRepresentation(epm);
or
Representation defaultRep = RepresentationHelper.service.getRepresentations(epm);

epm can be object that have representation (EPMDocument, WTPart, WTDocument, MPMOperation atc.)

 

I know it is not answer to your question but I would recommend to use it. 

 

PetrH

VladiSlav17-PeridotAuthor
17-Peridot
April 8, 2022

HelesicPetr, thank you very much for your answer, this is what you need. But we still want to learn how to use the REST API, and for this we need to get the login and password of the active user?

HelesicPetr
22-Sapphire II
22-Sapphire II
April 8, 2022

Hi @VladiSlav 

Actually I don't have so much experience with the RestAPI but as I know, you can not retrieve password from LDAP. 

 

You can get just login name with 

WTPrincipal currentuser = SessionHelper.getPrincipal();
if (currentuser instanceof WTUser)
{
WTUser user = (WTUser) currentuser;
String loginName = user.getAuthenticationName();
}

 

PetrH

12-Amethyst
April 29, 2025

If you are writing Java code in Windchill, then you should use the Java libraries and functions available to you, then you don't have to worry about authenticating. If you are writing java code outside of Windchill, then you need to authenticate based on how the server is configured. If it is using basic auth, then I believe you can just build a basic auth header. If the server is using OIDC, then you likely need to use tokens which usually requires sending an ID and Secret to an endpoint, getting a token in response, then using that in subsequent requests. Try searching the documentation on PTC's site for REST-API authentication via OAuth2 or Token Authentication, but each login flow is dependent on the Identity Provider used by the specific system.

16-Pearl
May 9, 2025

Another option maybe, make the rest post from within a JSP, that way the user can authenticate in the normal way.

Here's sample code that works as an example but not suitable for production environment. Downloads the PDF representation of the CAD doc.

<!DOCTYPE html>
<html>
 <head>
 <title>Download CAD Representation</title>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
 <script>
 $(document).ready(function () {
 var req_headers = {
 "Content-Type": "application/json",
 Accept: "application/json"
 };

 // Get the number parameter from the URL
 const urlParams = new URLSearchParams(window.location.search);
 let number = urlParams.get('number');

 if (!number) {
 alert("No document number provided in the URL.");
 return;
 }

 let apiUrl = "https://<my_host>/Windchill/servlet/odata/v5/CADDocumentMgmt/CADDocuments?%24filter=Number%20eq%20%27" + number + "%27&%24expand=Representations";

 $.ajax({
 type: "GET",
 headers: req_headers,
 url: apiUrl
 }).then(function (data) {
 console.log("Data received: " + JSON.stringify(data));

 // Check if the response contains the expected structure
 if (data.value && data.value.length > 0 && data.value[0].Representations) {
 let representations = data.value[0].Representations;

 for (let rep of representations) {
 if (rep.AdditionalFiles && rep.AdditionalFiles.length > 0) {
 let redirectUrl = rep.AdditionalFiles[0].URL;

 // Redirect the user to the extracted URL
 window.location.href = redirectUrl;
 return;
 }
 }
 }

 alert("No downloadable representation found.");
 }).fail(function () {
 alert("Error fetching data.");
 });
 });
 </script>
 </head>
 <body>
 <p>The representation is being downloaded, check your downloads folder</p>
		<!-- sample usage:
save the .jsp file in WT_HOME\codebase\netmarkets\jsp\docdownload
 https://<my_host>/Windchill/netmarkets/jsp/docdownload/downloadCADrep.jsp?number=00035772
-->
 </body>	

</html>