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

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

BAQ:

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:

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 ?

1 Like

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 (3.6 KB)

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

# 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:

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.

3 Likes

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.

2 Likes

Man the license on that is :fire: :rofl:

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

2 Likes

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

Joe

2 Likes

@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


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)


2 Likes

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

1 Like