# httpClient instance creation advice

**URL:** <https://forum.inductiveautomation.com/t/httpclient-instance-creation-advice/90568>\
**Category:** Ignition\
**Tags:** perspective, scripting\
**Created:** [May 17, 2024, 5:07pm UTC](https://forum.inductiveautomation.com/t/httpclient-instance-creation-advice/90568 "2024-05-17T17:07:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![YF129701](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/yf129701/32/60498_2.png) [@YF129701](https://forum.inductiveautomation.com/u/YF129701)\
**Post date:** [May 17, 2024, 5:07pm UTC](https://forum.inductiveautomation.com/t/httpclient-instance-creation-advice/90568/1 "2024-05-17T17:07:59Z")

</div>

I would like to get some input on a script library I'm writing to wrap ERP APIs and make them easy to use from Ignition. Right now, I have a class of type Transactor that will have methods within itself for specific requests. For instance, the stripped down version looks like this:

```python
class Transactor():
    def __init__ (self, transType):
        self.transType = transType
    
    def getWorkOrder(woNum):
        # make http call to endpoint to get work order

    def reportConsumption(item, quantity):
        # make http call to endpoint to post consumption

    ... etc ...

```

My question revolves around how often I should be creating an `httpClient` whenever I have this paradigm. I think I can probably create one `httpClient` in the constructor of the class and then use that instance for any methods. That way, on the consumer side, I can do something like

```python
erpObj = shared.erp.Transactor('erpName')
erpObj.getWorkOrder('abc123')
...
erpObj.reportConsumption('a123312', 12.9)

```

The other way that I could do this (but seems like a bad idea) is to create a new `httpClient` within each method in my class. This seems unnecessary, but I don't fully understand the implications if I don't do it this way (this is the simpler way to do it in my mind, and how I built out a POC).

Any input / advice would be very helpful!

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [May 17, 2024, 5:15pm UTC](https://forum.inductiveautomation.com/t/httpclient-instance-creation-advice/90568/2 "2024-05-17T17:15:29Z")

</div>

Instantiate it at the top level of the script library and use that one instance for all of your work.

> [@httpClient. java.io.IOException: Unable to establish loopback connection](https://forum.inductiveautomation.com/t/httpclient-java-io-ioexception-unable-to-establish-loopback-connection/53876/3):
>
> Also, as a generally good practice, you should instantiate your `httpClient()` instance _outside_ of the function

---

<div class="post-metadata">

**Author:** ![YF129701](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/yf129701/32/60498_2.png) [@YF129701](https://forum.inductiveautomation.com/u/YF129701)\
**Post date:** [May 17, 2024, 5:24pm UTC](https://forum.inductiveautomation.com/t/httpclient-instance-creation-advice/90568/3 "2024-05-17T17:24:07Z")

</div>

Dang... I know I read that somewhere but every search item I got for `httpClient` didn't bring up any relevant threads, at least from what I can see. I started down the road of using just one client and I think it'll be nice and clean. My setup is a little bit more complicated than this, since I have another class that inherits from Transactor and then a database configuration surface to manage the specific ERP transactors, but in essence, the problem definition is exactly what I stated.

Thank you for the quick link!
