# Test workflow started by API

**URL:** <https://community.dynatrace.com/t/test-workflow-started-by-api/48243>\
**Category:** Developer Q&A Forum\
**Tags:** workflows, automations\
**Created:** [October 9, 2024, 9:22pm UTC](https://community.dynatrace.com/t/test-workflow-started-by-api/48243 "2024-10-09T21:22:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![henk\_stobbe](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/henk_stobbe/32/30434_2.png) [@henk\_stobbe](https://community.dynatrace.com/u/henk_stobbe)\
**Post date:** [October 9, 2024, 9:22pm UTC](https://community.dynatrace.com/t/test-workflow-started-by-api/48243/1 "2024-10-09T21:22:55Z")

</div>

Hello,

I needed a way to test my workflow that can be started by the API. Not wanting to make use one of the existing tools for triggering the workflow, I just created a simple Dashboard with variables&nbsp; that fetches the OAUTH token for me, kicks off the workflow and shows the execution log!

 ![henk_stobbe_0-1728508728011.png](https://global.discourse-cdn.com/dynatrace/original/3X/d/8/d88a145dbdf26e7b6569834c086b9da571a67ff3.png)

Any var change, refreshes the dashboard. So simple but look so nice (-;

(want to look at the source, just contact me)

Have fun,

Henk

---

<div class="post-metadata">

**Author:** ![imsingh](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/imsingh/32/7858_2.png) [@imsingh](https://community.dynatrace.com/u/imsingh)\
**Post date:** [October 21, 2024, 7:05am UTC](https://community.dynatrace.com/t/test-workflow-started-by-api/48243/2 "2024-10-21T07:05:22Z")

</div>

Thanks for sharing it with the Community 🙂

---

<div class="post-metadata">

**Author:** ![andreas\_grabner](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/andreas_grabner/32/27879_2.png) [@andreas\_grabner](https://community.dynatrace.com/u/andreas_grabner)\
**Post date:** [November 12, 2024, 3:49pm UTC](https://community.dynatrace.com/t/test-workflow-started-by-api/48243/3 "2024-11-12T15:49:25Z")

</div>

Hey [@henk\_stobbe](https://community.dynatrace.com/user/11427)&nbsp;- would you mind sharing this dashboard with me?

---

<div class="post-metadata">

**Author:** ![henk\_stobbe](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/henk_stobbe/32/30434_2.png) [@henk\_stobbe](https://community.dynatrace.com/u/henk_stobbe)\
**Post date:** [December 12, 2024, 6:23pm UTC](https://community.dynatrace.com/t/test-workflow-started-by-api/48243/4 "2024-12-12T18:23:16Z")

</div>

Hello,

Forgot about this post, but while catching up found:

[![](https://global.discourse-cdn.com/dynatrace/original/3X/2/3/239107ca0ea1e18333fbe8ac208cf032417279e8.jpeg "Office Hours For App Development | November 2024") ](https://www.youtube.com/watch?v=IVFc0-Iil8w&t=879s)

Not my original idea, but yeah you can look at it as a kind of poor man’s app.

KR Henk

---

<div class="post-metadata">

**Author:** ![henk\_stobbe](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/henk_stobbe/32/30434_2.png) [@henk\_stobbe](https://community.dynatrace.com/u/henk_stobbe)\
**Post date:** [December 12, 2024, 6:26pm UTC](https://community.dynatrace.com/t/test-workflow-started-by-api/48243/5 "2024-12-12T18:26:27Z")

</div>

### &nbsp;:christmas\_tree:Weihnachtsgeschenk **(-;🎄**

Grüße Henk

---

<div class="post-metadata">

**Author:** ![henk\_stobbe](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/henk_stobbe/32/30434_2.png) [@henk\_stobbe](https://community.dynatrace.com/u/henk_stobbe)\
**Post date:** [December 14, 2024, 7:33am UTC](https://community.dynatrace.com/t/test-workflow-started-by-api/48243/6 "2024-12-14T07:33:50Z")

</div>

export default async function () {  
const sleep = (delay) =\> new Promise((resolve) =\> setTimeout(resolve, delay));

// Token request URL  
const token\_url = ‘[https://sso.dynatrace.com/sso/oauth2/token](https://sso.dynatrace.com/sso/oauth2/token)’;

// First token credentials  
const params1 = new URLSearchParams({  
grant\_type: ‘client\_credentials’,  
client\_id: ‘dt0s02.???’,  
client\_secret: ‘dt0s02.???’,  
resource: ‘urn:dtaccount:???’,  
});

// Second token credentials  
const params2 = new URLSearchParams({  
grant\_type: ‘client\_credentials’,  
client\_id: ‘dt0s02.???’,  
client\_secret: ‘dt0s02.???’,  
resource: ‘urn:dtaccount:???’,  
});

// Common headers for token requests  
const tokenHeaders = {  
‘Content-Type’: ‘application/x-www-form-urlencoded’,  
};

try {  
// First token request  
const response1 = await fetch(token\_url, {  
method: ‘POST’,  
headers: tokenHeaders,  
body: params1.toString(), // URL-encoded parameters  
});

if (!response1.ok) {  
const errorBody = await response1.text();  
throw new Error(`Error fetching first token: ${response1.status} - ${errorBody}`);  
}

const responseData1 = await response1.json();  
const accessToken1 = responseData1.access\_token;  
console.log(‘First token received:’, accessToken1);

// Use first token to call workflow API  
const workflowUrl = ‘https://???.apps.dynatrace.com/platform/automation/v1/workflows/???/run’;  
const workflowHeaders = {  
Authorization: `Bearer ${accessToken1}`,  
‘Content-Type’: ‘application/json’,  
};

const body = {  
input: {  
request: $request,  
endpoint: $endpoint,  
cloudprovider: $cloudprovider,  
environment: $environment,  
body: $body,  
}  
};

const workflowResponse = await fetch(workflowUrl, {  
method: ‘POST’,  
headers: workflowHeaders,  
body: JSON.stringify(body),  
});

if (!workflowResponse.ok) {  
const wfErrorBody = await workflowResponse.text();  
throw new Error(`Error in Workflow API call: ${workflowResponse.status} - ${wfErrorBody}`);  
}

const workflowData = await workflowResponse.json();  
console.log(‘Workflow response:’, workflowData);

// Second token request  
const response2 = await fetch(token\_url, {  
method: ‘POST’,  
headers: tokenHeaders,  
body: params2.toString(), // URL-encoded parameters  
});

if (!response2.ok) {  
const errorBody = await response2.text();  
throw new Error(`Error fetching second token: ${response2.status} - ${errorBody}`);  
}

const responseData2 = await response2.json();  
const accessToken2 = responseData2.access\_token;  
console.log(‘Second token received:’, accessToken2);

// Log request URL  
const logUrl = `https://???.apps.dynatrace.com/platform/automation/v1/executions/${workflowData.id}/tasks`;

// Sleep before fetching logs  
await sleep(10000);

// Make the log request with the second token  
const logHeaders = {  
Authorization: `Bearer ${accessToken2}`,  
‘Content-Type’: ‘application/json’,  
};

const logResponse = await fetch(logUrl, {  
method: ‘GET’,  
headers: logHeaders,  
});

if (!logResponse.ok) {  
const logErrorBody = await logResponse.text();  
throw new Error(`Error in Workflow LOG call: ${logResponse.status} - ${logErrorBody}`);  
}

const logData = await logResponse.json();  
console.log(‘Workflow log response:’, logData);

return logData;

} catch (error) {  
console.error(`Error occurred: ${error.message}`);  
throw error;  
}  
}
