# Is it possible to execute an external program from Postman?

**URL:** <https://community.postman.com/t/is-it-possible-to-execute-an-external-program-from-postman/5693>\
**Category:** Help Hub\
**Created:** [May 31, 2019, 8:23am UTC](https://community.postman.com/t/is-it-possible-to-execute-an-external-program-from-postman/5693 "2019-05-31T08:23:49Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrissbaumann](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/chrissbaumann/32/5521_2.png) [@chrissbaumann](https://community.postman.com/u/chrissbaumann)\
**Post date:** [May 31, 2019, 8:23am UTC](https://community.postman.com/t/is-it-possible-to-execute-an-external-program-from-postman/5693/1 "2019-05-31T08:23:49Z")

</div>

Hi all,

is it possible to run an external command from Postman´s pre-requesit Script or Test?  
Like call a batch file or an .exe? I´m on Windows 10, if that matters…

Many thanks,  
Christian

---

<div class="post-metadata">

**Author:** ![bhargavkaranam96](https://avatars.discourse-cdn.com/v4/letter/b/e99b99/32.png) [@bhargavkaranam96](https://community.postman.com/u/bhargavkaranam96)\
**Post date:** [June 1, 2019, 4:40am UTC](https://community.postman.com/t/is-it-possible-to-execute-an-external-program-from-postman/5693/2 "2019-06-01T04:40:31Z")

</div>

Hey @chrissbaumann,  
You can run a local server and write a small snippet like this to execute a batch file

```javascript
const http = require('http'),
  { exec } = require('child_process');

http.createServer((req, res) => {
  // Give the path of your bat or exe file
  exec('test.bat', (err, stdout, stderr) => {
    console.log({ err, stdout, stderr });
    
    if (err) {
      return res.writeHead(500).end(JSON.stringify(err));
    }
    
    // Output of the script in stdout
    return res.writeHead(200).end(stdout);
  });
}).listen(3000);

```

And in your pre-request / test script, you can call your local server’s URL ([http://localhost:3000](http://localhost:3000) here) that will execute the batch file and return the response for you. Something like this

```javascript
pm.sendRequest('http://localhost:3000', (err, response) => {
    // This will have the output of your batch file
    console.log(response.text());
})

```

You can execute exe files in the same way

---

<div class="post-metadata">

**Author:** ![chrissbaumann](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/chrissbaumann/32/5521_2.png) [@chrissbaumann](https://community.postman.com/u/chrissbaumann)\
**Post date:** [June 3, 2019, 6:32am UTC](https://community.postman.com/t/is-it-possible-to-execute-an-external-program-from-postman/5693/3 "2019-06-03T06:32:42Z")

</div>

Thx.  
What kind of server is that? How to start it?

---

<div class="post-metadata">

**Author:** ![kaustavdm](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kaustavdm/32/1778_2.png) [@kaustavdm](https://community.postman.com/u/kaustavdm)\
**Post date:** [June 3, 2019, 7:04am UTC](https://community.postman.com/t/is-it-possible-to-execute-an-external-program-from-postman/5693/4 "2019-06-03T07:04:14Z")

</div>

Hey @chrissbaumann, Postman can only trigger HTTP requests. There is no _direct_ way to run a local program through Postman. @bhargavkaranam96’s suggestion was to write a Node.js server which runs a local command (your required `.exe` or shell script) in a child process when you send a request to that server.

In this case, you will have to write that server and keep it running while you send the request from Postman.

That said, can you elaborate a bit more on your workflow, and what these batch files are supposed to do?

---

<div class="post-metadata">

**Author:** ![chrissbaumann](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/chrissbaumann/32/5521_2.png) [@chrissbaumann](https://community.postman.com/u/chrissbaumann)\
**Post date:** [June 3, 2019, 7:11am UTC](https://community.postman.com/t/is-it-possible-to-execute-an-external-program-from-postman/5693/5 "2019-06-03T07:11:49Z")

</div>

Thx.

Ok, understood.  
So basically it could be any kind of server, which then starts a process, when being called via Postman, no?

Basically I need to execute the same collections/ workflows in different environemnts (per client). The Postman/ API part is the same for every environment, but there´s a step in between, where a browser/ webpage needs to opened and some interaction is needed. Automating this interaction via Selenium/ Watir is far less effort, then doing it in Postman, so I´m searching for a possibility to call a browser GUI automation from Postman.

---

<div class="post-metadata">

**Author:** ![kaustavdm](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kaustavdm/32/1778_2.png) [@kaustavdm](https://community.postman.com/u/kaustavdm)\
**Post date:** [June 3, 2019, 7:39am UTC](https://community.postman.com/t/is-it-possible-to-execute-an-external-program-from-postman/5693/6 "2019-06-03T07:39:25Z")

</div>

> [@chrissbaumann](#):
>
> So basically it could be any kind of server, which then starts a process, when being called via Postman, no?

Yes. That’s the gist of it. You will have to trigger your GUI testing agent through a server. You can also have your server respond with a response code depending on the GUI test success/failure, and then assert on the response in Postman.

---

<div class="post-metadata">

**Author:** ![chrissbaumann](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/chrissbaumann/32/5521_2.png) [@chrissbaumann](https://community.postman.com/u/chrissbaumann)\
**Post date:** [June 5, 2019, 8:41am UTC](https://community.postman.com/t/is-it-possible-to-execute-an-external-program-from-postman/5693/7 "2019-06-05T08:41:06Z")

</div>

Awesome, thanks a lot!

Two small issues in your code I think.

In line 2 it has to be `const { exec } = require('child_process');`

When the started process ends, I get the following error:  
`TypeError: Cannot read property 'end' of undefined at exec (xxxxxxx\app.js:18:30) at ChildProcess.exithandler (child_process.js:285:7) at ChildProcess.emit (events.js:189:13) at maybeClose (internal/child_process.js:970:16) at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)`  
which is refering to this line `return res.writeHead(200).end(stdout);`

When I comment the line, it´s working w/o failure, but I assume, this is delivering the result/ output of the batch, so w/o that, I cannot access the result in Postman?

---

<div class="post-metadata">

**Author:** ![ptarte23](https://avatars.discourse-cdn.com/v4/letter/p/dbc845/32.png) [@ptarte23](https://community.postman.com/u/ptarte23)\
**Post date:** [July 16, 2020, 8:04am UTC](https://community.postman.com/t/is-it-possible-to-execute-an-external-program-from-postman/5693/8 "2020-07-16T08:04:47Z")

</div>

Hi @chrissbaumann, I am stuck at the similar situation (API flow requiring GUI involvement) for one of my client. Can you please confirm the solution stated above was working? Also, which server did you use to run it? And what was used for GUI testing

---

<div class="post-metadata">

**Author:** ![christianbaumann](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@christianbaumann](https://community.postman.com/u/christianbaumann)\
**Post date:** [July 16, 2020, 5:38pm UTC](https://community.postman.com/t/is-it-possible-to-execute-an-external-program-from-postman/5693/9 "2020-07-16T17:38:00Z")

</div>

Hi @ptarte23 ,

got as far as described in my last entry: Got the server running in node.js, but didn´t manage to get around the `TypeError`.  
So I went automating the web page via Postman, which is working fine and running daily in our builds for more than a a year now.

---

<div class="post-metadata">

**Author:** ![surendas](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@surendas](https://community.postman.com/u/surendas)\
**Post date:** [July 22, 2021, 5:41pm UTC](https://community.postman.com/t/is-it-possible-to-execute-an-external-program-from-postman/5693/10 "2021-07-22T17:41:40Z")

</div>

This article helped me in a workaround solution for an issue in Newman related to the URLEncoded body. Thank you very much Bhargav, Christianbaumann and Kaustavdm.

---

<div class="post-metadata">

**Author:** ![supply-cosmonaut-224](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@supply-cosmonaut-224](https://community.postman.com/u/supply-cosmonaut-224)\
**Post date:** [November 18, 2022, 12:50am UTC](https://community.postman.com/t/is-it-possible-to-execute-an-external-program-from-postman/5693/11 "2022-11-18T00:50:24Z")

</div>

It appears that child\_process is no longer allowed in pre-request scripts. Anyone have a work around?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [August 24, 2023, 4:25pm UTC](https://community.postman.com/t/is-it-possible-to-execute-an-external-program-from-postman/5693/12 "2023-08-24T16:25:15Z")

</div>

Closed on August 24, 2023, 4:25pm UTC


