# REST/EFx/BAQ Examples? -- From a Python Script

**URL:** <https://www.epiusers.help/t/rest-efx-baq-examples-from-a-python-script/115273>\
**Category:** Kinetic ERP\
**Created:** [May 31, 2024, 9:20pm UTC](https://www.epiusers.help/t/rest-efx-baq-examples-from-a-python-script/115273 "2024-05-31T21:20:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdtrent](https://www.epiusers.help/user_avatar/www.epiusers.help/jdtrent/32/915_2.png) [@jdtrent](https://www.epiusers.help/u/jdtrent)\
**Post date:** [May 31, 2024, 9:20pm UTC](https://www.epiusers.help/t/rest-efx-baq-examples-from-a-python-script/115273/1 "2024-05-31T21:20:36Z")

</div>

Hey,

Anyone have examples of accessing Epicor Functions and BAQs from a Python script?

I have the CURL code for an EFx I can start from but haven’t seen anything for BAQs.

Help a feller out?

Thanks,

Joe

---

<div class="post-metadata">

**Author:** ![klincecum](https://www.epiusers.help/user_avatar/www.epiusers.help/klincecum/32/100137_2.png) [@klincecum](https://www.epiusers.help/u/klincecum)\
**Post date:** [June 1, 2024, 1:40am UTC](https://www.epiusers.help/t/rest-efx-baq-examples-from-a-python-script/115273/2 "2024-06-01T01:40:18Z")

</div>

**BAQ:**

```auto
import requests

url = 'https://yourserver/server/api/v2/odata/Company/BaqSvc/KEV_ABC/Data'

headers = {'accept' : 'application/json', 'X-API-Key' : 'blablablabla'}

session = requests.Session()

user = 'username'
password = 'password'

session.auth = (user, password)

response = session.get(url, params=None, headers=headers)

print(response.content)

```

–

**Function:**

```auto
import requests
import json

url = 'https://yourserver/server/api/v2/efx/Company/TestDataLibrary/StringDataFromNotesFromOtherFunction'

headers = {'accept' : 'application/json',
           'X-API-Key' : 'blablabla',
           'Content-Type' : 'application/json'
           }

session = requests.Session()

user = 'username'
password = 'password'

session.auth = (user, password)

data = json.dumps({
  'parmesanParm1': 'something',
  'input2': 'somethingElse'
})

response = session.post(url, data=data, headers=headers)

print(response.content)

```

Look about right @deepak ?

---

<div class="post-metadata">

**Author:** ![ckoch](https://www.epiusers.help/user_avatar/www.epiusers.help/ckoch/32/32011_2.png) [@ckoch](https://www.epiusers.help/u/ckoch)\
**Post date:** [June 1, 2024, 5:43am UTC](https://www.epiusers.help/t/rest-efx-baq-examples-from-a-python-script/115273/3 "2024-06-01T05:43:06Z")

</div>

Here I was about to post my own thread over in Code Review about this little Python module I wrote that was probably going to get lost over the weekend, but now I have a good reason to share it!

[Epycor.zip](https://www.epiusers.help/uploads/short-url/2cbtsP4bGZ4JXaKtCJHQxY2fxw0.zip) (3.6 KB)

This is a really simple Python module that forms the request URL based on how you call the client methods:

```py
# Epycor - Python package for Epicor/Kinetic ERP REST API use

# Example usage:

>>> from Epycor.client import ERP
>>> foo = ERP(server_root, instance_name, api_key, company_ID)
>>> foo.Login(username, pwd)
>>> bar = foo.Erp.BO.VendorSvc.GetByVendID(vendorID="DIGCO").json()['returnObj']
>>> bar['Vendor'][0]['Name']
'Digi-Key Corporation'
>>> foo.Logout()

```

For an example using EFx:

```auto
from Epycor.client import ERP
from getpass import getpass, getuser

def get_Epicor():
    user_id = getuser() # will only work if Windows login matches Epicor
    pwd = getpass(f"User: {user_id}\nPassword: ")
    # log in
    our_epicor = ERP(server_root, instance_name, api_key, company_ID)
    our_epicor.Login(user_id, pwd)
    print("Logged in.")

    # the name of our EFx library is "TSConfigBridge"
    our_config_bridge = our_epicor.Erp.Efx.TSConfigBridge

    return our_epicor, our_config_bridge

if __name__ == ' __main__':
    epicor, config_bridge = get_Epicor()

    print("Creating quote header...")
    resp = config_bridge.CreateNewQuote(QuoteNum=config_quote_num, CustID=cust_id, SalesRepID='ckoch',
                                        InternalNotes="some notes", Notes="some other notes", Comments="some comments")
    resp.raise_for_status() # will raise an exception if not HTTP 200 OK
    resp_json = resp.json()
    epi_quote_num = resp_json['EpicorQuoteNum']
    print(f"Created quote number {epi_quote_num}. Errors: {resp_json['error']}")

```

This module does try to maintain one session ID from Epicor with the same license claim until `ERP.Logout()` or `ERP.Close()` is called, at which time the module tries to call the API endpoints to release the session. I haven’t added a convenient way of calling BAQs in this module yet, though.

I originally wrote this to aid in testing an EFx library I’m developing to provide a special API to our own in-house product configurator webapp.

---

<div class="post-metadata">

**Author:** ![ckoch](https://www.epiusers.help/user_avatar/www.epiusers.help/ckoch/32/32011_2.png) [@ckoch](https://www.epiusers.help/u/ckoch)\
**Post date:** [June 1, 2024, 6:12am UTC](https://www.epiusers.help/t/rest-efx-baq-examples-from-a-python-script/115273/4 "2024-06-01T06:12:46Z")

</div>

From the documentation, I think BAQs should be executed by a URL like `https://<baseURL>/<ERPInstance>/api/v2/odata/<companyID>/BaqSvc/<BaqID>/Data` with parameters for the BAQ being URL-encoded (i.e. after a question-mark symbol) like `https://erp.mycompany.com/E10Test/api/v2/odata/EPIC01/BaqSvc/CK_MyBAQ/Data?someparam='somevalue'`. Updatable BAQs are done using a PATCH request passing the rows to update in the body of the request, and you can do a GET on the `/GetNew` endpoint instead of `/Data` to get a new blank row. I might try to add this to Epycor next week if I have time, but if I don’t hopefully this still helps.

---

<div class="post-metadata">

**Author:** ![klincecum](https://www.epiusers.help/user_avatar/www.epiusers.help/klincecum/32/100137_2.png) [@klincecum](https://www.epiusers.help/u/klincecum)\
**Post date:** [June 1, 2024, 3:12pm UTC](https://www.epiusers.help/t/rest-efx-baq-examples-from-a-python-script/115273/5 "2024-06-01T15:12:03Z")

</div>

Man the license on that is 🔥 🤣

> [@ckoch](#):
>
> Here I was about to post my own thread over in Code Review

It’s sure gonna get lost here, so get over there and post a dedicated thread too.

---

<div class="post-metadata">

**Author:** ![jdtrent](https://www.epiusers.help/user_avatar/www.epiusers.help/jdtrent/32/915_2.png) [@jdtrent](https://www.epiusers.help/u/jdtrent)\
**Post date:** [June 3, 2024, 1:25pm UTC](https://www.epiusers.help/t/rest-efx-baq-examples-from-a-python-script/115273/6 "2024-06-03T13:25:05Z")

</div>

Thanks, folks. I’ll give it a try!

Joe

---

<div class="post-metadata">

**Author:** ![deepak](https://www.epiusers.help/user_avatar/www.epiusers.help/deepak/32/55818_2.png) [@deepak](https://www.epiusers.help/u/deepak)\
**Post date:** [June 3, 2024, 2:13pm UTC](https://www.epiusers.help/t/rest-efx-baq-examples-from-a-python-script/115273/7 "2024-06-03T14:13:05Z")

</div>

> [@klincecum](#):
>
> ```auto
> session = requests.Session()
> 
> ```

@klincecum You could use session but if it is simple request fire and forget, direct GET request is enough.

You could save this string instead of clear text user id and password

> **[Generate HTTP Basic Auth Header](https://www.debugbear.com/basic-auth-header-generator)**
>
> Convert a username and password into an Authorization header for HTTP Basic Auth.

```auto

import requests

url = "APIURL"

payload = {}
headers = {
  'x-api-key': 'APIKEY',
  'Authorization': 'Basic dXNlcm5hbWU6cGFzc3dvcmQ='
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)

```

---

<div class="post-metadata">

**Author:** ![ckoch](https://www.epiusers.help/user_avatar/www.epiusers.help/ckoch/32/32011_2.png) [@ckoch](https://www.epiusers.help/u/ckoch)\
**Post date:** [June 7, 2024, 12:31am UTC](https://www.epiusers.help/t/rest-efx-baq-examples-from-a-python-script/115273/8 "2024-06-07T00:31:32Z")

</div>

Here you go! Updated to support BAQs and switched to a BSD 3-clause license to put it on GitHub.

[https://www.epiusers.help/t/epycor-a-simple-python-package-for-using-epicors-rest-api/115509](https://www.epiusers.help/t/epycor-a-simple-python-package-for-using-epicors-rest-api/115509)
